pub struct Timestamp { /* private fields */ }Expand description
A timestamp value that can be serialized to and deserialized from JSON.
Timestamp is stored internally as (seconds: i64, nanos: u32), where
seconds counts (signed) seconds since the Unix epoch and nanos is
always in [0, 1_000_000_000). This matches the
google.protobuf.Timestamp convention and lets the type be
no_std-compatible. Negative whole-second values combined with a
positive nanos mean the same instant as the float
seconds + nanos / 1e9, e.g. (-5, 200_000_000) is -4.8s.
§Invariants
Every Timestamp value satisfies:
secondsis inSECONDS_MIN..=SECONDS_MAX, i.e. it represents an instant between0001-01-01T00:00:00Zand9999-12-31T23:59:59Zinclusive.nanosis in[0, 1_000_000_000).
All public constructors enforce these invariants — Timestamp::now,
Timestamp::from_unix, Timestamp::from_str, From<SystemTime>,
and the optional From<chrono::DateTime<Tz>> either validate, saturate,
or are infallible by construction. As a result, Buffer::format and
fmt::Display never fail.
With the default std feature, this type round-trips to and from
std::time::SystemTime efficiently — the conversion is just a
duration_since(UNIX_EPOCH) followed by storing the two fields
(saturating at the supported range bounds).
§Creating timestamps
// From Unix seconds + nanoseconds.
let ts = Timestamp::from_unix(1_641_006_000, 0).unwrap();
// Equivalent via the `TryFrom<(i64, u32)>` impl.
let ts = Timestamp::try_from((1_641_006_000i64, 0u32)).unwrap();
// With the `std` feature: from current system time.
let now = Timestamp::now();
let ts = Timestamp::from(std::time::SystemTime::now());§Inspecting timestamps
let ts = Timestamp::from_unix(1_641_006_000, 250_000_000).unwrap();
assert_eq!(ts.seconds(), 1_641_006_000);
assert_eq!(ts.subsec_nanos(), 250_000_000);Implementations§
Source§impl Timestamp
impl Timestamp
Sourcepub fn from_unix(seconds: i64, nanos: u32) -> Result<Self, TimestampError>
pub fn from_unix(seconds: i64, nanos: u32) -> Result<Self, TimestampError>
Constructs a Timestamp from Unix seconds and nanoseconds.
Returns TimestampError::OutOfRange if seconds is outside the
representable year 1..=9999 range, or if nanos >= 1_000_000_000.
§Examples
let ts = Timestamp::from_unix(0, 0).unwrap();
assert_eq!(ts.seconds(), 0);
assert_eq!(ts.subsec_nanos(), 0);Sourcepub fn seconds(&self) -> i64
pub fn seconds(&self) -> i64
Returns the (signed) seconds component, counted from the Unix epoch.
Sourcepub fn subsec_nanos(&self) -> u32
pub fn subsec_nanos(&self) -> u32
Returns the fractional-second component, in nanoseconds.
The returned value is always in [0, 1_000_000_000). The naming
matches std::time::Duration::subsec_nanos.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Timestamp
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Timestamp
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl From<&SystemTime> for Timestamp
Available on crate feature std only.Wraps a SystemTime reference as a Timestamp. See From<SystemTime>
for the saturation contract.
impl From<&SystemTime> for Timestamp
std only.Wraps a SystemTime reference as a Timestamp. See From<SystemTime>
for the saturation contract.
Source§fn from(value: &SystemTime) -> Self
fn from(value: &SystemTime) -> Self
Source§impl From<SystemTime> for Timestamp
Available on crate feature std only.Converts a SystemTime into a Timestamp by computing its offset from
the Unix epoch.
impl From<SystemTime> for Timestamp
std only.Converts a SystemTime into a Timestamp by computing its offset from
the Unix epoch.
SystemTime can in principle represent instants outside the
Timestamp range (year 1 through year 9999); such values are
saturated to the nearest in-range second. In practice this only
affects deliberately constructed SystemTimes; wall-clock times from
the system clock are well within range.
Source§fn from(value: SystemTime) -> Self
fn from(value: SystemTime) -> Self
Source§impl From<Timestamp> for SystemTime
Available on crate feature std only.Converts a Timestamp back into a SystemTime by adding (or
subtracting) its offset from the Unix epoch.
impl From<Timestamp> for SystemTime
std only.Converts a Timestamp back into a SystemTime by adding (or
subtracting) its offset from the Unix epoch.