Skip to content

time

Dates, times of day, and lengths of time. A Date is a calendar day, a DateTime is a moment with a UTC offset, and a Duration is how long something takes.

Date and DateTime always hold valid values: their fields are private and every function that could produce something like February 30 returns Option instead. Every timeout in the standard library takes a Duration, so seconds and milliseconds cannot be mixed up.

import time
let now = time.now()
print(now.to_str())
print(time.now_local().format("%H:%M"))
print(time.today().weekday())

format takes strftime-style codes. to_str gives ISO 8601.

import time
let t = time.from_unix(1795701909, 0)
print(t.to_str())
print(t.format("%A %e %B %Y, %I:%M %p"))
print(t.with_offset(3600).to_str())
let back = time.parse_datetime("2026-11-26T15:05:09+01:00").unwrap()
print(back.unix())
2026-11-26T14:05:09Z
Thursday 26 November 2026, 02:05 PM
2026-11-26T15:05:09+01:00
1795701909
import time
let d = time.date(2024, 2, 28).unwrap()
print(d.add_days(1).to_str())
print(d.add_days(2).to_str())
print(time.date(2023, 2, 29).is_none())
let start = time.parse_date("2026-01-01").unwrap()
let end = time.parse_date("2026-12-25").unwrap()
print(start.days_until(&end))
2024-02-29
2024-03-01
true
358
import time
let wait = time.seconds(2).plus(&time.milliseconds(500))
print(wait.as_milliseconds())
print(wait.as_seconds())
print(wait.to_str())
print(time.minutes(1).to_str())
2500
2
2500ms
60s
import time
pub fn days_in_month(year: i64, month: i64) -> i64
pub fn date(year: i64, month: i64, day: i64) -> Option<Date>

A real date, or None for something like February 30.

pub fn from_days(days: i64) -> Date

Howard Hinnant’s civil_from_days: the date days after 1970-01-01.

pub fn today() -> Date

The date in UTC today.

pub fn parse_date(s: &Str) -> Option<Date>

Reads YYYY-MM-DD.

pub fn from_unix(seconds: i64, offset: i64) -> DateTime

The time seconds after 1970-01-01 00:00 UTC, as seen offset seconds ahead of UTC.

pub fn now() -> DateTime
pub fn now_local() -> DateTime

The time in this machine’s time zone.

The offset is the local zone’s offset at this moment. Adding seconds to the result keeps that offset, even across a daylight saving change; call with_offset(os.utc_offset(t.unix())) to get the right one for another moment.

pub fn parse_datetime(s: &Str) -> Option<DateTime>

Reads ISO 8601 YYYY-MM-DDTHH:MM:SS (a space works for T), with Z or +HH:MM after it.

Accepts YYYY-MM-DDTHH:MM:SS (or a space instead of T), then Z, +HH:MM, -HH:MM or nothing. Nothing means UTC, not local time. Fractional seconds (14:05:09.250Z) are not accepted and give None.

pub fn milliseconds(n: i64) -> Duration
pub fn seconds(n: i64) -> Duration
pub fn minutes(n: i64) -> Duration
pub fn hours(n: i64) -> Duration
pub fn start() -> Timer

Starts measuring now; elapsed() tells how long ago that was.

pub fn sleep(d: &Duration)
import time
time.sleep(&time.milliseconds(10))
print("awake")
awake
pub struct Date {
}

A calendar date (proleptic Gregorian); days are counted from 1970-01-01.

It also has 3 private field(s), so outside this module it is made only by this module’s functions, which check what goes in.

pub fn year(&self) -> i64
pub fn month(&self) -> i64
pub fn day(&self) -> i64
pub fn days_since_epoch(&self) -> i64
pub fn add_days(&self, n: i64) -> Date
pub fn days_until(&self, other: &Date) -> i64

Positive when other is later.

pub fn weekday(&self) -> Str
pub fn to_str(&self) -> Str
pub fn format(&self, pattern: &Str) -> Str

The same % codes as DateTime.format, with the time of day at midnight UTC.

pub struct DateTime {
}

A date and time of day; offset is seconds ahead of UTC (0 for UTC).

It also has 5 private field(s), so outside this module it is made only by this module’s functions, which check what goes in.

pub fn date(&self) -> Date
pub fn hour(&self) -> i64
pub fn minute(&self) -> i64
pub fn second(&self) -> i64
pub fn offset(&self) -> i64

Seconds ahead of UTC.

pub fn unix(&self) -> i64

Seconds since 1970-01-01 00:00 UTC.

pub fn add_seconds(&self, n: i64) -> DateTime
pub fn with_offset(&self, offset: i64) -> DateTime

The same moment seen offset seconds ahead of UTC.

pub fn format(&self, pattern: &Str) -> Str

Codes: %Y year, %y two-digit year, %m month, %d day, %e day padded with a space, %H hour (24h), %I hour (12h), %p AM/PM, %M minute, %S second, %j day of the year, %a/%A short/long weekday, %b/%B short/long month, %z offset like +0100, %Z offset like +01:00 or Z for UTC, %F the date as YYYY-MM-DD, %T the time as HH:MM:SS, %% a percent sign. Names are in English. An unknown code is copied as written.

pub fn to_str(&self) -> Str

ISO 8601, like 2026-11-26T14:05:09Z or 2026-11-26T15:05:09+01:00.

pub struct Duration {
}

A length of time, never negative; made with milliseconds, seconds or minutes.

Never negative: milliseconds(-1) panics, and so does taking a longer Duration from a shorter one. Durations add (+, plus), subtract (-, minus), scale (times) and compare (<, sort). To measure how long something takes, use a Timer:

import time
let t = time.start()
let budget = time.seconds(2) - time.milliseconds(500)
print(f"{t.elapsed() < budget} {budget} {time.hours(1) + time.minutes(30)}")
true 1500ms 5400s

It also has 1 private field(s), so outside this module it is made only by this module’s functions, which check what goes in.

pub fn as_milliseconds(&self) -> i64
pub fn as_seconds(&self) -> i64

Whole seconds, rounded down.

pub fn plus(&self, other: &Duration) -> Duration
pub fn minus(&self, other: &Duration) -> Duration

The difference; a longer Duration taken from a shorter one stops the program.

pub fn times(&self, n: i64) -> Duration

n times as long.

pub fn to_str(&self) -> Str

Whole seconds print as 3s, anything else in milliseconds, like 2500ms.

fn add(&self, other: &Duration) -> Duration
fn sub(&self, other: &Duration) -> Duration
fn compare(&self, other: &Duration) -> i64
pub struct Timer {
}

A clock that only moves forward, for measuring how long something takes.

It also has 1 private field(s), so outside this module it is made only by this module’s functions, which check what goes in.

pub fn elapsed(&self) -> Duration