#[non_exhaustive]pub struct Duration { /* private fields */ }Expand description
Well-known duration representation for Google APIs.
§Examples
let d = Duration::try_from("12.34s")?;
assert_eq!(d.seconds(), 12);
assert_eq!(d.nanos(), 340_000_000);
assert_eq!(d, Duration::new(12, 340_000_000)?);
assert_eq!(d, Duration::clamp(12, 340_000_000));
A Duration represents a signed, fixed-length span of time represented as a count of seconds and fractions of seconds at nanosecond resolution. It is independent of any calendar and concepts like “day” or “month”. It is related to Timestamp in that the difference between two Timestamp values is a Duration and it can be added or subtracted from a Timestamp. Range is approximately +-10,000 years.
§JSON Mapping
In JSON format, the Duration type is encoded as a string rather than an object, where the string ends in the suffix “s” (indicating seconds) and is preceded by the number of seconds, with nanoseconds expressed as fractional seconds. For example, 3 seconds with 0 nanoseconds should be encoded in JSON format as “3s”, while 3 seconds and 1 nanosecond should be expressed in JSON format as “3.000000001s”, and 3 seconds and 1 microsecond should be expressed in JSON format as “3.000001s”.
Implementations§
Source§impl Duration
impl Duration
Sourcepub const MAX_SECONDS: i64 = 315_576_000_000i64
pub const MAX_SECONDS: i64 = 315_576_000_000i64
The maximum value for the seconds component, approximately 10,000 years.
Sourcepub const MIN_SECONDS: i64 = -315_576_000_000i64
pub const MIN_SECONDS: i64 = -315_576_000_000i64
The minimum value for the seconds component, approximately -10,000 years.
Sourcepub fn new(seconds: i64, nanos: i32) -> Result<Self, DurationError>
pub fn new(seconds: i64, nanos: i32) -> Result<Self, DurationError>
Creates a Duration from the seconds and nanoseconds component.
§Examples
let d = Duration::new(12, 340_000_000)?;
assert_eq!(String::from(d), "12.34s");
let d = Duration::new(-12, -340_000_000)?;
assert_eq!(String::from(d), "-12.34s");§Examples: invalid inputs
let d = Duration::new(12, 2_000_000_000);
assert!(matches!(d, Err(DurationError::OutOfRange)));
let d = Duration::new(-12, 340_000_000);
assert!(matches!(d, Err(DurationError::MismatchedSigns)));This function validates the seconds and nanos components and returns
an error if either are out of range or their signs do not match.
Consider using clamp() to add nanoseconds to seconds
with carry.
§Parameters
seconds- the seconds in the interval.nanos- the nanoseconds added to the interval.
Sourcepub fn clamp(seconds: i64, nanos: i32) -> Self
pub fn clamp(seconds: i64, nanos: i32) -> Self
Create a normalized, clamped Duration.
§Examples
let d = Duration::clamp(12, 340_000_000);
assert_eq!(String::from(d), "12.34s");
let d = Duration::clamp(10, 2_000_000_000);
assert_eq!(String::from(d), "12s");Durations must be in the [-10_000, +10_000] year range, the nanoseconds field must be in the [-999_999_999, +999_999_999] range, and the seconds and nanosecond fields must have the same sign. This function creates a new Duration instance clamped to those ranges.
The function effectively adds the nanoseconds part (with carry) to the seconds part, with saturation.
§Parameters
seconds- the seconds in the interval.nanos- the nanoseconds added to the interval.
Trait Implementations§
Source§impl From<Duration> for Duration
Available on crate feature time only.Convert from Duration to time::Duration.
impl From<Duration> for Duration
time only.Convert from Duration to time::Duration.
Source§impl From<Duration> for String
impl From<Duration> for String
§Example
let d = Duration::clamp(12, 340_000_000);
assert_eq!(String::from(d), "12.34s");Source§impl From<Duration> for Duration
Available on crate feature chrono only.Converts from Duration to chrono::Duration.
impl From<Duration> for Duration
chrono only.Converts from Duration to chrono::Duration.
§Example
let d = chrono::Duration::from(Duration::clamp(12, 340_000_000));
assert_eq!(d.num_seconds(), 12);
assert_eq!(d.subsec_nanos(), 340_000_000);Source§impl PartialOrd for Duration
impl PartialOrd for Duration
Source§impl TryFrom<&String> for Duration
Converts the string representation of a duration to Duration.
impl TryFrom<&String> for Duration
Converts the string representation of a duration to Duration.
§Example
let s = "12.34s".to_string();
let d = Duration::try_from(&s)?;
assert_eq!(d.seconds(), 12);
assert_eq!(d.nanos(), 340_000_000);Source§impl TryFrom<&str> for Duration
Converts the string representation of a duration to Duration.
impl TryFrom<&str> for Duration
Converts the string representation of a duration to Duration.
§Example
let d = Duration::try_from("12.34s")?;
assert_eq!(d.seconds(), 12);
assert_eq!(d.nanos(), 340_000_000);Source§impl TryFrom<Duration> for Duration
Convert from std::time::Duration to Duration.
impl TryFrom<Duration> for Duration
Convert from std::time::Duration to Duration.
§Example
let d = Duration::try_from(std::time::Duration::from_secs(123))?;
assert_eq!(d.seconds(), 123);
assert_eq!(d.nanos(), 0);Source§impl TryFrom<Duration> for Duration
Convert from Duration to std::time::Duration.
impl TryFrom<Duration> for Duration
Convert from Duration to std::time::Duration.
Returns an error if value is negative, as std::time::Duration cannot
represent negative durations.
§Example
let d = Duration::new(12, 340_000_000)?;
let duration = std::time::Duration::try_from(d)?;
assert_eq!(duration.as_secs(), 12);
assert_eq!(duration.subsec_nanos(), 340_000_000);Source§impl TryFrom<Duration> for Duration
Available on crate feature time only.Convert from time::Duration to Duration.
impl TryFrom<Duration> for Duration
time only.Convert from time::Duration to Duration.
This conversion may fail if the time::Duration value is out of range.
§Example
let d = Duration::try_from(time::Duration::new(12, 340_000_000))?;
assert_eq!(d.seconds(), 12);
assert_eq!(d.nanos(), 340_000_000);Source§impl TryFrom<TimeDelta> for Duration
Available on crate feature chrono only.Converts from chrono::Duration to Duration.
impl TryFrom<TimeDelta> for Duration
chrono only.Converts from chrono::Duration to Duration.
The conversion may fail if the input value is out of range.
§Example
let d = Duration::try_from(chrono::Duration::new(12, 340_000_000).unwrap())?;
assert_eq!(d.seconds(), 12);
assert_eq!(d.nanos(), 340_000_000);