March Docs

DateTime

DateTime module: date/time types and arithmetic.

Types: Date(year, month, day) -- Gregorian calendar, month 1-12, day 1-31 Time(hour, min, sec) -- 24-hour clock, all zero-based except day DateTime(date, time) -- combined date and time

Timestamps are Unix timestamps: integer seconds since 1970-01-01 00:00:00 UTC. now() returns the current time as a Unix timestamp (Int).

Calendar arithmetic assumes the proleptic Gregorian calendar throughout. Day-of-week encoding: 0 = Sunday, 1 = Monday, ..., 6 = Saturday.

Types

typeDateDate = Date(Int, Int, Int) -- year, month (1-12), day (1-31)#
typeTimeTime = Time(Int, Int, Int) -- hour (0-23), min (0-59), sec (0-59)#
typeDateTimeDateTime = DateTime(Date, Time)#
typeTzTz = Tz(String, Int)#

Functions

fnaddadd(dt : DateTime, dur : Int) : DateTime#

Add a Duration (stored in milliseconds) to a DateTime.

fnadd_daysadd_days(dt, n)#

Add n days to a DateTime. n may be negative.

fnadd_hoursadd_hours(dt, n)#

Add n hours to a DateTime. n may be negative.

fnadd_msadd_ms(dt : DateTime, n : Int) : DateTime#

Add n milliseconds to a DateTime. n may be negative.

fnadd_secondsadd_seconds(dt, n)#

Add n seconds to a DateTime. n may be negative.

fncomparecompare(a, b)#

Compare two DateTimes. Returns -1 if a < b, 0 if a == b, 1 if a > b.

fnday_of_weekday_of_week(dt)#

Return the day of the week for the date component of a DateTime. 0 = Sunday, 1 = Monday, ..., 6 = Saturday. (January 1, 1970 was a Thursday = 4.)

fndiff_secondsdiff_seconds(a, b)#

Difference between two DateTimes in seconds. diff_seconds(a, b) = to_timestamp(a) - to_timestamp(b).

fnfixed_zonefixed_zone(name : String, offset_seconds : {Int | _ >= -50400 && _ <= 50400}) : Tz#

Build a fixed-offset zone from an explicit label and second offset. offset_seconds is positive east of UTC, negative west. Panics if the offset is outside ±14 hours (the widest real-world legal range).

fnfixed_zone_hmfixed_zone_hm(hours : Int, minutes : {Int | _ >= 0 && _ < 60}) : Tz#

Convenience constructor: fixed offset as (hours, minutes) with a single sign applied by hours (per ISO convention). For UTC+05:30 pass (5, 30); for UTC−03:30 pass (-3, 30) (minutes always positive, sign comes from hours). Name is auto-derived.

fnfixed_zone_hoursfixed_zone_hours(hours : Int) : Tz#

Convenience constructor: fixed offset in whole hours. The name is auto-derived in ISO 8601 form (e.g. "+05:00", "-08:00", "Z"). For custom labels use fixed_zone(name, seconds).

fnformatformat(dt, fmt)#

Format a DateTime as a string. Supported specifiers: %Y %m %d %H %M %S %%

Example: DateTime.format(dt, "%Y-%m-%d %H:%M:%S")
fnformat_offsetformat_offset(tz : Tz) : String#

Format a zone's offset as ISO 8601: \"Z\" for UTC, \"+HH:MM\" or \"-HH:MM\" otherwise. Seconds-of-offset are ignored (no real zone uses sub-minute offsets since 1972).

fnfrom_timestampfrom_timestamp(ts)#

Convert a Unix timestamp (Int seconds) to a DateTime.

    DateTime.from_timestamp(0)     -- DateTime(Date(1970, 1, 1), Time(0, 0, 0))
    DateTime.from_timestamp(86400) -- DateTime(Date(1970, 1, 2), Time(0, 0, 0))
