pub struct Instant { /* private fields */ }Expand description
A SOML instant: a point in time with nanosecond precision, from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z.
This is a UTC timestamp, not a monotonic clock like std::time::Instant, so it is meant to be used qualified, as soml::Instant.
The offset a document writes an instant with is not part of the value, so 2026-09-19T21:00:00+07:00 and 2026-09-19T14:00:00Z are the same instant. Display writes the canonical form, which is always UTC.
let instant: soml::Instant = "2026-09-19T21:00:00.5+07:00".parse()?;
assert_eq!(instant.to_string(), "2026-09-19T14:00:00.5Z");
assert_eq!(instant.unix_seconds(), 1_789_826_400);
assert_eq!(instant.nanoseconds(), 500_000_000);With serde, it reads only from a SOML instant, not from a string, and it is written as a SOML instant. Other formats, such as JSON, see its canonical text.
Implementations§
Source§impl Instant
impl Instant
Sourcepub const fn from_unix(seconds: i64, nanoseconds: u32) -> Option<Self>
pub const fn from_unix(seconds: i64, nanoseconds: u32) -> Option<Self>
Creates an instant from whole seconds since 1970-01-01T00:00:00Z, and nanoseconds after them.
Returns None when nanoseconds is one billion or more, or when the instant is outside the years 0001 to 9999.
let instant = soml::Instant::from_unix(0, 0).unwrap();
assert_eq!(instant.to_string(), "1970-01-01T00:00:00Z");
assert!(soml::Instant::from_unix(-62_135_596_801, 0).is_none());Sourcepub const fn from_unix_nanoseconds(nanoseconds: i128) -> Option<Self>
pub const fn from_unix_nanoseconds(nanoseconds: i128) -> Option<Self>
Creates an instant from nanoseconds since 1970-01-01T00:00:00Z. Returns None when the instant is outside the years 0001 to 9999.
let instant = soml::Instant::from_unix_nanoseconds(1_500_000_000).unwrap();
assert_eq!(instant.to_string(), "1970-01-01T00:00:01.5Z");Sourcepub const fn unix_nanoseconds(self) -> i128
pub const fn unix_nanoseconds(self) -> i128
Nanoseconds since 1970-01-01T00:00:00Z. Negative for an instant before 1970.
Sourcepub const fn unix_seconds(self) -> i64
pub const fn unix_seconds(self) -> i64
Whole seconds since 1970-01-01T00:00:00Z. Negative for an instant before 1970, which is then made later by nanoseconds().
Sourcepub const fn nanoseconds(self) -> u32
pub const fn nanoseconds(self) -> u32
The nanoseconds after unix_seconds(), from 0 to 999,999,999.
Trait Implementations§
impl Copy for Instant
Source§impl<'de> Deserialize<'de> for Instant
impl<'de> Deserialize<'de> for Instant
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl Display for Instant
Writes the canonical form: UTC with Z, and a fraction without trailing zeros, left out when it is zero.
impl Display for Instant
Writes the canonical form: UTC with Z, and a fraction without trailing zeros, left out when it is zero.
impl Eq for Instant
Source§impl FromStr for Instant
Reads an instant in SOML syntax, with any offset.
impl FromStr for Instant
Reads an instant in SOML syntax, with any offset.
let instant: soml::Instant = "2026-09-19T14:00:00Z".parse()?;
assert_eq!(instant.unix_seconds(), 1_789_826_400);
assert!("2026-09-19".parse::<soml::Instant>().is_err());