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

Search

Menu

Temporal Types

Halyard provides dedicated types for working with dates, times, and durations. Each has its own module with creation, parsing, formatting, arithmetic, and comparison functions.

Date

The Date type represents a calendar date (year, month, day) without a time component.

Creating Dates

halyard
-- Creating dates from year, month, day
import Date

Date.fromYmd 2024 3 15

Parsing Dates

Parse from ISO 8601 format or a custom strftime format:

halyard
-- Parsing dates from strings
import Date

Date.parseIso "2024-06-15"
halyard
import Date

-- Custom format
Date.parse "15/06/2024" "%d/%m/%Y"

Date Components

halyard
-- Extracting date components
import Date

let date =
    case Date.fromYmd 2024 3 15 of
        Ok d -> d
        Err _ -> Date.today ()

(Date.year date, Date.month date, Date.day date)

Other components include weekday (1=Monday, 7=Sunday), dayOfYear, weekNumber, isLeapYear, and daysInMonth.

Date Arithmetic

halyard
-- Date arithmetic
import Date

let date =
    case Date.fromYmd 2024 3 15 of
        Ok d -> d
        Err _ -> Date.today ()

-- Add 30 days
let later = Date.addDays 30 date

Date.toIsoString later

Available operations: addDays, addWeeks, addMonths, addYears. Month and year addition handles edge cases (e.g., Jan 31 + 1 month = Feb 28).

Comparing Dates

halyard
-- Comparing dates and computing differences
import Date

let start =
    case Date.fromYmd 2024 1 1 of
        Ok d -> d
        Err _ -> Date.today ()

let end =
    case Date.fromYmd 2024 2 1 of
        Ok d -> d
        Err _ -> Date.today ()

Date.diffDays start end

Comparison functions: isBefore, isAfter, isEqual, compare, diffDays.

Time

The Time type represents a time of day (hour, minute, second) without a date component.

Creating Times

halyard
-- Creating time values
import Time

Time.fromHms 14 30 0

Convenience constructors: midnight () returns 00:00:00, noon () returns 12:00:00.

Parsing Times

halyard
-- Parsing time from strings
import Time

Time.parseIso "09:15:30"

Time Components

halyard
-- Extracting time components
import Time

let t =
    case Time.fromHms 14 30 0 of
        Ok t -> t
        Err _ -> Time.midnight ()

(Time.hour t, Time.minute t, Time.second t)

Time Arithmetic

halyard
-- Time arithmetic
import Time

let t =
    case Time.fromHms 14 30 0 of
        Ok t -> t
        Err _ -> Time.midnight ()

-- Add 45 minutes
let later = Time.addMinutes 45 t

Time.toIsoString later

Available operations: addHours, addMinutes, addSeconds. Time wraps around midnight (e.g., 23:00 + 2 hours = 01:00).

Duration

The Duration type represents a span of time, which can be positive or negative.

Creating Durations

halyard
-- Creating and converting durations
import Duration

let twoHours = Duration.fromHours 2

Duration.toSeconds twoHours

Creation functions: fromMillis, fromSeconds, fromMinutes, fromHours, fromDays, fromWeeks, zero.

Duration Arithmetic

halyard
-- Duration arithmetic
import Duration

let oneHour = Duration.fromHours 1

let thirtyMin = Duration.fromMinutes 30

case Duration.add oneHour thirtyMin of
    Ok d -> Duration.toSeconds d
    Err _ -> 0

Operations: add, sub, mul, div, negate, abs. Arithmetic returns Result Duration DurationError for overflow safety.

Converting Between Units

halyard
-- Converting between duration units
import Duration

let d = Duration.fromMinutes 150

-- Get total hours (truncated)
Duration.toHours d

Conversion functions: toMillis, toSeconds, toMinutes, toHours, toDays, toWeeks.

DateTime Interop

The DateTime module provides functions to convert between Date, Time, and DateTime:

Extract Date or Time

halyard
import Date
-- Extract Date component from DateTime
import DateTime

let dt =
    case DateTime.fromYmd 2024 6 15 of
        Ok d -> d
        Err _ -> DateTime.now ()

let date = DateTime.date dt

Date.year date
halyard
-- Extract Time component from DateTime
import DateTime
import Time

let dt =
    case DateTime.fromParts 2024 6 15 14 30 0 of
        Ok d -> d
        Err _ -> DateTime.now ()

let time = DateTime.time dt

Time.hour time

Combine Date and Time

halyard
import Date
-- Combine Date and Time into DateTime
import DateTime
import Time

let date =
    case Date.fromYmd 2024 6 15 of
        Ok d -> d
        Err _ -> Date.today ()

let time =
    case Time.fromHms 14 30 0 of
        Ok t -> t
        Err _ -> Time.midnight ()

let dt = DateTime.combine date time

DateTime.formatRfc3339 dt

Convert Date to DateTime

halyard
import Date
-- Convert Date to DateTime at midnight UTC
import DateTime

let date =
    case Date.fromYmd 2024 1 1 of
        Ok d -> d
        Err _ -> Date.today ()

let dt = DateTime.fromDate date

DateTime.hour dt

Error Handling

Date and Time functions that can fail return typed error results:

halyard
import Date

-- Invalid date returns a typed error
case Date.fromYmd 2024 2 30 of
    Ok date -> Date.toIsoString date
    Err _ -> "Invalid day for this month"

Error variants for DateError: InvalidMonth, InvalidDay, InvalidDate, ParseError. Error variants for TimeError: InvalidHour, InvalidMinute, InvalidSecond, InvalidNanosecond, InvalidTime, ParseError.

Next Steps

See the individual stdlib module pages for complete function references: Date, Time, Duration, DateTime.