Get current hour / minute / second in Haskell

Problem:

Using haskell, you want to get the current hour / minute / second (for the current timezone) as integral values.

Solution

You need the time package for this task. Use cabal install time to install it.

Also see this previous blogpost on how to get the current year / month / date.

import Data.Time.Clock
import Data.Time.LocalTime

main = do
    now <- getCurrentTime
    timezone <- getCurrentTimeZone
    let (TimeOfDay hour minute second) = localTimeOfDay $ utcToLocalTime timezone now
    putStrLn $ "Hour: " ++ show hour
    putStrLn $ "Minute: " ++ show minute
    -- Note: Second is of type @Pico@: It contains a fractional part.
    -- Use @fromIntegral@ to convert it to a plain integer.
    putStrLn $ "Second: " ++ show second