Time in Haskell

import Data.Time
import Data.Time.Clock.POSIX

main :: IO ()
main = do
    -- We'll start by getting the current time.
    now <- getCurrentTime
    print now

    -- You can build a UTCTime by providing the year, month, day, etc.
    let then' = UTCTime (fromGregorian 2009 11 17) (timeOfDayToTime (TimeOfDay 20 34 58))
    print then'

    -- You can extract the various components of the time value as expected.
    print $ utctYear then'
    print $ utctMonth then'
    print $ utctDay then'
    print $ utctHour then'
    print $ utctMinute then'
    print $ utctSecond then'
    print $ utctDayOfWeek then'

    -- These functions compare two times, testing if the first occurs before, after,
    -- or at the same time as the second, respectively.
    print $ then' < now
    print $ then' > now
    print $ then' == now

    -- The diffUTCTime function returns a NominalDiffTime representing
    -- the interval between two times.
    let diff = diffUTCTime now then'
    print diff

    -- We can compute the length of the duration in various units.
    print $ nominalDiffTimeToSeconds diff / 3600  -- hours
    print $ nominalDiffTimeToSeconds diff / 60    -- minutes
    print $ nominalDiffTimeToSeconds diff         -- seconds

    -- You can use addUTCTime to advance a time by a given duration,
    -- or with a negative value to move backwards by a duration.
    print $ addUTCTime diff then'
    print $ addUTCTime (negate diff) then'

-- Helper functions to extract components from UTCTime
utctYear :: UTCTime -> Integer
utctYear = (\(y,_,_) -> y) . toGregorian . utctDay

utctMonth :: UTCTime -> Int
utctMonth = (\(_,m,_) -> m) . toGregorian . utctDay

utctDay :: UTCTime -> Int
utctDay = (\(_,_,d) -> d) . toGregorian . utctDay

utctHour :: UTCTime -> Int
utctHour = todHour . timeToTimeOfDay . utctDayTime

utctMinute :: UTCTime -> Int
utctMinute = todMin . timeToTimeOfDay . utctDayTime

utctSecond :: UTCTime -> Int
utctSecond = floor . todSec . timeToTimeOfDay . utctDayTime

utctDayOfWeek :: UTCTime -> String
utctDayOfWeek = show . dayOfWeek . utctDay

This Haskell code demonstrates working with times and durations. Here’s an explanation of the key parts:

  1. We use the Data.Time and Data.Time.Clock.POSIX modules for time-related functionality.

  2. getCurrentTime is used to get the current time.

  3. We create a specific time using UTCTime and fromGregorian.

  4. Various helper functions (utctYear, utctMonth, etc.) are defined to extract components from a UTCTime.

  5. Time comparison is done using standard comparison operators (<, >, ==).

  6. diffUTCTime is used to calculate the duration between two times.

  7. We convert the duration to different units using nominalDiffTimeToSeconds.

  8. addUTCTime is used to add or subtract durations from a time.

To run this program, save it to a file (e.g., time_example.hs) and use the following commands:

$ ghc time_example.hs
$ ./time_example

This will compile and run the Haskell program, displaying various time-related outputs.

Note that Haskell’s time handling is somewhat different from Go’s. Haskell uses UTCTime for absolute times and NominalDiffTime for durations. The concept is similar, but the specific functions and types differ.