Time operations
Time operations
Extracting elements out of Time
Extracint a Hour, Minute, Second and Millisecond is simple.
We can deconstruct via pattern matching:
let (Time myHour myMinute mySecond myMillisecond) = myTime
Or we can use the hour, minute, second and millisecond functions:
let myHour = hour myTime
myMinute = minute myTime
mySecond = second myTime
myMillisecond = millisecond myTime
Adjusting a Time value
It’s possible to alter a Time value directly by using setHour, setMinute, setSecond and setMillisecond to change one of its components.
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
To calculate the difference between two Times we can use diff.
diff :: forall d. Duration d => Time -> Time -> d
The result of diff
is one of the types (Days,
Hours, Minute,
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