fnlocal_datetimelocal_datetime(ldt : LocalDateTime) : DateTime#

Return the civil (wall-clock) DateTime component.

fnlocal_formatlocal_format(ldt : LocalDateTime, fmt : String) : String#

Format a LocalDateTime. All DateTime.format specifiers are supported (%Y %m %d %H %M %S %%) plus: %z — offset without colon, e.g. +0530, -0800, +0000 %:z — ISO 8601 offset, e.g. +05:30, -08:00, Z for UTC %Z — zone name (whatever was passed to the Tz constructor)

fnlocal_from_timestamplocal_from_timestamp(ts : Int, tz : Tz) : LocalDateTime#

Build a LocalDateTime from a UTC instant expressed as a Unix timestamp (Int seconds).

fnlocal_from_utclocal_from_utc(utc_dt : DateTime, tz : Tz) : LocalDateTime#

Reinterpret a UTC DateTime as the same instant in zone tz.

fnlocal_nowlocal_now(tz : Tz) : LocalDateTime#

Return the current wall-clock time in zone tz.

fnlocal_oflocal_of(dt : DateTime, tz : Tz) : LocalDateTime#

Pair a civil DateTime (interpreted as the wall-clock time in tz) with its zone. No conversion happens — dt is used as-is. Use local_from_utc if you start from a UTC instant.

fnlocal_to_timestamplocal_to_timestamp(ldt : LocalDateTime) : Int#

Unix timestamp (UTC seconds) of the instant this LocalDateTime denotes.

fnlocal_to_utclocal_to_utc(ldt : LocalDateTime) : DateTime#

Convert a LocalDateTime to the corresponding UTC DateTime. Subtracts the zone's offset from the civil time.

fnlocal_with_zonelocal_with_zone(ldt : LocalDateTime, new_tz : Tz) : LocalDateTime#

Return the same instant rendered in a different zone. local_with_zone(ldt, z) satisfies local_to_timestamp(result) == local_to_timestamp(ldt) (same instant, different wall-clock).

fnlocal_zonelocal_zone(ldt : LocalDateTime) : Tz#

Return the zone of a LocalDateTime.

fnnownow()#

Return the current Unix timestamp as an Int (seconds since 1970-01-01 UTC).

fnparseparse(s)#

Parse a UTC DateTime from a string. Accepts "YYYY-MM-DD" and "YYYY-MM-DD HH:MM:SS". Returns Ok(DateTime) or Err(message) — rejects out-of-range fields (month 13, day 32, hour 25, …) rather than constructing a garbage calendar value.

This entry point is timezone-naive: the returned `DateTime` always
represents UTC.  For ISO 8601 strings with an explicit offset
(`Z`, `+HH:MM`, `-HHMM`, …) use `parse_offset` to get a
`LocalDateTime` whose zone is recovered from the suffix.
fnparse_offsetparse_offset(s : String) : Result(LocalDateTime, String)#

Parse an ISO 8601 datetime with explicit offset, e.g. "2024-01-15T10:30:45Z" or "2024-01-15 10:30:45+05:30". Returns Ok(LocalDateTime) or Err(msg).

Bare UTC instants (no offset suffix) parse as UTC.  The `T`/space
separator is required.  Offsets may use `:` (ISO form) or the compact
`+HHMM` form.  Civil fields are range-validated.
fnto_timestampto_timestamp(dt)#

Convert a DateTime to a Unix timestamp (Int seconds since 1970-01-01 UTC).

    DateTime.to_timestamp(DateTime(Date(1970, 1, 1), Time(0, 0, 0))) -- 0
fnutc_zoneutc_zone() : Tz#

The UTC zone (offset 0, name \

fnzone_namezone_name(tz : Tz) : String#

Return the zone's human label (e.g. \

fnzone_offset_secondszone_offset_seconds(tz : Tz) : Int#

Return the zone's offset from UTC in seconds (east is positive).