Skip to content

std.time ​

The time module.

yaoxiang
use std.time

The implementation gap has been fixed (#338 / #340, 2026-09-19).

The return types of now / parse_time are Int (Unix timestamps in seconds). DateTime is not a built-in type name—older versions used a temporary compatibility alias with the same meaning (at runtime it was still Int; fixed as part of #338), and P10 S11 (issue #513) has now removed it: a bare DateTime annotation is reported as an unknown type, so timestamps must always be written as Int. The accessor export names use the flat form, such as datetime_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_time and the eight datetime_* accessors use timestamp_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 between Local and Utc. Therefore, the ISO 8601 string output by datetime_to_string is also in UTC.

Function Overview ​

FunctionSignature
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 ​

yaoxiang
now: () -> Int

Returns 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.

yaoxiang
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"))
}

now and timestamp return the same thing (both are Unix timestamps in seconds and have the type Int); the two names are retained only for readability at call sites.

timestamp ​

yaoxiang
timestamp: () -> Int

Returns the current Unix timestamp (seconds), which can be used directly in arithmetic and comparisons.

yaoxiang
use std.assert
use std.time

main: () -> Void = {
    assert(time.timestamp() > 0)
}

timestamp_ms ​

yaoxiang
timestamp_ms: () -> Int

Returns the current Unix timestamp (milliseconds).

yaoxiang
use std.assert
use std.time

main: () -> Void = {
    // Millisecond precision is no lower than second precision
    assert(time.timestamp_ms() >= time.timestamp())
}

sleep ​

yaoxiang
sleep: (seconds: Float) -> Void

Sleeps 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; accepts Int (interpreted as seconds) or Float

Error: throws E6007 if the argument is neither Int nor Float.

yaoxiang
use std.time

main: () -> Void = {
    time.sleep(0.0)
}

Not exported on the wasm32 target.

Formatting and Parsing ​

format_time ​

yaoxiang
format_time: (dt: Int, fmt: String) -> String

Formats a timestamp according to fmt, supporting strftime-style placeholders.

  • dt — Unix timestamp (seconds), must be an Int
  • fmt — Format string

The return values of now and parse_time are Int, so passing them directly does not cause E1002.

Supported placeholders:

PlaceholderMeaningExample
%YFour-digit year2024
%mTwo-digit month01
%dTwo-digit day15
%HTwo-digit hour (24-hour clock)10
%MTwo-digit minute30
%STwo-digit second00
%wWeekday (0 = Sunday)1
%FEquivalent to %Y-%m-%d2024-01-15
%TEquivalent to %H:%M:%S10: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.

yaoxiang
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 ​

yaoxiang
parse_time: (fmt: String, s: String) -> Int

Parses a time string into a Unix timestamp (seconds) according to fmt.

  • fmt — Format string; it participates in parsing (src/std/time.rs:382 calls parse_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.

yaoxiang
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 nameSignatureDescription
datetime_year(dt: Int) -> IntFour-digit year
datetime_month(dt: Int) -> IntMonth (1–12)
datetime_day(dt: Int) -> IntDay of month (1–31)
datetime_hour(dt: Int) -> IntHour (0–23)
datetime_minute(dt: Int) -> IntMinute (0–59)
datetime_second(dt: Int) -> IntSecond (0–59)
datetime_weekday(dt: Int) -> IntWeekday (0 = Sunday)
datetime_to_string(dt: Int) -> StringISO 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):

yaoxiang
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")
}