Esc
Start typing to search...

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

Example:
import Time

Time.now ()
Try it

Time.fromHms

Int -> Int -> Int -> Result Time TimeError

Create a Time from hour, minute, second

Example:
import Time

Time.fromHms 14 30 45
Try it

Time.fromHmsNano

Int -> Int -> Int -> Int -> Result Time TimeError

Create a Time from hour, minute, second, nanosecond

Example:
import Time

Time.fromHmsNano 14 30 45 123456789
Try it

Time.midnight

() -> Time

Get midnight (00:00:00)

Example:
import Time

Time.midnight ()
Try it

Time.noon

() -> Time

Get noon (12:00:00)

Example:
import Time

Time.noon ()
Try it

Parsing

Time.parse

String -> String -> Result Time TimeError

Parse a time string using a strftime format

Example:
import Time

Time.parse "02:30 PM" "%I:%M %p"
Try it

Time.parseIso

String -> Result Time TimeError

Parse an ISO 8601 time string (HH:MM:SS)

Example:
import Time

Time.parseIso "14:30:45"
Try it

Components

Time.hour

Time -> Int

Get the hour component (0-23)

Example:
import Time

case Time.fromHms 14 30 45 of
    Ok t -> Time.hour t  -- 14
    Err _ -> 0
Try it

Time.minute

Time -> Int

Get the minute component (0-59)

Example:
import Time

case Time.fromHms 14 30 45 of
    Ok t -> Time.minute t  -- 30
    Err _ -> 0
Try it

Time.second

Time -> Int

Get the second component (0-59)

Example:
import Time

case Time.fromHms 14 30 45 of
    Ok t -> Time.second t  -- 45
    Err _ -> 0
Try it

Time.nanosecond

Time -> Int

Get the nanosecond component (0-999999999)

Example:
import Time

case Time.fromHmsNano 14 30 45 500000000 of
    Ok t -> Time.nanosecond t  -- 500000000
    Err _ -> 0
Try it

Manipulation

Time.addSeconds

Int -> Time -> Time

Add seconds to a Time

Example:
import Time

case Time.fromHms 14 30 0 of
    Ok t -> Time.toIsoString (Time.addSeconds 30 t)
    Err _ -> "error"
Try it

Time.addMinutes

Int -> Time -> Time

Add minutes to a Time

Example:
import Time

case Time.fromHms 14 30 0 of
    Ok t -> Time.toIsoString (Time.addMinutes 15 t)
    Err _ -> "error"
Try it

Time.addHours

Int -> Time -> Time

Add hours to a Time

Example:
import Time

case Time.fromHms 10 0 0 of
    Ok t -> Time.toIsoString (Time.addHours 4 t)
    Err _ -> "error"
Try it

Difference

Time.diffSeconds

Time -> Time -> Int

Calculate the difference in seconds between two times

Example:
import Time

case (Time.fromHms 10 0 0, Time.fromHms 10 0 30) of
    (Ok t1, Ok t2) -> Time.diffSeconds t1 t2  -- 30
    _ -> 0
Try it

Time.diffMinutes

Time -> Time -> Int

Calculate the difference in minutes between two times

Example:
import Time

case (Time.fromHms 10 0 0, Time.fromHms 10 30 0) of
    (Ok t1, Ok t2) -> Time.diffMinutes t1 t2  -- 30
    _ -> 0
Try it

Formatting

Time.format

String -> Time -> String

Format a time using a strftime format string

Example:
import Time

case Time.fromHms 14 30 45 of
    Ok t -> Time.format "%H:%M" t  -- "14:30"
    Err _ -> "error"
Try it

Time.toIsoString

Time -> String

Format as ISO 8601 (HH:MM:SS)

Example:
import Time

case Time.fromHms 14 30 45 of
    Ok t -> Time.toIsoString t  -- "14:30:45"
    Err _ -> "error"
Try it

Comparison

Time.isBefore

Time -> Time -> Bool

Check if the first time is before the second

Example:
import Time

case (Time.fromHms 10 0 0, Time.fromHms 14 0 0) of
    (Ok t1, Ok t2) -> Time.isBefore t1 t2  -- True
    _ -> False
Try it

Time.isAfter

Time -> Time -> Bool

Check if the first time is after the second

Example:
import Time

case (Time.fromHms 14 0 0, Time.fromHms 10 0 0) of
    (Ok t1, Ok t2) -> Time.isAfter t1 t2  -- True
    _ -> False
Try it

Time.isEqual

Time -> Time -> Bool

Check if two times are equal

Example:
import Time

case (Time.fromHms 14 30 45, Time.fromHms 14 30 45) of
    (Ok t1, Ok t2) -> Time.isEqual t1 t2  -- True
    _ -> False
Try it

Time.compare

Time -> Time -> Int

Compare two times (-1 if before, 0 if equal, 1 if after)

Example:
import Time

case (Time.fromHms 10 0 0, Time.fromHms 14 0 0) of
    (Ok t1, Ok t2) -> Time.compare t1 t2  -- -1
    _ -> 0
Try it