std

module std::timesource

Time: a monotonic clock for measuring, the wall clock, sleeping, and the calendar: days of the proleptic Gregorian calendar, times of day and the system's offset from UTC; time zones by name from the platform's database, dates and times on a zone's clocks, and ISO 8601, RFC 3339 and HTTP dates (F12e, notes at the end).

The externs are the runtime's (alloyc/src/support/system.c); the interpreter reads the same clocks, and on the web the page's time zone comes through std::web.

const day = Date::of(2026, Month::October, 15);
expect(day.weekday() == Weekday::Thursday);
expect_equal(&f"{day.add_days(17)}", &String::from(&"2026-11-01"));
expect(Date::of(2026, Month::January, 31).add_months(1) == Date::of(2026, Month::February, 28));

type Durationsource

exp type Duration : Format = struct

A span of time, in nanoseconds.

Fields

exp nanoseconds: u64

Functions

fn Duration::from_nanosecondssource

exp fn Duration::from_nanoseconds(nanoseconds: u64) -> Duration

fn Duration::from_microsecondssource

exp fn Duration::from_microseconds(microseconds: u64) -> Duration

fn Duration::from_millisecondssource

exp fn Duration::from_milliseconds(milliseconds: u64) -> Duration

fn Duration::from_secondssource

exp fn Duration::from_seconds(seconds: f64) -> Duration

The seconds rounded to the nearest nanosecond; a negative or NaN count is zero.

fn secondssource

exp fn seconds(self d: &Duration) -> f64

fn millisecondssource

exp fn milliseconds(self d: &Duration) -> f64

fn formatsource

exp fn format(self d: &Duration, out: &var String)

The duration in seconds: 0.016667s.

type Instantsource

exp type Instant = struct

A moment on the monotonic clock, which never goes back: for frame times and measurements.

Functions

fn Instant::nowsource

exp fn Instant::now() -> Instant

fn elapsedsource

exp fn elapsed(self start: &Instant) -> Duration

The time since this moment.

fn sincesource

exp fn since(self later: &Instant, earlier: &Instant) -> Duration

The time from the earlier moment to this one; zero when it is later.

fn wall_clocksource

exp fn wall_clock() -> i64

Nanoseconds since 1970-01-01 UTC on the wall clock, which may jump when the system's time is set.

fn sleepsource

exp fn sleep(duration: Duration)

Waits at least the duration.

fn sleep_untilsource

exp fn sleep_until(moment: i64)

Waits until the wall clock reaches the moment (nanoseconds since 1970 UTC, as wall_clock gives), looking at it again at least every 5 seconds, so a moment passed while the machine slept or the system's time was set ends the wait at most that late.

const moment = wall_clock() + 20000000; // 20 ms from now
sleep_until(moment);
expect(wall_clock() >= moment);

type Monthsource

exp type Month = enum : u8

Variants

January = 1
February = 2
March = 3
April = 4
May = 5
June = 6
July = 7
August = 8
September = 9
October = 10
November = 11
December = 12

type Weekdaysource

exp type Weekday = enum : u8

The days of the week in ISO 8601's order and numbers, Monday first.

Variants

Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7

Functions

fn addsource

exp fn add(self w: &Weekday, days: i64) -> Weekday

The day days after this one, going round the week.

type Datesource

exp type Date : Format = struct

A day of the proleptic Gregorian calendar, the one in use today taken back before 1582; year 0 is 1 BC.

Fields

exp year: i32
exp month: Month
exp day: u8

Functions

fn Date::ofsource

exp fn Date::of(year: i32, month: Month, day: u8) -> Date

The day; faults on a day the month does not have, such as 30 February.

fn Date::try_ofsource

exp fn Date::try_of(year: i32, month: Month, day: u8) -> Option<Date>

The day, or None when the month does not have it.

fn Date::from_dayssource

exp fn Date::from_days(days: i64) -> Date

