Skip to content
CryoCryo home
Stdlibtime

datetime

import std::time::datetime; · source

DateTime

type struct DateTime {
    year:   i64;
    month:  u32;
    day:    u32;
    hour:   u32;
    minute: u32;
    second: u32;

    static from_unix_secs(secs: i64) -> DateTime;
    static from_system_time(t: &SystemTime) -> DateTime;
    static now() -> DateTime;
    to_string(&this) -> String;
}

A broken-out UTC civil calendar reading, derived from a Unix timestamp or a SystemTime.

import std::time::datetime;

println(f"{DateTime::now()}");     // 2026-07-28T21:14:07Z

All fields are UTC and already normalized: month in 1–12, day in 1–31, hour in 0–23, minute and second in 0–59. year is the proleptic Gregorian year and may be negative for timestamps far before the Common Era. to_string() renders the same ISO-8601 text as the Display impl.

The conversion uses Howard Hinnant's civil_from_days algorithm, which is exact and branch-light across the whole proleptic Gregorian calendar — leap years, century rules, and pre-epoch timestamps all fall out of the arithmetic with no per-month tables. The day index and second-of-day are split with a floor division so a negative timestamp lands on the correct civil day; truncating toward zero would put −1 second on 1970-01-01 instead of 1969-12-31.

Only whole seconds are rendered. Any sub-second precision stays in the source SystemTime.

Trait implementations

implement trait Display for struct DateTime