#[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_000
pub const MAX_SECONDS: i64 = 315_576_000_000
The maximum value for the seconds component, approximately 10,000 years.
Sourcepub const MIN_SECONDS: i64
pub const MIN_SECONDS: i64
The minimum value for the seconds component, approximately -10,000 years.
Sourcepub fn new(seconds: i64, nanos: i32) -> Result<Duration, DurationError>
pub fn new(seconds: i64, nanos: i32) -> Result<Duration, 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) -> Duration
pub fn clamp(seconds: i64, nanos: i32) -> Duration
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§
impl Copy for Duration
Source§impl PartialOrd for Duration
impl PartialOrd for Duration
impl StructuralPartialEq 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<SignedDuration> for Duration
Available on crate feature time only.Convert from time::Duration to Duration.
impl TryFrom<SignedDuration> 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§type Error = DurationError
type Error = DurationError
Source§fn try_from(
value: SignedDuration,
) -> Result<Duration, <Duration as TryFrom<SignedDuration>>::Error>
fn try_from( value: SignedDuration, ) -> Result<Duration, <Duration as TryFrom<SignedDuration>>::Error>
Auto Trait Implementations§
impl Forget for Duration
impl Freeze for Duration
impl Move for Duration
impl RefUnwindSafe for Duration
impl Send for Duration
impl Sync for Duration
impl Unpin for Duration
impl UnsafeUnpin for Duration
impl UnwindSafe for Duration
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request