The day days after 1970-01-01, before it when negative (Howard Hinnant's civil_from_days).

fn Date::todaysource

exp fn Date::today() -> Date

Today in the system's time zone.

fn dayssource

exp fn days(self d: &Date) -> i64

The days since 1970-01-01, negative before it (Howard Hinnant's days_from_civil).

fn weekdaysource

exp fn weekday(self d: &Date) -> Weekday

fn add_dayssource

exp fn add_days(self d: &Date, days: i64) -> Date

fn add_monthssource

exp fn add_months(self d: &Date, months: i64) -> Date

The same day months later, or earlier when negative, kept within the month: 31 January and a month is the last of February.

fn comparesource

exp fn compare(self d: &Date, other: &Date) -> Ordering

fn beforesource

exp fn before(self d: &Date, other: &Date) -> bool

Whether the day comes before the other.

fn aftersource

exp fn after(self d: &Date, other: &Date) -> bool

Whether the day comes after the other.

fn is_leap_yearsource

exp fn is_leap_year(year: i32) -> bool

fn days_in_monthsource

exp fn days_in_month(year: i32, month: Month) -> u8

fn formatsource

exp fn format(self d: &Date, out: &var String)

The day as ISO 8601 writes it: 2026-10-15, -0001-03-01, +10000-01-01.

type TimeOfDaysource

exp type TimeOfDay : Format = struct

A time of day, to the second.

Fields

exp hour: u8
exp minute: u8
exp second: u8 = 0

Functions

fn TimeOfDay::ofsource

exp fn TimeOfDay::of(hour: u8, minute: u8) -> TimeOfDay

The time; faults past 23:59.

fn secondssource

exp fn seconds(self t: &TimeOfDay) -> u32

The seconds since midnight.

fn comparesource

exp fn compare(self t: &TimeOfDay, other: &TimeOfDay) -> Ordering

fn formatsource

exp fn format(self t: &TimeOfDay, out: &var String)

The time on a 24-hour clock: 14:30, or 14:30:15 with seconds.

fn local_offsetsource

exp fn local_offset(at: i64) -> i64

Seconds east of UTC in the system's time zone at the moment, in nanoseconds since 1970-01-01 UTC as wall_clock gives them: 7200 in Paris in summer, -18000 in New York in winter, 0 where the system has no time zone (WASI).

type DateTimesource

exp type DateTime : Format = struct

A date and a time of day, on no zone's clocks in particular.

Fields

exp date: Date
exp time: TimeOfDay
exp nanosecond: u32 = 0

Functions

fn comparesource

exp fn compare(self t: &DateTime, other: &DateTime) -> Ordering

fn formatsource

exp fn format(self t: &DateTime, out: &var String)

The date and time as ISO 8601 writes them: 2026-11-20T19:30, 2026-11-20T19:30:05.25.

type Resolvedsource

exp type Resolved = enum

A local time may be skipped by a change to summer time, or happen twice.

Variants

Exact: i64
Skipped: i64

the moment the clocks jumped over it: 02:30 on a spring night is 03:00

Twice: [i64 : 2]

the earlier and the later

type Zonesource

exp type Zone = struct

A time zone: its name and the offsets from UTC its clocks keep, from the platform's database (Zone::named) or a TZif file the program carries (Zone::from_tzif).

const utc = Zone::utc();
const moment: i64 = 1795199400000000000; // 2026-11-20 18:30 UTC
expect_equal(&f"{utc.local(moment)}", &String::from(&"2026-11-20T18:30"));
expect(utc.moment(&utc.local(moment)) == Resolved::Exact(moment));

Functions

fn Zone::utcsource

exp fn Zone::utc() -> Zone

fn Zone::namedsource

exp fn Zone::named(name: &[u8]) -> Option<Zone>

The zone the platform's database knows by the name ("Europe/Paris"): TZif files in /usr/share/zoneinfo on Linux and macOS, ICU's in icu.dll on Windows (10 1903 and later), the page's Intl on the web. UTC is known everywhere, WASI knowing nothing else.

fn Zone::systemsource

exp fn Zone::system() -> Zone

The system's zone, as local_offset follows it: TZ (a name, a path or a POSIX rule), else /etc/localtime and the name its link gives (Linux, macOS); Windows' zone by its IANA name; the page's on the web; UTC when there is none, as under WASI.

fn Zone::from_tzifsource

exp fn Zone::from_tzif(name: &[u8], bytes: &[u8]) -> Option<Zone>

The zone a TZif file describes (RFC 8536, versions 1 to 4), named as given: a zone the program carries (#read_file), the only kind WASI has. Leap seconds are not counted.

fn namesource

exp fn name(self zone: &Zone) -> &[u8]

fn offset_atsource

exp fn offset_at(
    self zone: &Zone,
    moment: i64
) -> i64

Seconds east of UTC on the zone's clocks at the moment (nanoseconds since 1970 UTC, as wall_clock gives).

fn localsource

exp fn local(self zone: &Zone, moment: i64) -> DateTime

The moment on the zone's clocks.

fn momentsource

exp fn moment(self zone: &Zone, local: &DateTime) -> Resolved

The moment the zone's clocks show the local time: exact, skipped by a change of offset (the moment of the change), or shown twice.

fn Date::parse_isosource

exp fn Date::parse_iso(text: &[u8]) -> Option<Date>

The day 2026-11-20 names, with a sign and five digits or more for years past 9999 or before 0 as format writes them (-0001-03-01).

expect(Date::parse_iso(&"2026-11-20") == ::Some(Date::of(2026, Month::November, 20)));
expect(Date::parse_iso(&"2026-02-30") is ::None);

fn DateTime::parse_isosource

exp fn DateTime::parse_iso(text: &[u8]) -> Option<DateTime>

The date and time 2026-11-20T19:30 names, seconds and their fraction optional (19:30:05.25), with no zone.

fn Duration::parse_isosource

exp fn Duration::parse_iso(text: &[u8]) -> Option<Duration>

The duration ISO 8601 writes PT1H30M: weeks (P2W), or days and a time of hours, minutes and seconds, the last of them with a fraction (PT0.5S). Years and months, which have no fixed length, are refused.

expect(Duration::parse_iso(&"PT1H30M") == ::Some(Duration::from_seconds(5400.0)));
expect(Duration::parse_iso(&"P1DT0.25S") == ::Some(Duration::from_seconds(86400.25)));
expect(Duration::parse_iso(&"P1M") is ::None);

fn parse_rfc3339source

exp fn parse_rfc3339(text: &[u8]) -> Option<i64>

The moment an RFC 3339 time names: 2026-11-20T19:30:00+01:00, 2026-11-20T18:30:00.5Z. A leap second (23:59:60) is the second after it.

const moment: i64 = 1795199400000000000;
expect(parse_rfc3339(&"2026-11-20T19:30:00+01:00") == ::Some(moment));
var text = String::empty();
format_rfc3339(moment, 3600, &var text);
expect_equal(&text, &String::from(&"2026-11-20T19:30:00+01:00"));

fn format_rfc3339source

exp fn format_rfc3339(moment: i64, offset: i64, out: &var String)

Writes the moment as RFC 3339 does on clocks at the offset (seconds east, whole minutes of it): 2026-11-20T19:30:00+01:00, Z for UTC, a fraction only when there is one.

fn parse_http_datesource

exp fn parse_http_date(text: &[u8]) -> Option<i64>

The moment an HTTP date names (RFC 9110, 5.6.7), as Retry-After, Last-Modified and Expires carry them: `Fri, 20 Nov 2026 18:30:00 GMT, or the obsolete Friday, 20-Nov-26 18:30:00 GMT and Fri Nov 20 18:30:00 2026`.

const moment: i64 = 1795199400000000000;
expect(parse_http_date(&"Fri, 20 Nov 2026 18:30:00 GMT") == ::Some(moment));
expect(parse_http_date(&"Fri Nov 20 18:30:00 2026") == ::Some(moment));