JFLib

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 Timestamp a point in time: seconds since 1970-01-01 00:00:00 UTC and nanoseconds
    • secs: i64
    • nanos: u32

impl Timestamp

  • const fn from_secs(secs: i64) -> Timestamp

  • const fn from_nanos(ns: i64) -> Timestamp from nanoseconds since the epoch (plat::unix_time_ns)

  • const fn from_millis(ms: i64) -> Timestamp

  • const fn as_millis(&self) -> i64

  • const fn add_secs(&self, s: i64) -> Timestamp

  • struct Civil a calendar date and wall time without a zone

    • year: i32
    • month: u8
    • day: u8
    • hour: u8
    • minute: u8
    • second: u8
    • nanos: u32

impl Civil

  • const fn new(year: i32, month: u8, day: u8, hour: u8, minute: u8, second: u8) -> Civil

  • const fn date(year: i32, month: u8, day: u8) -> Civil

  • fn from_unix(secs: i64) -> Civil the civil time of seconds since the epoch read as UTC

  • fn to_unix(&self) -> i64 seconds since the epoch of this wall time read as UTC

  • fn weekday(&self) -> u8 0 = Monday .. 6 = Sunday

  • fn is_valid(&self) -> bool

  • fn add_days(&self, days: i64) -> Civil the same wall time days days later

  • fn is_leap(y: i32) -> bool

  • fn days_in_month(y: i32, m: u8) -> u8 days of month m (1..12)

  • fn days_from_civil(y: i32, m: u8, d: u8) -> i64 days 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-01

  • fn weekday(y: i32, m: u8, d: u8) -> u8 weekday of a date: 0 = Monday .. 6 = Sunday

  • fn iso_weeks_in_year(y: i32) -> u8 the 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 52

  • fn 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 week week of week-numbering year y -> (year, month, day); a week beyond the year's last counts on into the next year

  • fn 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) -> String the timestamp as local time with its UTC offset: "2026-10-12T21:58:00+02:00"

  • fn parse_rfc3339(s: &str) -> Option<Timestamp> parse YYYY-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 zone

  • fn 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) -> String a 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) -> String a 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 Cron a parsed cron expression

  • struct CronError a parse error: the field (0 minute .. 4 day of week, 5 = the whole expression) and why

    • field: u8
    • msg: &'static str

impl Cron

  • fn parse(s: &str) -> Result<Cron, CronError> parse an expression (see the module comment)

  • fn as_str(&self) -> &str the expression as written

  • fn matches_date(&self, y: i32, m: u8, d: u8) -> bool whether a date matches the month, day-of-month and day-of-week fields

  • fn next_after(&self, after: Timestamp, zone: &Zone) -> Option<Timestamp> the first run strictly after after in zone

  • fn next_n(&self, after: Timestamp, zone: &Zone, n: usize) -> Vec<Timestamp> the next n runs after after

  • fn describe(&self, en: bool) -> String the 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 Zone a time zone: its name, standard offset and an optional daylight saving rule

  • enum LocalResult the instants a local wall time stands for

    • Single(Timestamp)
    • Ambiguous(Timestamp, Timestamp) -- the wall time occurs twice (the clocks went back): earlier and later instant
    • Gap { after: Timestamp } -- the wall time does not exist (the clocks jumped forward): the jump's instant

impl LocalResult

  • fn earliest(&self) -> Timestamp the earliest instant that stands for the wall time (a gap: the jump)

impl Zone

  • fn utc() -> Zone UTC
  • fn fixed(name: &str, offset_secs: i32) -> Zone a fixed offset (seconds east of UTC)
  • fn from_posix(name: &str, rule: &str) -> Option<Zone> a zone from a POSIX TZ rule
  • fn 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 key
  • fn names() -> impl Iterator<Item = &'static str> every IANA zone name of the table, sorted
  • fn from_tzif(name: &str, data: &[u8]) -> Option<Zone> a zone from a TZif file (its footer rule; /etc/localtime), named name
  • fn name(&self) -> &str
  • fn std_offset(&self) -> i32 the standard offset (seconds east of UTC)
  • fn is_dst(&self, secs: i64) -> bool whether daylight saving time is in effect at an instant
  • fn offset_at(&self, secs: i64) -> i32 the UTC offset (seconds east) in effect at an instant
  • fn abbrev_at(&self, secs: i64) -> &str the abbreviation in effect at an instant ("CET" / "CEST")
  • fn to_local(&self, ts: Timestamp) -> Civil the wall time of an instant
  • fn from_local(&self, c: &Civil) -> LocalResult the instants of a wall time (seconds; nanos kept)

Source: docs/API.md in the JFLib repository.