datetime
import std::time::datetime; · source
Types
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.
Properties
| Property | Type |
|---|---|
year | i64 |
month | u32 |
day | u32 |
hour | u32 |
minute | u32 |
second | u32 |
Constructors
static from_unix_secs(secs: i64) -> DateTime;
static from_unix_secs(secs: i64) -> DateTime;Break a whole-second Unix timestamp into its UTC calendar parts.
static from_system_time(t: &SystemTime) -> DateTime;
static from_system_time(t: &SystemTime) -> DateTime;The UTC calendar reading for a SystemTime (its whole-second Unix timestamp; any sub-second remainder is dropped).
static now() -> DateTime;
static now() -> DateTime;The current UTC date and time from the wall clock.
Methods
to_string(&this) -> String;
to_string(&this) -> String;Render as ISO-8601 UTC (YYYY-MM-DDThh:mm:ssZ) - the same text as the Display impl, available directly without an f-string.
Trait implementations
implement trait Display for struct DateTime
Constants
const SECS_PER_DAY: i64 = 86400;
const SECS_PER_HOUR: i64 = 3600;
const SECS_PER_MIN: i64 = 60;
const DAYS_SHIFT: i64 = 719468;
const DAYS_PER_ERA: i64 = 146097;
const YEARS_PER_ERA: i64 = 400;type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct DateTime {
year: i64;
month: u32;
day: u32;
hour: u32;
minute: u32;
second: u32;
}A UTC civil calendar reading. All fields are UTC and already normalized: month in 1..=12, day in 1..=31, hour in 0..=23, minute/second in 0..=59. year is the proleptic Gregorian year and may be negative for timestamps far before the Common Era. POD.
DateTime.year: i64DateTime.month: u32DateTime.day: u32DateTime.hour: u32DateTime.minute: u32DateTime.second: u32static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static DateTime::from_unix_secs(secs: i64) -> DateTimeBreak a whole-second Unix timestamp into its UTC calendar parts.
(parameter) secs: i64type struct DateTime {
year: i64;
month: u32;
day: u32;
hour: u32;
minute: u32;
second: u32;
}A UTC civil calendar reading. All fields are UTC and already normalized: month in 1..=12, day in 1..=31, hour in 0..=23, minute/second in 0..=59. year is the proleptic Gregorian year and may be negative for timestamps far before the Common Era. POD.
static DateTime::from_system_time(t: &SystemTime) -> DateTimeThe UTC calendar reading for a SystemTime (its whole-second Unix timestamp; any sub-second remainder is dropped).
(parameter) t: &SystemTimetype struct SystemTime {
secs: i64;
nanos: i64;
}A measurement of the wall clock (CLOCK_REALTIME) as a raw timespec. duration_since_epoch() yields a Unix timestamp. Subject to NTP steps and manual changes; do not use for elapsed-time measurement. POD.
static DateTime::now() -> DateTimeThe current UTC date and time from the wall clock.
DateTime::to_string(&this) -> StringRender as ISO-8601 UTC (YYYY-MM-DDThh:mm:ssZ) - the same text as the Display impl, available directly without an f-string.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
type struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringimport a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
static DateTime::now() -> DateTimeThe current UTC date and time from the wall clock.
std::time::datetimeconst NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const SECS_PER_DAY: i64 = 86400;const SECS_PER_HOUR: i64 = 3600;const SECS_PER_MIN: i64 = 60;const DAYS_SHIFT: i64 = 719468;const DAYS_PER_ERA: i64 = 146097;const YEARS_PER_ERA: i64 = 400;DateTime.month: u32DateTime.day: u32DateTime.hour: u32DateTime.minute: u32DateTime.second: u32DateTime.year: i64DateTime::to_string(&this) -> StringRender as ISO-8601 UTC (YYYY-MM-DDThh:mm:ssZ) - the same text as the Display impl, available directly without an f-string.
type trait Display {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}Display / Debug / Formatter.
std::fmt::displaystatic DateTime::from_unix_secs(secs: i64) -> DateTimeBreak a whole-second Unix timestamp into its UTC calendar parts.
std::time::datetimestatic DateTime::from_system_time(t: &SystemTime) -> DateTimeThe UTC calendar reading for a SystemTime (its whole-second Unix timestamp; any sub-second remainder is dropped).
implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referenceimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language reference