std.time
The time module.
use std.timeThe implementation gap has been fixed (#338 / #340, 2026-09-19).
The return types of
now/parse_timeareInt(Unix timestamps in seconds).DateTimeis not a built-in type name—older versions used a temporary compatibility alias with the same meaning (at runtime it was stillInt; fixed as part of #338), and P10 S11 (issue #513) has now removed it: a bareDateTimeannotation is reported as an unknown type, so timestamps must always be written asInt. The accessor export names use the flat form, such asdatetime_year(::is a reserved lexical token and cannot appear in a field-access position).The values returned by
now()/parse_time()can now be used directly in arithmetic, formatting, and all accessors.Time zone: Both
format_timeand the eightdatetime_*accessors usetimestamp_to_datetime(src/std/time.rs:123-178), which performs pure UTC arithmetic—it does not read the local time-zone offset, and there is no distinction betweenLocalandUtc. Therefore, the ISO 8601 string output bydatetime_to_stringis also in UTC.
Function Overview
| Function | Signature |
|---|---|
now | () -> Int |
timestamp | () -> Int |
timestamp_ms | () -> Int |
sleep | (seconds: Float) -> Void |
format_time | (dt: Int, fmt: String) -> String |
parse_time | (fmt: String, s: String) -> Int |
datetime_year | (dt: Int) -> Int |
datetime_month | (dt: Int) -> Int |
datetime_day | (dt: Int) -> Int |
datetime_hour | (dt: Int) -> Int |
datetime_minute | (dt: Int) -> Int |
datetime_second | (dt: Int) -> Int |
datetime_weekday | (dt: Int) -> Int |
datetime_to_string | (dt: Int) -> String |
Retrieving Time
now
now: () -> IntReturns the current time.
Returns: an Int timestamp (seconds), i.e., the number of seconds since the Unix epoch (src/std/time.rs:238-244 directly returns RuntimeValue::Int)—the return value is an Int: it can be used directly in arithmetic and comparisons, and passed directly to format_time and all datetime_* accessors.
Use timestamp_ms when millisecond precision is required.
use std.assert
use std.time
main: () -> Void = {
t = time.now()
assert(time.datetime_year(t) > 2020) // Int, can be passed directly to an accessor
assert(t > 0) // Int, can be used directly in comparisons
println(time.format_time(t, "%Y-%m-%dT%H:%M:%SZ"))
}
nowandtimestampreturn the same thing (both are Unix timestamps in seconds and have the typeInt); the two names are retained only for readability at call sites.
timestamp
timestamp: () -> IntReturns the current Unix timestamp (seconds), which can be used directly in arithmetic and comparisons.
use std.assert
use std.time
main: () -> Void = {
assert(time.timestamp() > 0)
}timestamp_ms
timestamp_ms: () -> IntReturns the current Unix timestamp (milliseconds).
use std.assert
use std.time
main: () -> Void = {
// Millisecond precision is no lower than second precision
assert(time.timestamp_ms() >= time.timestamp())
}sleep
sleep: (seconds: Float) -> VoidSleeps for the specified number of seconds (decimals are allowed). The similarly named std.concurrent.sleep uses milliseconds, so take care to distinguish them.
seconds— Number of seconds to sleep; acceptsInt(interpreted as seconds) orFloat
Error: throws E6007 if the argument is neither Int nor Float.
use std.time
main: () -> Void = {
time.sleep(0.0)
}Not exported on the
wasm32target.
Formatting and Parsing
format_time
format_time: (dt: Int, fmt: String) -> StringFormats a timestamp according to fmt, supporting strftime-style placeholders.
dt— Unix timestamp (seconds), must be anIntfmt— Format string
The return values of
nowandparse_timeareInt, so passing them directly does not causeE1002.
Supported placeholders:
| Placeholder | Meaning | Example |
|---|---|---|
%Y | Four-digit year | 2024 |
%m | Two-digit month | 01 |
%d | Two-digit day | 15 |
%H | Two-digit hour (24-hour clock) | 10 |
%M | Two-digit minute | 30 |
%S | Two-digit second | 00 |
%w | Weekday (0 = Sunday) | 1 |
%F | Equivalent to %Y-%m-%d | 2024-01-15 |
%T | Equivalent to %H:%M:%S | 10:30:00 |
The timestamp is broken down in UTC (src/std/time.rs:123-178 is pure arithmetic and does not include a local time-zone offset). Unrecognized placeholders are preserved as-is.
Error: throws E6007 when too few arguments are provided.
use std.assert
use std.string
use std.time
main: () -> Void = {
// 0 = Unix epoch (UTC)
assert(time.format_time(0, "%Y") == "1970")
assert(time.format_time(0, "%m") == "01")
assert(time.format_time(0, "%F %T") == "1970-01-01 00:00:00")
// The current timestamp (Int) can also be used directly
s = time.format_time(time.timestamp(), "%Y")
assert(string.len(s) == 4)
}parse_time
parse_time: (fmt: String, s: String) -> IntParses a time string into a Unix timestamp (seconds) according to fmt.
fmt— Format string; it participates in parsing (src/std/time.rs:382callsparse_by_format(&fmt, &s))s— String to parse
Returns: an Int timestamp (seconds; see now), which can be used directly in arithmetic, with format_time, and with all datetime_* accessors.
The directives recognized by fmt correspond one-to-one with those in format_time: %Y %m %d %H %M %S, as well as the combined forms %F (%Y-%m-%d) and %T (%H:%M:%S). Fields that are not present default to January 1 at 00:00:00—parse_time("%Y", "2024") yields 2024-01-01T00:00:00Z.
Error: throws E6007 when the string does not match fmt, with a message such as Invalid time format: '…' does not match '…'. This means that fmt is a contract, not merely a hint: parse_time("%d/%m/%Y", "15/01/2024") succeeds, whereas parse_time("totally-bogus", "2024-01-15T10:30:00") raises an error.
use std.assert
use std.time
main: () -> Void = {
// fmt determines the parsing format
ts = time.parse_time("%d/%m/%Y", "15/01/2024")
assert(time.datetime_year(ts) == 2024)
assert(time.datetime_month(ts) == 1)
assert(time.datetime_day(ts) == 15)
// Combined directives are also recognized
full = time.parse_time("%Y-%m-%d %H:%M:%S", "2024-01-15 10:30:00")
assert(time.datetime_hour(full) == 10)
assert(time.datetime_minute(full) == 30)
}Date Component Access
std.time exports eight date component accessors (#338 has been fixed, and the export names use the flat form):
| Export name | Signature | Description |
|---|---|---|
datetime_year | (dt: Int) -> Int | Four-digit year |
datetime_month | (dt: Int) -> Int | Month (1–12) |
datetime_day | (dt: Int) -> Int | Day of month (1–31) |
datetime_hour | (dt: Int) -> Int | Hour (0–23) |
datetime_minute | (dt: Int) -> Int | Minute (0–59) |
datetime_second | (dt: Int) -> Int | Second (0–59) |
datetime_weekday | (dt: Int) -> Int | Weekday (0 = Sunday) |
datetime_to_string | (dt: Int) -> String | ISO 8601-format string |
The return values of now() / parse_time() are Int timestamps and can be passed directly to these accessors, as well as used directly with format_time. They are broken down in UTC (src/std/time.rs:123-178 is pure arithmetic and does not include a local time-zone offset):
use std.assert
use std.time
main: () -> Void = {
ts = time.parse_time("%Y-%m-%d", "2024-01-15")
assert(time.datetime_year(ts) == 2024)
assert(time.datetime_month(ts) == 1)
assert(time.datetime_day(ts) == 15)
// Equivalent to format_time
assert(time.format_time(ts, "%Y-%m-%d") == "2024-01-15")
// 0 = Unix epoch (UTC Thursday)
assert(time.datetime_weekday(0) == 4)
assert(time.datetime_to_string(0) == "1970-01-01T00:00:00")
}Related
std.concurrent— Millisecond sleep- Error code reference —
E6007generic runtime error
