datetime#
timestamps, civil dates, RFC 3339, IANA time zones with DST, cron schedules -- crate root lib/datetime/lib.rs
datetime -- calendar time: timestamps, civil dates, RFC 3339, time zones, cron schedules
Timestamp { secs, nanos } seconds since 1970-01-01 00:00 UTC
Civil { year .. second, nanos } a calendar date and wall time (no zone)
days_from_civil / civil_from_days / days_in_month / is_leap / weekday (0 = Monday)
iso_week / iso_weeks_in_year / iso_week_monday ISO 8601 weeks (Monday to Sunday, week 1
holds the year's first Thursday)
rfc3339(ts) / rfc3339_offset(ts, offset) / parse_rfc3339(s)
format_date_time(ts, &zone, en) / format_date / format_time / format_duration(secs, en)
tz::Zone IANA zones with their current DST rule (zones.rs from
tools/gen_tz.py), Windows zone keys, POSIX TZ strings;
offset_at / to_local / from_local (Single, Ambiguous,
Gap)
cron::Cron 5-field cron expressions: next_after in a zone (a time
in a DST gap runs right after the gap, a repeated time
runs once), next_n, describe (de / en)
No clock here: plat::unix_time_ns gives "now"; datetime only computes.
datetime crate root#
struct Timestampa point in time: seconds since 1970-01-01 00:00:00 UTC and nanosecondssecs: i64nanos: u32
impl Timestamp
const fn from_secs(secs: i64) -> Timestampconst fn from_nanos(ns: i64) -> Timestampfrom nanoseconds since the epoch (plat::unix_time_ns)const fn from_millis(ms: i64) -> Timestampconst fn as_millis(&self) -> i64const fn add_secs(&self, s: i64) -> Timestampstruct Civila calendar date and wall time without a zoneyear: i32month: u8day: u8hour: u8minute: u8second: u8nanos: u32
impl Civil
const fn new(year: i32, month: u8, day: u8, hour: u8, minute: u8, second: u8) -> Civilconst fn date(year: i32, month: u8, day: u8) -> Civilfn from_unix(secs: i64) -> Civilthe civil time of seconds since the epoch read as UTCfn to_unix(&self) -> i64seconds since the epoch of this wall time read as UTCfn weekday(&self) -> u80 = Monday .. 6 = Sundayfn is_valid(&self) -> boolfn add_days(&self, days: i64) -> Civilthe same wall timedaysdays laterfn is_leap(y: i32) -> boolfn days_in_month(y: i32, m: u8) -> u8days of month m (1..12)fn days_from_civil(y: i32, m: u8, d: u8) -> i64days since 1970-01-01 of a date (proleptic Gregorian; Howard Hinnant's algorithm)fn civil_from_days(z: i64) -> (i32, u8, u8)(year, month, day) of days since 1970-01-01fn weekday(y: i32, m: u8, d: u8) -> u8weekday of a date: 0 = Monday .. 6 = Sundayfn iso_weeks_in_year(y: i32) -> u8the ISO 8601 weeks of week-numbering year y: 53 when the year starts on a Thursday, or on a Wednesday in a leap year, else 52fn iso_week(y: i32, m: u8, d: u8) -> (i32, u8)the ISO 8601 week of a date -> (its week-numbering year, the week 1 ..= 53): the days of 29 December to 3 January may lie in the week of the other year (2026-12-31 is 2026-W53, 2027-01-01 too)fn iso_week_monday(y: i32, week: u8) -> (i32, u8, u8)the Monday of ISO 8601 weekweekof week-numbering year y -> (year, month, day); a week beyond the year's last counts on into the next yearfn rfc3339(ts: Timestamp) -> String"2026-10-12T19:58:00Z" (with ".123" milliseconds when the timestamp has them)fn rfc3339_offset(ts: Timestamp, offset_secs: i32) -> Stringthe timestamp as local time with its UTC offset: "2026-10-12T21:58:00+02:00"fn parse_rfc3339(s: &str) -> Option<Timestamp>parseYYYY-MM-DDTHH:MM:SS[.fff…](Z|±HH:MM)(also with a space or lower-case t / z)fn format_date_time(ts: Timestamp, zone: &Zone, en: bool) -> String"12.10.2026 19:58" (de) / "2026-10-12 19:58" (en) in a zonefn format_date(ts: Timestamp, zone: &Zone, en: bool) -> String"12.10.2026" / "2026-10-12"fn format_time(ts: Timestamp, zone: &Zone) -> String"19:58"fn format_duration(secs: i64, en: bool) -> Stringa duration for people: "42 s", "3 min 05 s", "1 h 12 min", "2 d 3 h"fn format_relative(now: i64, then: i64, en: bool) -> Stringa relative time for people: "vor 5 min" / "5 min ago", "in 2 h" (now and then in seconds)
datetime/cron.rs#
Source: lib/datetime/cron.rs
cron -- 5-field cron schedules evaluated in a time zone
Cron::parse("30 2 * * 1-5") minute hour day-of-month month day-of-week; fields: *, n,
a-b, a-b/n, */n, lists a,b; month names jan..dec, weekday
names sun..sat (0 and 7 = Sunday); @hourly @daily @midnight
@weekly @monthly @yearly @annually. Day of month and day of
week restricted both: either matches; one of them beginning
with "*" (also "*/2"): both must match (Vixie cron).
cron.next_after(ts, &zone) the first run after ts: a wall time in a DST gap runs at the
jump (right after the gap), a repeated wall time runs once
(its first occurrence); None when nothing matches within
5 years
cron.next_n(ts, &zone, n) the next n runs
cron.describe(en) "Täglich um 02:00", "Werktags um 02:00", "Alle 15 Minuten" …
Schedule helpers: daily(h, m), weekly(mask, h, m), monthly(day, h, m), hourly(m) build
the expressions of the usual choices (mask bit 0 = Monday)
struct Crona parsed cron expressionstruct CronErrora parse error: the field (0 minute .. 4 day of week, 5 = the whole expression) and whyfield: u8msg: &'static str
impl Cron
fn parse(s: &str) -> Result<Cron, CronError>parse an expression (see the module comment)fn as_str(&self) -> &strthe expression as writtenfn matches_date(&self, y: i32, m: u8, d: u8) -> boolwhether a date matches the month, day-of-month and day-of-week fieldsfn next_after(&self, after: Timestamp, zone: &Zone) -> Option<Timestamp>the first run strictly afterafterinzonefn next_n(&self, after: Timestamp, zone: &Zone, n: usize) -> Vec<Timestamp>the next n runs afterafterfn describe(&self, en: bool) -> Stringthe schedule in words (German, or English with en)fn daily(h: u8, m: u8) -> String"M H * * *"fn hourly(m: u8) -> String"M * * * *"fn weekly(mask: u8, h: u8, m: u8) -> String"M H * * days" with mask bit 0 = Monday .. bit 6 = Sunday (empty mask: every day)fn monthly(day: u8, h: u8, m: u8) -> String"M H D * *"fn classify(c: &Cron) -> (u8, u8, u8, u8, u8)the (kind, hour, minute, weekday mask, day of month) of an expression built by the helpers above: kind 1 hourly, 2 daily, 3 weekly, 4 monthly, 0 anything else (custom)
datetime/tz.rs#
Source: lib/datetime/tz.rs
tz -- time zones from POSIX TZ rules ("CET-1CEST,M3.5.0,M10.5.0/3")
Zone::utc() / Zone::by_name("Europe/Berlin") / Zone::from_windows("W. Europe Standard Time")
Zone::from_posix(name, rule) / Zone::fixed(name, offset_secs)
Zone::names() -> every IANA name of the table (sorted)
zone.offset_at(secs) -> UTC offset (seconds east), zone.abbrev_at(secs), zone.is_dst(secs)
zone.to_local(ts) -> Civil; zone.from_local(&civil) -> LocalResult:
Single(ts) | Ambiguous(first, second) (the repeated hour) | Gap { after } (the skipped
hour: `after` is the instant the clocks jumped, the first valid time after the gap)
A rule describes the zone from its last change on (the TZif footer of tools/gen_tz.py), so
times before that change use today's rule.
struct Zonea time zone: its name, standard offset and an optional daylight saving ruleenum LocalResultthe instants a local wall time stands forSingle(Timestamp)Ambiguous(Timestamp, Timestamp)-- the wall time occurs twice (the clocks went back): earlier and later instantGap { after: Timestamp }-- the wall time does not exist (the clocks jumped forward): the jump's instant
impl LocalResult
fn earliest(&self) -> Timestampthe earliest instant that stands for the wall time (a gap: the jump)
impl Zone
fn utc() -> ZoneUTCfn fixed(name: &str, offset_secs: i32) -> Zonea fixed offset (seconds east of UTC)fn from_posix(name: &str, rule: &str) -> Option<Zone>a zone from a POSIX TZ rulefn by_name(name: &str) -> Option<Zone>an IANA zone of the table ("Europe/Berlin"; "UTC", "Etc/UTC", "Z" also work)fn from_windows(key: &str) -> Option<Zone>the IANA zone of a Windows time zone key ("W. Europe Standard Time")fn windows_to_iana(key: &str) -> Option<&'static str>the IANA name of a Windows time zone keyfn names() -> impl Iterator<Item = &'static str>every IANA zone name of the table, sortedfn from_tzif(name: &str, data: &[u8]) -> Option<Zone>a zone from a TZif file (its footer rule; /etc/localtime), namednamefn name(&self) -> &strfn std_offset(&self) -> i32the standard offset (seconds east of UTC)fn is_dst(&self, secs: i64) -> boolwhether daylight saving time is in effect at an instantfn offset_at(&self, secs: i64) -> i32the UTC offset (seconds east) in effect at an instantfn abbrev_at(&self, secs: i64) -> &strthe abbreviation in effect at an instant ("CET" / "CEST")fn to_local(&self, ts: Timestamp) -> Civilthe wall time of an instantfn from_local(&self, c: &Civil) -> LocalResultthe instants of a wall time (seconds; nanos kept)