TimeZone
The TimeZone module provides a wrapper type for timezone identifiers,
offering type safety over raw string-based timezone operations. It also
defines the DateTimeError type for date/time parsing errors.
import Timezone
# Create a TimeZone from a string identifier
tz = TimeZone{id: "America/New_York"}
utc = TimeZone{id: "UTC"}
local = TimeZone{id: Time.local-timezone}
TimeZone Type
TimeZone
type TimeZone = TimeZone{id: String}
A wrapper type for timezone identifiers. The
id field contains
the IANA timezone identifier string.
# Access the timezone id for use with Time.* builtins
tz = TimeZone{id: "Europe/London"}
match tz
| TimeZone{id} ->
offset = Time.utc-offset id timestamp
is-dst = Time.is-dst? id timestamp
formatted = Time.format-in timestamp id "%Y-%m-%d"
Trait Implementations
TimeZone implements the following traits:
- Show — Displays as
TimeZone(America/New_York) - Eq — Two timezones are equal if their ids match
Common Timezone Identifiers
Timezone identifiers follow the IANA Time Zone Database format:
| Region | Identifiers |
|---|---|
| Universal | "UTC", "GMT" |
| Americas | "America/New_York", "America/Chicago", "America/Denver", "America/Los_Angeles" |
| Europe | "Europe/London", "Europe/Paris", "Europe/Berlin" |
| Asia | "Asia/Tokyo", "Asia/Shanghai", "Asia/Singapore" |
| Oceania | "Australia/Sydney" |
Use Time.local-timezone to get the system's local timezone,
and Time.list-timezones to list all available timezones.
DateTimeError
Error type for date/time parsing and validation operations.
DateTimeError
type DateTimeError = InvalidFormat{...} | OutOfRange{...} | InvalidTimezone{...} | DateTimeError{...}
| Constructor | Fields | Description |
|---|---|---|
InvalidFormat |
input: String, format: String |
Input doesn't match the expected format |
OutOfRange |
field: String, value: Int, min: Int, max: Int |
A date/time field is outside valid range |
InvalidTimezone |
timezone: String |
Unknown or invalid timezone identifier |
DateTimeError |
message: String |
Generic date/time error |
Example Usage
match DateTime.parse "2024-01-15" "%Y-%m-%d"
| Ok dt -> process dt
| Err InvalidFormat{input, format} -> println "Bad format: ${input}"
| Err OutOfRange{field, value, min, max} -> println "${field} out of range"
| Err InvalidTimezone{timezone} -> println "Unknown timezone: ${timezone}"
| Err DateTimeError{message} -> println "Error: ${message}"
Trait Implementations
DateTimeError implements:
- Show — Human-readable error description
- Error — Standard error interface with
messageandkind
| Constructor | Error Kind |
|---|---|
InvalidFormat | :invalid-format |
OutOfRange | :out-of-range |
InvalidTimezone | :invalid-timezone |
DateTimeError | :datetime-error |