Struct cld::ClDuration
source · pub struct ClDuration { /* private fields */ }
Expand description
Command line duration
Notes
-
Display
implementation is only for human presentation. The result should not be parsed. -
Support 2 forms for parsing from strings:
- With unit.
hour:minute:second
.
Length of the string must be equal to or smaller than 64 bytes. It’s for protection against flood attack.
Units
Unit | Description |
---|---|
ns | Nanosecond |
mc | Microsecond |
ms | Millisecond |
s | Second |
m | Minute |
h | Hour |
d | Day |
w | Week |
Examples
use core::{
str::FromStr,
time::Duration,
};
use cld::ClDuration;
assert_eq!(ClDuration::from_str("9s")?, Duration::from_secs(9));
assert_eq!(ClDuration::from_str("2d")?, Duration::from_secs(60 * 60 * 24 * 2));
assert_eq!(
ClDuration::from_str("99:59:59")?,
Duration::from_secs((99 * 60 * 60) + (59 * 60) + 59),
);
Methods from Deref<Target = Duration>§
pub const SECOND: Duration = Duration::from_secs(1)
pub const MILLISECOND: Duration = Duration::from_millis(1)
pub const MICROSECOND: Duration = Duration::from_micros(1)
pub const NANOSECOND: Duration = Duration::from_nanos(1)
pub const ZERO: Duration = Duration::from_nanos(0)
pub const MAX: Duration = Duration::new(u64::MAX, NANOS_PER_SEC - 1)
1.53.0 · sourcepub fn is_zero(&self) -> bool
pub fn is_zero(&self) -> bool
Returns true if this Duration
spans no time.
Examples
use std::time::Duration;
assert!(Duration::ZERO.is_zero());
assert!(Duration::new(0, 0).is_zero());
assert!(Duration::from_nanos(0).is_zero());
assert!(Duration::from_secs(0).is_zero());
assert!(!Duration::new(1, 1).is_zero());
assert!(!Duration::from_nanos(1).is_zero());
assert!(!Duration::from_secs(1).is_zero());
1.3.0 · sourcepub fn as_secs(&self) -> u64
pub fn as_secs(&self) -> u64
Returns the number of whole seconds contained by this Duration
.
The returned value does not include the fractional (nanosecond) part of the
duration, which can be obtained using subsec_nanos
.
Examples
use std::time::Duration;
let duration = Duration::new(5, 730023852);
assert_eq!(duration.as_secs(), 5);
To determine the total number of seconds represented by the Duration
including the fractional part, use as_secs_f64
or as_secs_f32
1.27.0 · sourcepub fn subsec_millis(&self) -> u32
pub fn subsec_millis(&self) -> u32
Returns the fractional part of this Duration
, in whole milliseconds.
This method does not return the length of the duration when represented by milliseconds. The returned number always represents a fractional portion of a second (i.e., it is less than one thousand).
Examples
use std::time::Duration;
let duration = Duration::from_millis(5432);
assert_eq!(duration.as_secs(), 5);
assert_eq!(duration.subsec_millis(), 432);
1.27.0 · sourcepub fn subsec_micros(&self) -> u32
pub fn subsec_micros(&self) -> u32
Returns the fractional part of this Duration
, in whole microseconds.
This method does not return the length of the duration when represented by microseconds. The returned number always represents a fractional portion of a second (i.e., it is less than one million).
Examples
use std::time::Duration;
let duration = Duration::from_micros(1_234_567);
assert_eq!(duration.as_secs(), 1);
assert_eq!(duration.subsec_micros(), 234_567);
1.3.0 · sourcepub fn subsec_nanos(&self) -> u32
pub fn subsec_nanos(&self) -> u32
Returns the fractional part of this Duration
, in nanoseconds.
This method does not return the length of the duration when represented by nanoseconds. The returned number always represents a fractional portion of a second (i.e., it is less than one billion).
Examples
use std::time::Duration;
let duration = Duration::from_millis(5010);
assert_eq!(duration.as_secs(), 5);
assert_eq!(duration.subsec_nanos(), 10_000_000);
1.33.0 · sourcepub fn as_millis(&self) -> u128
pub fn as_millis(&self) -> u128
Returns the total number of whole milliseconds contained by this Duration
.
Examples
use std::time::Duration;
let duration = Duration::new(5, 730023852);
assert_eq!(duration.as_millis(), 5730);
1.33.0 · sourcepub fn as_micros(&self) -> u128
pub fn as_micros(&self) -> u128
Returns the total number of whole microseconds contained by this Duration
.
Examples
use std::time::Duration;
let duration = Duration::new(5, 730023852);
assert_eq!(duration.as_micros(), 5730023);
1.33.0 · sourcepub fn as_nanos(&self) -> u128
pub fn as_nanos(&self) -> u128
Returns the total number of nanoseconds contained by this Duration
.
Examples
use std::time::Duration;
let duration = Duration::new(5, 730023852);
assert_eq!(duration.as_nanos(), 5730023852);
1.38.0 · sourcepub fn as_secs_f64(&self) -> f64
pub fn as_secs_f64(&self) -> f64
Returns the number of seconds contained by this Duration
as f64
.
The returned value does include the fractional (nanosecond) part of the duration.
Examples
use std::time::Duration;
let dur = Duration::new(2, 700_000_000);
assert_eq!(dur.as_secs_f64(), 2.7);
1.38.0 · sourcepub fn as_secs_f32(&self) -> f32
pub fn as_secs_f32(&self) -> f32
Returns the number of seconds contained by this Duration
as f32
.
The returned value does include the fractional (nanosecond) part of the duration.
Examples
use std::time::Duration;
let dur = Duration::new(2, 700_000_000);
assert_eq!(dur.as_secs_f32(), 2.7);
Trait Implementations§
source§impl Clone for ClDuration
impl Clone for ClDuration
source§fn clone(&self) -> ClDuration
fn clone(&self) -> ClDuration
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for ClDuration
impl Debug for ClDuration
source§impl Deref for ClDuration
impl Deref for ClDuration
source§impl Display for ClDuration
impl Display for ClDuration
source§impl From<ClDuration> for Duration
impl From<ClDuration> for Duration
source§fn from(cld: ClDuration) -> Self
fn from(cld: ClDuration) -> Self
source§impl From<Duration> for ClDuration
impl From<Duration> for ClDuration
source§impl FromStr for ClDuration
impl FromStr for ClDuration
source§impl Hash for ClDuration
impl Hash for ClDuration
source§impl Ord for ClDuration
impl Ord for ClDuration
source§fn cmp(&self, other: &ClDuration) -> Ordering
fn cmp(&self, other: &ClDuration) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
source§impl PartialEq<ClDuration> for ClDuration
impl PartialEq<ClDuration> for ClDuration
source§fn eq(&self, other: &ClDuration) -> bool
fn eq(&self, other: &ClDuration) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<ClDuration> for Duration
impl PartialEq<ClDuration> for Duration
source§fn eq(&self, cld: &ClDuration) -> bool
fn eq(&self, cld: &ClDuration) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<Duration> for ClDuration
impl PartialEq<Duration> for ClDuration
source§impl PartialOrd<ClDuration> for ClDuration
impl PartialOrd<ClDuration> for ClDuration
source§fn partial_cmp(&self, other: &ClDuration) -> Option<Ordering>
fn partial_cmp(&self, other: &ClDuration) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<ClDuration> for Duration
impl PartialOrd<ClDuration> for Duration
source§fn partial_cmp(&self, cld: &ClDuration) -> Option<Ordering>
fn partial_cmp(&self, cld: &ClDuration) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<Duration> for ClDuration
impl PartialOrd<Duration> for ClDuration
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more