🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of agbell

agbell's solution

to Gigasecond in the Haskell Track

Published at Jul 13 2018 · 0 comments
Test suite

Calculate the moment when someone has lived for 10^9 seconds.

A gigasecond is 10^9 (1,000,000,000) seconds.

Getting Started

For installation and learning resources, refer to the exercism help page.

Running the tests

To run the test suite, execute the following command:

stack test

If you get an error message like this...

No .cabal file found in directory

You are probably running an old stack version and need to upgrade it.

Otherwise, if you get an error message like this...

No compiler found, expected minor version match with...
Try running "stack setup" to install the correct GHC...

Just do as it says and it will download and install the correct compiler version:

stack setup

Running GHCi

If you want to play with your solution in GHCi, just run the command:

stack ghci

Feedback, Issues, Pull Requests

The exercism/haskell repository on GitHub is the home for all of the Haskell exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!


Chapter 9 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=09

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


import Data.Time.Clock   (UTCTime)
import Test.Hspec        (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import Gigasecond (fromDay)

import Data.Time.Format
  ( ParseTime
  , TimeLocale
  , defaultTimeLocale
  , iso8601DateFormat
  , parseTimeOrError

readTime :: ParseTime t => TimeLocale -> String -> String -> t
readTime = parseTimeOrError True

main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = describe "fromDay" $ do

          let dt = readTime defaultTimeLocale
                   (iso8601DateFormat (Just "%T%Z")) :: String -> UTCTime

          it "from apr 25 2011" $
            fromDay (dt "2011-04-25T00:00:00Z")
            `shouldBe` dt "2043-01-01T01:46:40Z"

          it "from jun 13 1977" $
            fromDay (dt "1977-06-13T00:00:00Z")
            `shouldBe` dt "2009-02-19T01:46:40Z"

          it "from jul 19 1959" $
            fromDay (dt "1959-07-19T00:00:00Z")
            `shouldBe` dt "1991-03-27T01:46:40Z"
module Gigasecond (fromDay) where

import Data.Time.Calendar(Day, addDays)

fromDay :: Day -> Day
fromDay day = addDays gigaday day

gigaday :: Integer
gigaday = foldr (flip div) gigaSecond divisors
   divisors = [60,60,24]
   gigaSecond = 10^9

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?