pub struct Duration { /* private fields */ }Expand description
A SOML duration: a signed length of time, as a whole number of nanoseconds in the i64 range, which is about 292 years either way.
Unlike std::time::Duration, it can be negative, so it is meant to be used qualified, as soml::Duration.
Display writes the canonical form, in hours, minutes, and seconds with a fraction:
let duration: soml::Duration = "90m".parse()?;
assert_eq!(duration.to_string(), "1h30m");
let duration = soml::Duration::from_nanoseconds(-1_500_000_000);
assert_eq!(duration.to_string(), "-1.5s");With serde, it reads only from a SOML duration, not from a string, and it is written as a SOML duration. Other formats, such as JSON, see its canonical text.
Implementations§
Source§impl Duration
impl Duration
Sourcepub const fn from_nanoseconds(nanoseconds: i64) -> Self
pub const fn from_nanoseconds(nanoseconds: i64) -> Self
Creates a duration from a signed count of nanoseconds.
Sourcepub const fn nanoseconds(self) -> i64
pub const fn nanoseconds(self) -> i64
The signed count of nanoseconds.
Sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Whether the duration is less than zero.
Trait Implementations§
impl Copy for Duration
Source§impl<'de> Deserialize<'de> for Duration
impl<'de> Deserialize<'de> for Duration
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 Duration
Writes the canonical form: - when negative, then the hours, minutes, and seconds, each left out when zero, with the seconds’ fraction written like an instant’s. Zero is 0s.
impl Display for Duration
Writes the canonical form: - when negative, then the hours, minutes, and seconds, each left out when zero, with the seconds’ fraction written like an instant’s. Zero is 0s.
impl Eq for Duration
Source§impl From<Duration> for SignedDuration
impl From<Duration> for SignedDuration
Source§impl FromStr for Duration
Reads a duration in SOML syntax.
impl FromStr for Duration
Reads a duration in SOML syntax.
let duration: soml::Duration = "1.5s".parse()?;
assert_eq!(duration.nanoseconds(), 1_500_000_000);
assert!("1d".parse::<soml::Duration>().is_err());