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.
Get the current time
Section titled “Get the current time”import time
let now = time.now()print(now.to_str())print(time.now_local().format("%H:%M"))print(time.today().weekday())Format and parse
Section titled “Format and parse”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:09ZThursday 26 November 2026, 02:05 PM2026-11-26T15:05:09+01:001795701909Work with dates
Section titled “Work with dates”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-292024-03-01true358Durations
Section titled “Durations”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())250022500ms60sReference
Section titled “Reference”import timeFunctions
Section titled “Functions”days_in_month
Section titled “days_in_month”pub fn days_in_month(year: i64, month: i64) -> i64pub fn date(year: i64, month: i64, day: i64) -> Option<Date>A real date, or None for something like February 30.
from_days
Section titled “from_days”pub fn from_days(days: i64) -> DateHoward Hinnant’s civil_from_days: the date days after 1970-01-01.
pub fn today() -> DateThe date in UTC today.
parse_date
Section titled “parse_date”pub fn parse_date(s: &Str) -> Option<Date>Reads YYYY-MM-DD.
from_unix
Section titled “from_unix”pub fn from_unix(seconds: i64, offset: i64) -> DateTimeThe time seconds after 1970-01-01 00:00 UTC, as seen offset seconds ahead of UTC.
pub fn now() -> DateTimenow_local
Section titled “now_local”pub fn now_local() -> DateTimeThe 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.
parse_datetime
Section titled “parse_datetime”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.
milliseconds
Section titled “milliseconds”pub fn milliseconds(n: i64) -> Durationseconds
Section titled “seconds”pub fn seconds(n: i64) -> Durationminutes
Section titled “minutes”pub fn minutes(n: i64) -> Durationpub fn hours(n: i64) -> Durationpub fn start() -> TimerStarts measuring now; elapsed() tells how long ago that was.
pub fn sleep(d: &Duration)import time
time.sleep(&time.milliseconds(10))print("awake")awakepub 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.
Date.year
Section titled “Date.year”pub fn year(&self) -> i64Date.month
Section titled “Date.month”pub fn month(&self) -> i64Date.day
Section titled “Date.day”pub fn day(&self) -> i64Date.days_since_epoch
Section titled “Date.days_since_epoch”pub fn days_since_epoch(&self) -> i64Date.add_days
Section titled “Date.add_days”pub fn add_days(&self, n: i64) -> DateDate.days_until
Section titled “Date.days_until”pub fn days_until(&self, other: &Date) -> i64Positive when other is later.
Date.weekday
Section titled “Date.weekday”pub fn weekday(&self) -> StrDate.to_str
Section titled “Date.to_str”pub fn to_str(&self) -> StrDate.format
Section titled “Date.format”pub fn format(&self, pattern: &Str) -> StrThe same % codes as DateTime.format, with the time of day at midnight UTC.
DateTime
Section titled “DateTime”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.
DateTime.date
Section titled “DateTime.date”pub fn date(&self) -> DateDateTime.hour
Section titled “DateTime.hour”pub fn hour(&self) -> i64DateTime.minute
Section titled “DateTime.minute”pub fn minute(&self) -> i64DateTime.second
Section titled “DateTime.second”pub fn second(&self) -> i64DateTime.offset
Section titled “DateTime.offset”pub fn offset(&self) -> i64Seconds ahead of UTC.
DateTime.unix
Section titled “DateTime.unix”pub fn unix(&self) -> i64Seconds since 1970-01-01 00:00 UTC.
DateTime.add_seconds
Section titled “DateTime.add_seconds”pub fn add_seconds(&self, n: i64) -> DateTimeDateTime.with_offset
Section titled “DateTime.with_offset”pub fn with_offset(&self, offset: i64) -> DateTimeThe same moment seen offset seconds ahead of UTC.
DateTime.format
Section titled “DateTime.format”pub fn format(&self, pattern: &Str) -> StrCodes: %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.
DateTime.to_str
Section titled “DateTime.to_str”pub fn to_str(&self) -> StrISO 8601, like 2026-11-26T14:05:09Z or 2026-11-26T15:05:09+01:00.
Duration
Section titled “Duration”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 5400sIt also has 1 private field(s), so outside this module it is made only by this module’s functions, which check what goes in.
Duration.as_milliseconds
Section titled “Duration.as_milliseconds”pub fn as_milliseconds(&self) -> i64Duration.as_seconds
Section titled “Duration.as_seconds”pub fn as_seconds(&self) -> i64Whole seconds, rounded down.
Duration.plus
Section titled “Duration.plus”pub fn plus(&self, other: &Duration) -> DurationDuration.minus
Section titled “Duration.minus”pub fn minus(&self, other: &Duration) -> DurationThe difference; a longer Duration taken from a shorter one stops the program.
Duration.times
Section titled “Duration.times”pub fn times(&self, n: i64) -> Durationn times as long.
Duration.to_str
Section titled “Duration.to_str”pub fn to_str(&self) -> StrWhole seconds print as 3s, anything else in milliseconds, like 2500ms.
Duration.add
Section titled “Duration.add”fn add(&self, other: &Duration) -> DurationDuration.sub
Section titled “Duration.sub”fn sub(&self, other: &Duration) -> DurationDuration.compare
Section titled “Duration.compare”fn compare(&self, other: &Duration) -> i64pub 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.
Timer.elapsed
Section titled “Timer.elapsed”pub fn elapsed(&self) -> Duration