Skip to content
Halyard
Project
Source code on Codeberg (opens in new tab)

Search

Menu

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

halyard
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

() ->{Clock} Time

Get the current local time

Example:
halyard
import Time

Time.now ()

Time.fromHms

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

Create a Time from hour, minute, second

Example:
halyard
import Time

Time.fromHms 14 30 45

Time.fromHmsNano

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

Create a Time from hour, minute, second, nanosecond

Example:
halyard
import Time

Time.fromHmsNano 14 30 45 123456789

Time.midnight

() -> Time

Get midnight (00:00:00)

Example:
halyard
import Time

Time.midnight ()

Time.noon

() -> Time

Get noon (12:00:00)

Example:
halyard
import Time

Time.noon ()

Parsing

Time.parse

String -> String -> Result Time TimeError

Parse a time string using a strftime format

Example:
halyard
import Time

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

Time.parseIso

String -> Result Time TimeError

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

Example:
halyard
import Time

Time.parseIso "14:30:45"

Components

Time.hour

Time -> Int

Get the hour component (0-23)

Example:
halyard
import Time

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

Time.minute

Time -> Int

Get the minute component (0-59)

Example:
halyard
import Time

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

Time.second

Time -> Int

Get the second component (0-59)

Example:
halyard
import Time

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

Time.nanosecond

Time -> Int

Get the nanosecond component (0-999999999)

Example:
halyard
import Time

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

Manipulation

Time.addSeconds

Int -> Time -> Time

Add seconds to a Time

Example:
halyard
import Time

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

Time.addMinutes

Int -> Time -> Time

Add minutes to a Time

Example:
halyard
import Time

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

Time.addHours

Int -> Time -> Time

Add hours to a Time

Example:
halyard
import Time

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

Difference

Time.diffSeconds

Time -> Time -> Int

Calculate the difference in seconds between two times

Example:
halyard
import Time

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

Time.diffMinutes

Time -> Time -> Int

Calculate the difference in minutes between two times

Example:
halyard
import Time

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

Formatting

Time.format

String -> Time -> String

Format a time using a strftime format string

Example:
halyard
import Time

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

Time.toIsoString

Time -> String

Format as ISO 8601 (HH:MM:SS)

Example:
halyard
import Time

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

Comparison

Time.isBefore

Time -> Time -> Bool

Check if the first time is before the second

Example:
halyard
import Time

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

Time.isAfter

Time -> Time -> Bool

Check if the first time is after the second

Example:
halyard
import Time

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

Time.isEqual

Time -> Time -> Bool

Check if two times are equal

Example:
halyard
import Time

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

Time.compare

Time -> Time -> Ordering

Compare two times, returning an Ordering value

Example:
halyard
import Time
import Ordering

case (Time.fromHms 10 0 0, Time.fromHms 14 0 0) of
    (Ok t1, Ok t2) -> case Time.compare t1 t2 of
        Ordering::Lt -> "before"
        Ordering::Eq -> "same"
        Ordering::Gt -> "after"
    _ -> "error"