Standard Library
std.time
Time helpers for tool-style scripts.
Exported functions
Return the current UTC timestamp.
Examples
use time from "std.time"
const timestamp = time.now()
@println(time.format_rfc3339(timestamp)) # Current UTC timeReturn the current Unix timestamp in seconds.
Return the current Unix timestamp in milliseconds.
Pause the current thread for the specified number of milliseconds.
Pause the current thread for a duration value.
Construct a timestamp from Unix seconds.
Construct a timestamp from Unix milliseconds.
Return the Unix seconds for a timestamp.
Return the Unix milliseconds for a timestamp.
Create a duration in milliseconds.
Create a duration in seconds.
Create a duration in minutes.
Create a duration in hours.
Create a duration in days.
Return the sum of two durations.
Examples
use time from "std.time"
const delay = time.add(time.seconds(2), time.milliseconds(500))
@println(delay.milliseconds) # 2500Return the difference between two durations.
Multiply a duration by an integer factor.
Return the elapsed duration between two timestamps.
Examples
use time from "std.time"
const start = time.from_unix_seconds(10)
const finish = time.from_unix_millis(12500)
@println(time.between(start, finish).milliseconds) # 2500Add a duration to a timestamp.
Examples
use time from "std.time"
const start = time.parse_rfc3339("2026-04-18T12:00:00Z")
const deadline = time.add_to(start, time.minutes(30))
@println(time.format_rfc3339(deadline)) # 2026-04-18T12:30:00ZSubtract a duration from a timestamp.
Format a timestamp as an RFC3339 string in UTC.
Examples
use time from "std.time"
@println(time.format_rfc3339(time.from_unix_seconds(0))) # 1970-01-01T00:00:00ZParse an RFC3339 string into a timestamp.
Examples
use time from "std.time"
def demonstrate() -> Void
var timestamp = try time.try_parse_rfc3339("not-a-date") catch err
case is time.TimeError.InvalidRfc3339 => time.from_unix_seconds(0)
case _ => time.from_unix_seconds(0)
end
@println(time.format_rfc3339(timestamp)) # 1970-01-01T00:00:00Z
end
demonstrate()Parse an RFC3339 string and panic on invalid input.
Examples
use time from "std.time"
const timestamp = time.parse_rfc3339("2026-04-18T14:30:00+02:00")
@println(time.format_rfc3339(timestamp)) # 2026-04-18T12:30:00ZBreak a timestamp into UTC calendar components. Weekday is ISO numbering: 1 = Monday through 7 = Sunday.
Examples
use time from "std.time"
const parts = time.to_utc(time.from_unix_seconds(0))
@println(parts.year) # 1970
@println(parts.weekday) # 4 (Thursday; Monday is 1)Return the UTC year of a timestamp.
Return the UTC month (1-12) of a timestamp.
Return the UTC day of month (1-31) of a timestamp.
Return the UTC hour (0-23) of a timestamp.
Return the UTC minute (0-59) of a timestamp.
Return the UTC second (0-59) of a timestamp.
Return the ISO weekday (1 = Monday .. 7 = Sunday) of a timestamp.
Format a timestamp with a format description such as "[year]-[month]-[day] [hour]:[minute]:[second]".
Format a timestamp with a format description, panicking on failure.
Examples
use time from "std.time"
const timestamp = time.from_calendar(2026, 4, 18, 8, 5, 9)
@println(time.format(timestamp, "[year]-[month]-[day] [hour]:[minute]")) # 2026-04-18 08:05Parse text with a format description into a UTC timestamp. Patterns without an offset are interpreted as UTC.
Parse text with a format description, panicking on failure.
Examples
use time from "std.time"
const timestamp = time.parse("2026-04-18", "[year]-[month]-[day]")
@println(time.format_rfc3339(timestamp)) # 2026-04-18T00:00:00ZBuild a UTC timestamp from calendar components.
Examples
use time from "std.time"
def demonstrate() -> Void
try time.try_from_calendar(2023, 2, 29, 0, 0, 0) catch err
case is time.TimeError.InvalidDate
@println("That date does not exist")
return
case _
@panic("unexpected time error")
end
return
end
demonstrate()Build a UTC timestamp from calendar components, panicking on failure.
Examples
use time from "std.time"
const timestamp = time.from_calendar(2024, 2, 29, 12, 30, 45)
@println(time.format_date(timestamp)) # 2024-02-29
@println(time.format_time(timestamp)) # 12:30:45Format a timestamp as "YYYY-MM-DD" in UTC.
Examples
use time from "std.time"
@println(time.format_date(time.from_unix_seconds(0))) # 1970-01-01Format a timestamp as "HH:MM:SS" in UTC.
Examples
use time from "std.time"
@println(time.format_time(time.from_unix_seconds(0))) # 00:00:00