Time
Source: lib/time.x
Guide: Time, Random Values, and Encodings
Clocks, durations, and calendar time.
Include it with #include "time.x".
Primary API
| Function | Summary |
|---|---|
DateTime.str | Returns d as an RFC 3339 timestamp with its offset, or Z for UTC, as in 2026-10-28T10:05:09-04:00. |
DateTime.time | Returns the instant that the fields of d name at its utc_offset; a DateTime converts to a Time this way wherever one is expected. |
DateTime.weekday | Returns the day of the week of d, from 0 for Sunday to 6 for Saturday, as strftime’s %w numbers it. |
Duration.add | Returns the sum of two Durations; a + b calls it. |
Duration.compare | Orders two Durations: negative, zero, or positive as a is shorter, equal, or longer. |
Duration.microseconds | Returns count microseconds as a Duration. |
Duration.milliseconds | Returns count milliseconds as a Duration. |
Duration.neg | Returns d with its sign reversed; -d calls it. |
Duration.scale | Returns d multiplied by factor, rounded to the nearest nanosecond. |
Duration.seconds | Returns seconds as a Duration, rounded to the nearest nanosecond. |
Duration.str | Returns d in the units Go prints: 1h2m3.5s, 1m30s, 2.5ms, 750us, 12ns, or 0s. |
Duration.sub | Returns a less b; a - b calls it. |
Duration.total_milliseconds | Returns d in whole milliseconds, truncated toward zero. |
Duration.total_seconds | Returns d in seconds, with its fraction. |
Time.compare | Orders two instants: negative, zero, or positive as a is earlier, the same, or later. |
Time.format | Formats t in UTC with the strftime conversions in pattern, as t.format("%a, %d %b %Y %H:%M:%S GMT") writes an HTTP date. |
Time.format_local | Formats t in the C library’s local zone, which the TZ environment variable selects, with the strftime conversions in pattern. |
Time.from_unix | Returns the instant seconds after the Unix epoch, the count that Path.modified_time and C’s time report. |
Time.local | Returns the calendar fields of t in the C library’s local zone, which the TZ environment variable selects, with that zone’s offset at t. |
Time.monotonic | Returns the monotonic clock’s reading as the Duration since an unspecified starting point. |
Time.now | Returns the current wall-clock time from CLOCK_REALTIME. |
Time.parse | Returns the instant an RFC 3339 timestamp names, such as 2026-10-28T14:05:09Z or 2026-10-28 10:05:09.250-04:00. |
Time.plus | Returns the instant d after t; a negative d moves earlier. |
Time.since | Returns the Duration from earlier to t, negative when earlier is the later instant. |
Time.sleep | Suspends the calling thread for at least d, resuming after an interrupting signal until the whole span has passed. |
Time.str | Returns t as an RFC 3339 timestamp in UTC, such as 2026-10-28T14:05:09.25Z. |
Time.unix_seconds | Returns t in seconds since the Unix epoch, with its fraction. |
Time.utc | Returns the UTC calendar fields of t. |
DateTime
DateTime.str
String DateTime.str(DateTime d)
Returns d as an RFC 3339 timestamp with its offset, or Z for UTC,
as in 2026-10-28T10:05:09-04:00.
Source: lib/time.x
DateTime.time
Time DateTime.time(DateTime d)
Returns the instant that the fields of d name at its utc_offset;
a DateTime converts to a Time this way wherever one is expected. The
fields need not be normalized: day 32 of January is February 1.
Raises: <conv-range> when the instant is outside the Time range.
Source: lib/time.x
DateTime.weekday
int DateTime.weekday(DateTime d)
Returns the day of the week of d, from 0 for Sunday to 6 for
Saturday, as strftime’s %w numbers it.
Source: lib/time.x
Duration
Duration.add
Duration Duration.add(Duration a, Duration b)
Returns the sum of two Durations; a + b calls it.
Raises: <conv-range> when the sum leaves the 64-bit range.
Source: lib/time.x
Duration.compare
int Duration.compare(Duration a, Duration b)
Orders two Durations: negative, zero, or positive as a is shorter,
equal, or longer. <, <=, >, and >= call it.
Source: lib/time.x
Duration.microseconds
Duration Duration.microseconds(long long count)
Returns count microseconds as a Duration.
Raises: <conv-range> when the Duration cannot represent it.
Source: lib/time.x
Duration.milliseconds
Duration Duration.milliseconds(long long count)
Returns count milliseconds as a Duration.
Raises: <conv-range> when the Duration cannot represent it.
Source: lib/time.x
Duration.neg
Duration Duration.neg(Duration d)
Returns d with its sign reversed; -d calls it.
Raises: <conv-range> for the most negative Duration.
Source: lib/time.x
Duration.scale
Duration Duration.scale(Duration d, double factor)
Returns d multiplied by factor, rounded to the nearest nanosecond.
Raises: <conv-range> when the product is not finite or leaves the
range.
Source: lib/time.x
Duration.seconds
Duration Duration.seconds(double seconds)
Returns seconds as a Duration, rounded to the nearest nanosecond.
Raises: <conv-range> when seconds is not finite or the Duration
cannot represent it.
Source: lib/time.x
Duration.str
String Duration.str(Duration d)
Returns d in the units Go prints: 1h2m3.5s, 1m30s, 2.5ms,
750us, 12ns, or 0s. The fraction keeps every significant digit
and no trailing zero. Interpolation prints a Duration this way.
Source: lib/time.x
Duration.sub
Duration Duration.sub(Duration a, Duration b)
Returns a less b; a - b calls it.
Raises: <conv-range> when the difference leaves the 64-bit range.
Source: lib/time.x
Duration.total_milliseconds
long long Duration.total_milliseconds(Duration d)
Returns d in whole milliseconds, truncated toward zero.
Source: lib/time.x
Duration.total_seconds
double Duration.total_seconds(Duration d)
Returns d in seconds, with its fraction.
Source: lib/time.x
Time
Time.compare
int Time.compare(Time a, Time b)
Orders two instants: negative, zero, or positive as a is earlier,
the same, or later. <, <=, >, and >= call it.
Source: lib/time.x
Time.format
String Time.format(Time t, String pattern)
Formats t in UTC with the strftime conversions in pattern, as
t.format("%a, %d %b %Y %H:%M:%S GMT") writes an HTTP date. A pattern
whose output is empty returns "".
Source: lib/time.x
Time.format_local
String Time.format_local(Time t, String pattern)
Formats t in the C library’s local zone, which the TZ environment
variable selects, with the strftime conversions in pattern.
Source: lib/time.x
Time.from_unix
Time Time.from_unix(double seconds)
Returns the instant seconds after the Unix epoch, the count that
Path.modified_time and C’s time report.
Raises: <conv-range> when seconds is not finite or out of range.
Source: lib/time.x
Time.local
DateTime Time.local(Time t)
Returns the calendar fields of t in the C library’s local zone, which
the TZ environment variable selects, with that zone’s offset at t.
Source: lib/time.x
Time.monotonic
Duration Time.monotonic(void)
Returns the monotonic clock’s reading as the Duration since an unspecified starting point. The reading never decreases, so the difference of two readings measures elapsed time.
#include "time.x"
int main(void) {
Duration start = Time.monotonic();
Time.sleep(Duration.milliseconds(5));
Duration elapsed = Time.monotonic() - start;
return elapsed >= Duration.milliseconds(5) ? 0 : 1;
}
Source: lib/time.x
Time.now
Time Time.now(void)
Returns the current wall-clock time from CLOCK_REALTIME. The clock can
jump when the system time is set; measure intervals with
Time.monotonic.
Source: lib/time.x
Time.parse
Time Time.parse(String text)
Returns the instant an RFC 3339 timestamp names, such as
2026-10-28T14:05:09Z or 2026-10-28 10:05:09.250-04:00. The date and
time may be separated by T, t, or a space, and the offset is Z,
z, +hh:mm, or -hh:mm. Digits finer than a nanosecond are dropped.
POSIX time has no leap seconds, so a second of 60 is out of range.
Raises: <malformed> with reason and byte offset when text is not
such a timestamp or names a day its month does not have, and
<conv-range> when the instant is outside the Time range.
Source: lib/time.x
Time.plus
Time Time.plus(Time t, Duration d)
Returns the instant d after t; a negative d moves earlier.
Raises: <conv-range> when the result leaves the 64-bit range.
Source: lib/time.x
Time.since
Duration Time.since(Time t, Time earlier)
Returns the Duration from earlier to t, negative when earlier is
the later instant.
Raises: <conv-range> when the span leaves the 64-bit range.
Source: lib/time.x
Time.sleep
void Time.sleep(Duration d)
Suspends the calling thread for at least d, resuming after an
interrupting signal until the whole span has passed. A Duration that is
not positive returns at once.
Source: lib/time.x
Time.str
String Time.str(Time t)
Returns t as an RFC 3339 timestamp in UTC, such as
2026-10-28T14:05:09.25Z. The fraction keeps every significant digit
and is omitted when zero. Interpolation prints a Time this way.
Source: lib/time.x
Time.unix_seconds
double Time.unix_seconds(Time t)
Returns t in seconds since the Unix epoch, with its fraction.
Source: lib/time.x
Time.utc
DateTime Time.utc(Time t)
Returns the UTC calendar fields of t.
Source: lib/time.x
Public types
| Type | Kind | Summary |
|---|---|---|
DateTime | class | The calendar fields of an instant in one zone. |
Duration | class | A signed span of time in whole nanoseconds. |
Time | class | An instant, in nanoseconds since 1970-01-01T00:00:00Z without leap seconds, as POSIX counts time. |
DateTime
class DateTime { int year, month, day, hour, minute, second, nanosecond, utc_offset; }
The calendar fields of an instant in one zone. month and day count
from 1, and utc_offset is the zone’s offset east of UTC in seconds.
Time.utc and Time.local fill every field.
Source: lib/time.x
Duration
class Duration { long long nanoseconds; }
A signed span of time in whole nanoseconds. +, -, unary -, and
the comparison operators work on two Durations; arithmetic that leaves
the 64-bit range raises <conv-range>.
Source: lib/time.x
Time
class Time { long long nanoseconds; }
An instant, in nanoseconds since 1970-01-01T00:00:00Z without leap seconds, as POSIX counts time. The comparison operators order two Times.
Source: lib/time.x
Design notes
A Time is an instant counted in nanoseconds from the Unix epoch, and a
Duration is a signed count of nanoseconds; each holds one 64-bit
count, so each spans about 292 years either side of zero. Calendar
fields come from gmtime_r for UTC and localtime_r for the C
library’s local zone; there is no time zone database. Formatting
defaults to UTC, and strftime names follow the C locale.