Time Module
Functions for working with time of day (without date) backed by chrono.
The Time module provides a Time type for representing clock time. Like Date, Time uses Result with typed TimeError variants for precise error handling.
Common patterns
import Time
Time.fromHms 14 30 0
Time.now () |> Time.format
Return types
Functions that can fail return Result Time TimeError.
Functions
Creation
Time.now
() -> Time
Get the current local time
import Time
Time.now ()Try itTime.fromHms
Int -> Int -> Int -> Result Time TimeError
Create a Time from hour, minute, second
import Time
Time.fromHms 14 30 45Try itTime.fromHmsNano
Int -> Int -> Int -> Int -> Result Time TimeError
Create a Time from hour, minute, second, nanosecond
import Time
Time.fromHmsNano 14 30 45 123456789Try itTime.midnight
() -> Time
Get midnight (00:00:00)
import Time
Time.midnight ()Try itTime.noon
() -> Time
Get noon (12:00:00)
import Time
Time.noon ()Try itParsing
Time.parse
String -> String -> Result Time TimeError
Parse a time string using a strftime format
import Time
Time.parse "02:30 PM" "%I:%M %p"Try itTime.parseIso
String -> Result Time TimeError
Parse an ISO 8601 time string (HH:MM:SS)
import Time
Time.parseIso "14:30:45"Try itComponents
Time.hour
Time -> Int
Get the hour component (0-23)
import Time
case Time.fromHms 14 30 45 of
Ok t -> Time.hour t -- 14
Err _ -> 0Try itTime.minute
Time -> Int
Get the minute component (0-59)
import Time
case Time.fromHms 14 30 45 of
Ok t -> Time.minute t -- 30
Err _ -> 0Try itTime.second
Time -> Int
Get the second component (0-59)
import Time
case Time.fromHms 14 30 45 of
Ok t -> Time.second t -- 45
Err _ -> 0Try itTime.nanosecond
Time -> Int
Get the nanosecond component (0-999999999)
import Time
case Time.fromHmsNano 14 30 45 500000000 of
Ok t -> Time.nanosecond t -- 500000000
Err _ -> 0Try itManipulation
Time.addSeconds
Int -> Time -> Time
Add seconds to a Time
import Time
case Time.fromHms 14 30 0 of
Ok t -> Time.toIsoString (Time.addSeconds 30 t)
Err _ -> "error"Try itTime.addMinutes
Int -> Time -> Time
Add minutes to a Time
import Time
case Time.fromHms 14 30 0 of
Ok t -> Time.toIsoString (Time.addMinutes 15 t)
Err _ -> "error"Try itTime.addHours
Int -> Time -> Time
Add hours to a Time
import Time
case Time.fromHms 10 0 0 of
Ok t -> Time.toIsoString (Time.addHours 4 t)
Err _ -> "error"Try itDifference
Time.diffSeconds
Time -> Time -> Int
Calculate the difference in seconds between two times
import Time
case (Time.fromHms 10 0 0, Time.fromHms 10 0 30) of
(Ok t1, Ok t2) -> Time.diffSeconds t1 t2 -- 30
_ -> 0Try itTime.diffMinutes
Time -> Time -> Int
Calculate the difference in minutes between two times
import Time
case (Time.fromHms 10 0 0, Time.fromHms 10 30 0) of
(Ok t1, Ok t2) -> Time.diffMinutes t1 t2 -- 30
_ -> 0Try itFormatting
Time.format
String -> Time -> String
Format a time using a strftime format string
import Time
case Time.fromHms 14 30 45 of
Ok t -> Time.format "%H:%M" t -- "14:30"
Err _ -> "error"Try itTime.toIsoString
Time -> String
Format as ISO 8601 (HH:MM:SS)
import Time
case Time.fromHms 14 30 45 of
Ok t -> Time.toIsoString t -- "14:30:45"
Err _ -> "error"Try itComparison
Time.isBefore
Time -> Time -> Bool
Check if the first time is before the second
import Time
case (Time.fromHms 10 0 0, Time.fromHms 14 0 0) of
(Ok t1, Ok t2) -> Time.isBefore t1 t2 -- True
_ -> FalseTry itTime.isAfter
Time -> Time -> Bool
Check if the first time is after the second
import Time
case (Time.fromHms 14 0 0, Time.fromHms 10 0 0) of
(Ok t1, Ok t2) -> Time.isAfter t1 t2 -- True
_ -> FalseTry itTime.isEqual
Time -> Time -> Bool
Check if two times are equal
import Time
case (Time.fromHms 14 30 45, Time.fromHms 14 30 45) of
(Ok t1, Ok t2) -> Time.isEqual t1 t2 -- True
_ -> FalseTry itTime.compare
Time -> Time -> Int
Compare two times (-1 if before, 0 if equal, 1 if after)
import Time
case (Time.fromHms 10 0 0, Time.fromHms 14 0 0) of
(Ok t1, Ok t2) -> Time.compare t1 t2 -- -1
_ -> 0Try it