time
import time from "time";
import io from "io";
let start: time.Instant = time.now();
do_work();
io.println("took {start.elapsed().as_millis()} ms");
let today: time.DateTime = time.utc_now();
io.println(today.to_iso8601()); // 2026-10-02T14:03:00.250Ztime has two clocks. The monotonic clock (Instant) is for measuring
and never goes backwards. The wall clock (unix_nanos, DateTime) is
the date and time in UTC; it can jump when the clock is set. A Zoned
pairs an instant with a zone’s offset: the user’s own zone, or the zone a
mail was sent from.
Durations
Duration is a span of time in nanoseconds, made with:
| Function | |
|---|---|
nanos(n), micros(n), millis(n), seconds(n) | A Duration of n units |
| Method | Returns |
|---|---|
d.as_nanos(), d.as_micros(), d.as_millis(), d.as_secs() | The length in that unit, truncated |
d.as_secs_f64() | Seconds with the fraction |
d.add(other), d.sub(other) | A new Duration |
Measuring and waiting
| Function | Does |
|---|---|
now() -> Instant | Read the monotonic clock |
i.elapsed() -> Duration | Time since i |
i.since(earlier) -> Duration | Time from earlier to i |
i.add(d) -> Instant | i moved by d |
sleep(d: Duration) | Pause for at least d |
sleep_until(t: Instant) | Pause until t; nothing if it has passed |
let deadline: time.Instant = time.now().add(time.seconds(5));
while (deadline.since(time.now()).as_nanos() > 0) {
poll();
time.sleep(time.millis(100));
}The OS may sleep a little longer than asked: about 0.1 ms on Linux, and on
Windows 0 to 0.6 ms, because its timers step by about 0.5 ms. So a 100 us
sleep on Windows takes about 500 us. macOS uses nanosleep, as Linux does. Windows older than 10
version 1803 sleeps in whole milliseconds, often 15.6 ms. On Uusi a sleep
counts milliseconds and uses the processor while it waits.
Frame timing
A game’s update should run at a fixed rate, whatever the speed of the
screen and however long a frame took. FrameClock is that loop’s clock, a
value the game owns:
import time from "time";
struct State { x: f32; v: f32; }
fn update(s: *State, dt: f32) { s.x = s.x + s.v * dt; }
fn run() {
var clock = time.frame_clock(time.from_hz(60.0));
var now: State = State { x: 0.0; v: 100.0; };
var before: State = now;
while (running()) {
clock.tick(); // how long was the frame?
while (clock.step()) { // 0 or more fixed updates
before = now;
update(&now, clock.dt());
}
// Between the last two states, so motion is smooth on any screen
let a: f32 = clock.alpha();
draw_player(before.x + (now.x - before.x) * a);
}
}tick measures the frame and banks its time. step takes one fixed step
from the bank, until less than a step is left. alpha says how far that
leftover goes into the next step, from 0 to 1. The drawing is therefore up to
one step behind the simulation, and in exchange it never jumps: blend each
thing that moves between its last two states with alpha, as above.
dt() never changes, so the simulation does the same thing at any frame rate.
Time is kept in whole nanoseconds, so nothing drifts: after N steps exactly
N times the step has been used.
| Function | Does |
|---|---|
from_hz(hz: f64) -> Duration | One period at hz a second: from_hz(60.0) is 16666667 ns. Traps for hz <= 0 |
frame_clock(step: Duration) -> FrameClock | A clock of steps of step. Traps for step <= 0 |
c.tick() | Start a frame, reading the clock. The first tick measures nothing |
c.advance(d: Duration) | Start a frame d long, made up: for tests and replays |
c.step() -> bool | Take one step: true while there is one to run |
c.dt() -> f32 | The step in seconds |
c.alpha() -> f32 | How far between the last two states, 0 to 1 |
c.step_ns() -> i64, c.step_time() -> Duration | The step, exactly |
c.leftover() -> Duration | The time not stepped yet: alpha, exactly |
c.sim_time() -> Duration | The time simulated so far |
c.step_count() -> i64, c.frame_count() -> i64 | Steps taken, frames started |
c.frame_time() -> Duration | The last frame’s real length |
c.fps() -> f64 | Frames a second, over about the last 16 frames; 0 at first |
Since advance takes the frame’s length, the clock’s logic needs no clock to
test:
var c = time.frame_clock(time.millis(10));
c.advance(time.millis(25));
while (c.step()) { update(&s, c.dt()); } // runs twice
io.println("{c.alpha()}"); // 0.5When a frame takes too long
If a frame takes long, the next frame has more updates to run, which take longer still, and the game never recovers. So a frame may add only so much time: 250 ms by default (or 4 steps, for a step over 62.5 ms). The rest is thrown away and the game slows down instead of stalling.
| Function | Does |
|---|---|
c.set_max_frame(d: Duration) | The most simulated time one frame may add. Traps for d <= 0 |
c.dropped() -> Duration | All the time thrown away so far |
A game that wants to know it fell behind watches dropped grow. To allow at
most 4 updates a frame, c.set_max_frame(time.millis(66)) at 60 Hz.
Pause and slow motion
| Function | Does |
|---|---|
c.pause(), c.resume(), c.paused() -> bool | Stop simulated time and go on |
c.set_scale(num, den), c.scale() -> f64 | Run at num / den of real time |
While paused step is false and alpha holds, so the last picture stays
still; frame_time and fps go on. resume forgets the last reading, so a
pause is never taken for a long frame. That also makes it the way to skip a
level load: c.pause(); load(); c.resume();.
c.set_scale(1, 2) is half speed, c.set_scale(2, 1) double,
c.set_scale(0, 1) freezes time without pausing. The step’s length doesn’t
change, only how many steps run. It’s a fraction rather than a float so that
no time is lost, at any scale, and a replay steps the same on every machine.
num is 0 to 1000000, den 1 to 1000000; anything else traps.
Pacing without vsync
With vsync on, presenting a frame waits for the screen, and that is your
pacing. With vsync off or unavailable, a Pacer holds a target frame time:
var clock = time.frame_clock(time.from_hz(60.0));
var d = window.primary_display();
var hz: f64 = 60.0;
if (d.refresh_hz > 0.0) { hz = d.refresh_hz; } // 0 when the screen doesn't say
var pace = time.pacer(time.from_hz(hz));
while (running()) {
clock.tick();
while (clock.step()) { update(clock.dt()); }
render(clock.alpha());
pace.wait(); // sleep, then spin to the frame's end
}| Function | Does |
|---|---|
pacer(target: Duration) -> Pacer | Frames of target. Traps for target <= 0 |
p.wait() | Wait for the end of this frame |
p.spin_margin() -> Duration | How long it spins before the end of a frame |
p.set_spin_margin(d: Duration) | Fix that; 0 is sleep only. Traps for d < 0 |
wait sleeps most of what is left of the frame and spins the rest on the
clock, because sleeps wake late and a spin doesn’t. The spin margin follows
how late this machine’s sleeps have been: about 0.8 ms on Windows, 0.15 ms on
Linux. A frame that ran late shortens the next, so the rate holds; a frame a
whole target late starts over from now, with no burst of short frames.
Spinning uses one core for that time. Measured on Windows, with 2 ms of work a frame:
| Target | wait | Frame times, 1st to 99th percentile | Spinning |
|---|---|---|---|
| 60 Hz | as it comes | 16647 to 16685 us | 3 % of a core |
| 60 Hz | sleep only | 16227 to 17121 us | none |
| 144 Hz | as it comes | 6937 to 6951 us | 7 % of a core |
| 144 Hz | sleep only | 6501 to 7364 us | none |
For a menu or a laptop on battery, p.set_spin_margin(time.nanos(0)) gives
up the precision and the spinning.
Dates
pub struct DateTime { year: i64; month: i64; day: i64; hour: i64; minute: i64; second: i64; nanos: i64; }| Function | Does |
|---|---|
unix_nanos() -> i64 | Nanoseconds since 1970-01-01 00:00:00 UTC |
utc_now() -> DateTime | The current UTC date and time |
from_unix_nanos(ns) -> DateTime | The date and time ns after (or before) 1970 |
dt.to_unix_nanos() -> i64 | Back to nanoseconds |
dt.to_iso8601() -> string | "2026-10-02T14:03:00Z" |
parse_iso8601(text) -> DateTime or TimeError | Read ISO 8601 / RFC 3339 text |
to_iso8601 adds a fraction only when there is one, with 3, 6 or 9
digits (...:07.250Z).
parse_iso8601 accepts a date (2026-10-02) or a date and time
(2026-10-02T14:03:00, also with t or a space), an optional fraction
(.5, up to 9 digits), and an optional zone: Z, or an offset like
+02:00, which is converted to UTC. Text without a zone is UTC.
let t: time.DateTime = time.parse_iso8601("2026-10-02T16:03:00+02:00") or {
io.println("bad date: {err}");
return 1;
};
io.println(t.to_iso8601()); // 2026-10-02T14:03:00ZIt fails with TimeError.InvalidFormat for text in another shape and
TimeError.OutOfRange for a field outside its range: month 13, February 30,
hour 24, second 60 (leap seconds aren’t represented).
modified_ns from fs.metadata is a unix_nanos value:
time.from_unix_nanos(m.modified_ns) is when the file last changed.
Date arithmetic
| Function | Does |
|---|---|
from_fields(y, m, d, h = 0, min = 0, s = 0, nanos = 0) -> DateTime or TimeError | A date checked: OutOfRange for February 30, hour 24 … |
dt.add_days(n) -> DateTime | n days later (earlier when negative) |
dt.add_months(n) -> DateTime | n months later; a day past the month’s end becomes its last day |
dt.add_years(n) -> DateTime | n years later; February 29 becomes February 28 in a common year |
days_between(a, b) -> i64 | Days from a’s date to b’s, the times of day left out |
dt.day_of_year() -> i64 | 1 for January 1 … 365 or 366 |
dt.date() -> DateTime | The same day at midnight |
dt.with_time(h, m, s) -> DateTime or TimeError | The same day at another time |
days_in_month(y, m) -> i64 | 28 to 31 |
is_leap_year(y) -> bool | 2024 and 2000 are, 2026 and 1900 are not |
The calendar arithmetic underneath is the calendar
module (days_from_civil, is_leap_year, days_in_month,
put_digits): pure and without a clock, for code that cannot use time,
such as a pure parser of dates. time.is_leap_year and
time.days_in_month are the same functions.
let start: time.DateTime = time.from_fields(2026, 1, 31) or { return 1; };
io.println(start.add_months(1).to_iso8601()); // 2026-02-28T00:00:00Z
io.println(start.add_days(30).to_iso8601()); // 2026-03-02T00:00:00Z
let end: time.DateTime = time.from_fields(2026, 12, 24) or { return 1; };
io.println("{time.days_between(start, end)} days"); // 327 daysThe arithmetic is in UTC: a day is always 24 hours. To count on a wall
clock, work on a zone’s reading, z.local().
Time zones
pub struct Zoned { utc: DateTime; offset: i64; } // offset: minutes east of UTC| Function | Does |
|---|---|
local_offset(ns) -> i64 | The system zone’s offset in minutes at the instant ns, daylight saving time included |
local(ns) -> Zoned | The instant ns in the system zone |
local_now() -> Zoned | Now, in the system zone |
zoned(ns, offset_s) -> Zoned | The instant ns in a zone offset_s seconds east of UTC |
z.local() -> DateTime | The date and time a clock in that zone shows |
z.to_iso8601() -> string | "2026-10-13T09:12:03+03:00" |
parse_iso8601_zoned(text) -> Zoned or TimeError | Read ISO 8601 text keeping its offset |
dt.weekday() -> i64 | 1 Monday … 7 Sunday |
let z: time.Zoned = time.local_now();
let t: time.DateTime = z.local();
io.println("{t.hour}:{t.minute}"); // the user's wall clocklocal_offset asks the operating system, at the instant you give it: a
date from last winter gets winter time, a date in summer gets summer time.
The system zone is the one the user set. On Uusi the offset is 0.
A zone by name comes from intl, which carries the IANA time
zone database: intl.zone finds it and offset_at gives its offset in
seconds at an instant (in seconds since 1970), which zoned takes:
import intl from "intl";
let hel: intl.Zone = intl.zone("Europe/Helsinki") or { return 1; };
let t: i64 = time.unix_nanos();
let here: time.Zoned = time.zoned(t, hel.offset_at(t / 1000000000));
io.println(here.to_iso8601()); // 2026-10-26T15:05:09+02:00time doesn’t import intl itself: intl embeds 4 MB of data (the
zones and CLDR’s locale data) that a program wanting only the clock
shouldn’t carry. parse_iso8601_zoned keeps the offset the text was
written with, so to_iso8601 writes it back the same.
The user’s own zone by name ("Europe/Helsinki") is
timezone.system_zone(). Give that name to intl when a
date far from today matters (a calendar, a booking): local_offset is
right for the instant you ask about, but a name carries the zone’s rules
for every date.
Day and month names, layouts and words in the user’s language are
intl’s too: intl.DateTimeFormat writes “maanantai 26. lokakuuta 2026
klo 15.05” for Finnish, and intl.relative_time “vor 3 Tagen”.
Format, relative and brief here stay English.
Mail dates
| Function | Does |
|---|---|
parse_rfc5322(text) -> Zoned or TimeError | Read a mail’s Date header |
z.to_rfc5322() -> string | "Tue, 13 Oct 2026 09:12:03 +0300" |
let sent: time.Zoned = time.parse_rfc5322("Tue, 13 Oct 2026 09:12:03 +0300") or {
return 1;
};
io.println(sent.utc.to_iso8601()); // 2026-10-13T06:12:03Z
io.println("{sent.offset}"); // 180
let date_header: string = time.local_now().to_rfc5322();parse_rfc5322 is lenient, as mail needs. The day of the week is
optional. Names match on their first three letters in any case. Comments
like (EEST) and line folds are skipped. IMAP’s 13-Oct-2026 09:12:03 +0300
reads too. A 2-digit year is 1950-2049. The seconds are optional. The zone
can be +0300, +03:00, GMT, UT, or a US zone name like EST or
PDT. A zone that isn’t given or isn’t known (-0000, CEST) gives offset
0. Text after the zone is ignored.
It fails with InvalidFormat when the day, month, year, hour and minute
aren’t there, and OutOfRange for a field outside its range (32 Oct,
24:00).
Formatting
Build a Format once, call by call, then apply it to any number of dates:
var f: time.Format = time.format();
f.day();
f.text(".");
f.month();
f.text(".");
f.year();
f.text(" ");
f.hour();
f.text(":");
f.minute(2);
let z: time.Zoned = time.local_now();
io.println(f.apply_zoned(&z)); // 13.10.2026 9:05| Call | Writes |
|---|---|
year(pad), year_short() | 2026, 26 |
month(pad), month_name(), month_short() | 10, October, Oct |
day(pad) | 13 |
weekday(), weekday_name(), weekday_short() | 2 (Monday is 1), Tuesday, Tue |
hour(pad), hour12(pad), am_pm() | 9, 9, AM |
minute(pad), second(pad) | 5 (05 with pad 2) |
fraction(digits) | the second’s fraction: fraction(3) is milliseconds, 250 |
offset(), offset(false) | +03:00, +0300 |
text(s) | s as it is |
pad is the least number of digits, filled with zeros: month(2) writes
03. Without it the number is written as it is.
f.apply(d) writes a DateTime as it is (a UTC one, or a zone’s reading
from z.local()); f.apply_zoned(&z) writes a Zoned in its own zone,
with its offset. Day and month names are English; for the user’s language
use intl.DateTimeFormat.
Relative time and message-list dates
io.println(time.relative(sent_ns, time.unix_nanos())); // 5 minutes ago
io.println(time.brief(sent_ns, time.unix_nanos())); // 14:32relative(then, now) says how long ago (or how far ahead) then is:
“just now”, “1 minute ago”, “5 hours ago”, “3 days ago”, “7 months ago”,
“2 years ago”, “in 3 days”. It rounds to the nearest whole unit.
brief(then, now) writes a date as a message list does, in the user’s
time zone: the time for today (“14:32”), “Yesterday”, the day’s name for
the past week (“Tue”), the day and month this year (“13 Oct”), and the
year too for older dates (“13 Oct 2025”).
To change the shapes, take time.brief_format() and replace its fields
(today, yesterday, week, year, older), then call
b.apply(&then, &now) with two Zoned values:
var b: time.Brief = time.brief_format();
b.yesterday = "eilen";
let then: time.Zoned = time.local(sent_ns);
let now: time.Zoned = time.local_now();
io.println(b.apply(&then, &now));Changed in this release
The old time.nanos(), time.micros() and time.millis() (no argument,
returning a clock reading) are gone: those names now make Durations. Use
time.now() and Instant.elapsed():
let start: time.Instant = time.now(); // was: let start: i64 = time.nanos();
let ns: i64 = start.elapsed().as_nanos();Platforms
Windows, Linux and macOS have both clocks and the system zone (Windows’
time zone settings; on Linux and macOS TZ, else /etc/localtime). On Uusi the monotonic clock
works in millisecond steps and the wall clock in whole seconds (the
kernel’s real-time clock; on a kernel without it unix_nanos() is 0).
Measure time on Uusi with now(), not with differences of unix_nanos().
Uusi has no time zones: local_offset is 0.
Last modified: