@std/time.ms#
Import each required symbol with import Name from "@std/time.ms".
CalendarDate Type#
A proleptic Gregorian calendar date without time of day or time zone, from 0001-01-01 through 9999-12-31.
Build a validated date with calendar_date(year, month, day), parse_date("2026-11-30"), or Date.calendar_date().
type CalendarDate {
year Int
month Int
day Int
}CalendarDate.day#
The day of the month, 1 through 31.
day IntCalendarDate.month#
The month, 1 (January) through 12.
month IntCalendarDate.year#
The year, 1 through 9999.
year IntCalendarDate.add_days#
Returns the date days days after this one (days may be negative). Returns
ERANGE if the result falls outside 0001-01-01 through 9999-12-31.
fn add_days(days Int) Result[CalendarDate, Error]CalendarDate.add_months#
Returns the date months months after this one (months may be negative), clamping
the day to the last day of the target month (e.g. Jan 31 + 1 month is Feb 28/29).
Returns ERANGE if the result falls outside years 1 through 9999.
fn add_months(months Int) Result[CalendarDate, Error]CalendarDate.add_years#
Returns the date years years after this one (years may be negative), clamping
Feb 29 to Feb 28 in non-leap years. Returns ERANGE if the result falls outside
years 1 through 9999.
fn add_years(years Int) Result[CalendarDate, Error]CalendarDate.compare#
Returns -1 when this date is earlier than other, 0 when equal, and 1 when later.
fn compare(other CalendarDate) IntCalendarDate.day_of_year#
Returns the 1-based day of the year (1 through 366).
fn day_of_year() IntCalendarDate.days_between#
Returns the signed number of calendar days from other to this date
(self - other).
fn days_between(other CalendarDate) IntCalendarDate.is_leap_year#
Reports whether this date’s year is a Gregorian leap year.
fn is_leap_year() BoolCalendarDate.to_json#
Writes the date as an ISO 8601 YYYY-MM-DD JSON string.
fn to_json() JsonValueCalendarDate.to_string#
Formats as ISO 8601 YYYY-MM-DD, such as 2026-11-30.
fn to_string() StringCalendarDate.to_timestamp#
Converts this date to midnight (00:00:00) in zone (default "UTC"), using the
same zone rules as from_calendar.
fn to_timestamp(zone String = "UTC") Result[Timestamp, Error]CalendarDate.weekday#
Returns the day of the week, 0 (Sunday) through 6 (Saturday).
fn weekday() IntDate Type#
Date and time fields of a timestamp in one time zone.
type Date {
year Int
month Int
day Int
hour Int
minute Int
second Int
nanosecond Int
weekday Int
day_of_year Int
offset_seconds Int
zone String
}Date.day#
The day of the month, starting at 1.
day IntDate.day_of_year#
The day of the year, starting at 1.
day_of_year IntDate.hour#
The hour, 0 through 23.
hour IntDate.minute#
The minute, 0 through 59.
minute IntDate.month#
The month, 1 (January) through 12.
month IntDate.nanosecond#
The nanosecond within the second, 0 through 999999999.
nanosecond IntDate.offset_seconds#
The zone’s offset from UTC in seconds at this moment; positive is east.
offset_seconds IntDate.second#
The second, 0 through 59.
second IntDate.weekday#
The day of the week, 0 (Sunday) through 6 (Saturday).
weekday IntDate.year#
The year, 1 through 9999 in UTC and nearby values in other zones.
year IntDate.zone#
The zone abbreviation at this moment, such as UTC or EST.
zone StringDate.calendar_date#
Extracts the calendar date (year, month, day) without time of day or zone.
fn calendar_date() CalendarDateDeadline Type#
A point on the monotonic clock to wait for or check against, such as the end of a request’s time budget.
type Deadline {
at Instant
}Deadline.at#
The instant the deadline expires.
at InstantDeadline.is_expired#
Reports whether the deadline has passed.
fn is_expired() BoolDeadline.remaining#
Returns the time left, or zero once the deadline has passed.
fn remaining() DurationDeadline.wait#
Suspends the current task until the deadline passes. Returns at once if it already has.
fn wait() VoidDuration Type#
A span of elapsed time, counted in signed nanoseconds.
type Duration {
nanos Int
}Duration.nanos#
The signed count of nanoseconds; nanoseconds(n) and the unit constructors build
one.
nanos IntDuration.abs#
Returns the absolute value; the minimum Duration faults.
fn abs() DurationDuration.add#
Adds signed nanoseconds; a result outside Duration’s representable range faults.
fn add(other Duration) DurationDuration.clamp#
Returns this value limited to low through high; low greater than high is
a programming fault.
fn clamp(low Duration, high Duration) DurationDuration.compare#
Returns -1 when this duration is shorter than other, 0 when equal and 1 when
longer.
fn compare(other Duration) IntDuration.divide#
Divides by a nonzero integer, truncating toward zero; zero faults.
fn divide(divisor Int) DurationDuration.max#
Returns the greater of this value and other, or this value when they compare
equal.
fn max(other Duration) DurationDuration.min#
Returns the lesser of this value and other, or this value when they compare
equal.
fn min(other Duration) DurationDuration.multiply#
Multiplies by an integer factor; overflow faults.
fn multiply(factor Int) DurationDuration.negate#
Negates this duration; the minimum Duration faults.
fn negate() DurationDuration.subtract#
Subtracts signed nanoseconds; a result outside Duration’s representable range faults.
fn subtract(other Duration) DurationDuration.to_hours#
Converts this duration to floating-point hours.
fn to_hours() FloatDuration.to_json#
Writes the signed nanoseconds as a JSON integer.
fn to_json() JsonValueDuration.to_microseconds#
Converts this duration to an integer number of microseconds.
fn to_microseconds() IntDuration.to_milliseconds#
Converts this duration to an integer number of milliseconds.
fn to_milliseconds() IntDuration.to_minutes#
Converts this duration to floating-point minutes.
fn to_minutes() FloatDuration.to_nanoseconds#
Converts this duration to an integer number of nanoseconds.
fn to_nanoseconds() IntDuration.to_seconds#
Converts this duration to floating-point seconds.
fn to_seconds() FloatDuration.to_string#
Returns human text such as 1m30.5s, 250ms, 1.5µs, -2h0m0s, or 0s; string
interpolation uses it too. A larger unit is followed by every smaller whole
unit down to seconds, so two minutes is 2m0s and an exact hour is 1h0m0s.
fn to_string() StringInstant Type#
An instant on a monotonic clock; it is not a calendar timestamp.
type Instant {
domain Int
tick Int
}Instant.domain#
The clock the reading belongs to; instants from different domains do not compare.
domain IntInstant.tick#
Nanoseconds on that clock.
tick IntInstant.clamp#
Returns this value limited to low through high; low greater than high is
a programming fault.
fn clamp(low Instant, high Instant) InstantInstant.compare#
Returns -1 when this instant is earlier than other, 0 when equal and 1 when
later. Instants from different clock domains fault.
fn compare(other Instant) IntInstant.difference#
Returns this instant minus other: the time elapsed since other, negative when
other is later.
fn difference(other Instant) DurationInstant.max#
Returns the greater of this value and other, or this value when they compare
equal.
fn max(other Instant) InstantInstant.min#
Returns the lesser of this value and other, or this value when they compare
equal.
fn min(other Instant) InstantInstant.shift#
Shifts this monotonic instant by a duration.
fn shift(duration Duration) InstantTimestamp Type#
A wall-clock time with Unix seconds and nanoseconds.
type Timestamp {
seconds Int
nanos Int
}Timestamp.nanos#
The nanosecond within the second, 0 through 999999999.
nanos IntTimestamp.seconds#
Whole seconds since the Unix epoch.
seconds IntTimestamp.calendar#
Breaks this timestamp into calendar fields in a time zone: "UTC", "Local", an
IANA name such as "America/New_York", or a fixed offset such as "+05:30". An
unknown zone returns EINVAL.
fn calendar(zone String) Result[Date, Error]Timestamp.clamp#
Returns this value limited to low through high; low greater than high is
a programming fault.
fn clamp(low Timestamp, high Timestamp) TimestampTimestamp.compare#
Returns -1 when this timestamp is earlier than other, 0 when equal and 1 when
later, across the full calendar range.
fn compare(other Timestamp) IntTimestamp.difference#
Returns this timestamp minus other as a duration. A result that does not fit a
Duration returns ERANGE.
fn difference(other Timestamp) Result[Duration, Error]Timestamp.format#
Formats this timestamp in a zone with a token pattern.
The tokens are YYYY, MM, DD, HH, mm, ss, SSS (milliseconds), ZZ (offset such as +05:30) and zone (abbreviation). Other text is copied.
Tokens are replaced wherever they appear, even inside words ("Dummy" becomes "Du13y"), and there is no escape.
To include literal letters that spell a token, format the fields separately and join the pieces yourself.
An unknown zone returns EINVAL.
fn format(pattern String, zone String) Result[String, Error]Timestamp.max#
Returns the greater of this value and other, or this value when they compare
equal.
fn max(other Timestamp) TimestampTimestamp.min#
Returns the lesser of this value and other, or this value when they compare
equal.
fn min(other Timestamp) TimestampTimestamp.nanoseconds#
Returns this timestamp’s nanosecond component.
fn nanoseconds() IntTimestamp.shift#
Adds a duration to this wall-clock timestamp. A result outside years 1 through
9999 returns ERANGE.
fn shift(duration Duration) Result[Timestamp, Error]Timestamp.to_json#
Writes the RFC 3339 text as a JSON string.
fn to_json() JsonValueTimestamp.to_string#
Formats this timestamp in RFC 3339 form.
fn to_string() StringTimestamp.unix_seconds#
Returns the whole Unix seconds represented by this timestamp.
fn unix_seconds() Intcalendar_date Function#
Validates and constructs a Gregorian CalendarDate for years 1 through 9999.
Invalid dates (such as February 29 in a non-leap year) return EINVAL.
fn calendar_date(year Int, month Int, day Int) Result[CalendarDate, Error] { ... }deadline_after Function#
Returns a deadline duration from now.
fn deadline_after(duration Duration) Deadline { ... }from_calendar Function#
Builds a timestamp from calendar fields interpreted in a time zone (see Timestamp.calendar for zone names).
Fields outside their calendar range, such as month 13 or February 30, return ERANGE; an unknown zone returns EINVAL.
A local time skipped or repeated by a daylight-saving change resolves to the earlier valid offset.
fn from_calendar(year Int, month Int, day Int, hour Int, minute Int, second Int, nanosecond Int, zone String) Result[Timestamp, Error] { ... }hours Function#
Constructs a duration of n hours; n may be negative, and a nanosecond count
outside Int faults like any Int multiplication.
fn hours(n Int) Duration { ... }local_zone Function#
Returns the host’s local time zone name, or UTC when it cannot be determined.
fn local_zone() String { ... }microseconds Function#
Constructs a duration of n microseconds; n may be negative, and a nanosecond
count outside Int faults like any Int multiplication.
fn microseconds(n Int) Duration { ... }milliseconds Function#
Constructs a duration of n milliseconds; n may be negative, and a nanosecond
count outside Int faults like any Int multiplication.
fn milliseconds(n Int) Duration { ... }minutes Function#
Constructs a duration of n minutes; n may be negative, and a nanosecond count
outside Int faults like any Int multiplication. For example,
monotonic().difference(start).compare(minutes(5)) > 0 tests whether more than
five minutes have elapsed.
fn minutes(n Int) Duration { ... }monotonic Function#
Returns the current reading of the monotonic clock, for measuring elapsed time. Readings are only comparable with others from this clock and do not correspond to a calendar date.
fn monotonic() Instant { ... }nanoseconds Function#
Constructs a duration of n nanoseconds, the unit Duration counts in; n may be
negative.
fn nanoseconds(n Int) Duration { ... }now Function#
Returns the current calendar time as a UTC Timestamp with nanosecond precision.
It can jump when the system clock is adjusted, so use monotonic for elapsed time.
fn now() Timestamp { ... }parse_date Function#
Parses a strict ISO 8601 YYYY-MM-DD date string. Malformed text or an invalid
calendar date returns EBADMSG.
fn parse_date(text String) Result[CalendarDate, Error] { ... }parse_duration Function#
Parses a span such as 1h30m, -250ms or 1.5µs, including every Duration.to_string() result.
Accepts one optional leading + or -, then decimal components with the units ns, us, µs, μs, ms, s, m or h.
Components may repeat or appear in any order; .5s and 1.s are valid. A bare 0 is also valid, including +0 and -0.
Fractions may have any number of ASCII digits. Each component truncates toward zero to whole nanoseconds, without floating-point rounding, and negative zero becomes zero.
Whitespace, exponents, missing units and other syntax return EBADMSG, even after an overflowing prefix.
A well-formed span outside -9223372036854775808 through 9223372036854775807 nanoseconds returns ERANGE.
fn parse_duration(text String) Result[Duration, Error] { ... }parse_timestamp Function#
Parses an RFC 3339 timestamp with an explicit time zone.
fn parse_timestamp(text String) Result[Timestamp, Error] { ... }seconds Function#
Constructs a duration of n seconds, as in sleep(seconds(2)); n may be negative,
and a nanosecond count outside Int faults like any Int multiplication.
fn seconds(n Int) Duration { ... }sleep Function#
Starts a timer for the requested duration and returns its promise; wait() it to
pause the current task for at least that long. The timer has no task of its own, and
dropping the promise cancels it.
fn sleep(duration Duration) Promise[Void] { ... }ticker Function#
Returns a ticker whose first tick comes one period from now. A period of zero or
less is a programming fault.
fn ticker(period Duration) ReadStream[Instant] { ... }timestamp Function#
Constructs a timestamp from Unix seconds and the nanosecond within that second.
nanoseconds must be 0 through 999999999, and seconds from -62135596800 (year 1)
through 253402300799 (year 9999); anything else returns ERANGE. It does not carry
extra nanoseconds into the seconds.
fn timestamp(seconds Int, nanoseconds Int) Result[Timestamp, Error] { ... }