Extracting elements out of Time
We can deconstruct via pattern matching:
let (Time myHour myMinute mySecond myMillisecond) = myTime
let myHour = hour myTime myMinute = minute myTime mySecond = second myTime myMillisecond = millisecond myTime
Adjusting a Time value
setHour :: Hour -> Time -> Time setMinute :: Minute -> Time -> Time setSecond :: Second -> Time -> Time setMillisecond :: Millisecond -> Time -> Time
It’s also possible to use adjust.
adjust :: forall d. Duration d => d -> Time -> Tuple Days Time
This function adjusts a time value with a Duration offset. The result includes a remainder value of the whole number of days involved in the adjustment, for example, if a time of 23:00:00:00 has a duration of +2 hours added to it, the result will be 1 day, and 01:00:00:00. Correspondingly, if the dureation is negative, a negative number of days may also be returned as the remainder.
Difference between two Time values
diff :: forall d. Duration d => Time -> Time -> d
The result of
diff is one of the types (Days,
Seconds, Milliseconds) which implements the
Duration type class. We need to help the compiler
by specifying the type when it’s not clear from the context.
daysDiff :: Days daysDiff = diff date1 date2 hoursDiff :: Hours hoursDiff = diff date1 date2