Skip to main content

humantime/
wrapper.rs

1use std::fmt;
2use std::ops::Deref;
3use std::str::FromStr;
4use std::time::{Duration as StdDuration, SystemTime};
5
6use crate::date::{self, format_rfc3339, parse_rfc3339_weak};
7use crate::duration::{self, format_duration, parse_duration};
8
9/// A wrapper for duration that has `FromStr` implementation
10///
11/// This is useful if you want to use it somewhere where `FromStr` is
12/// expected.
13///
14/// See `parse_duration` for the description of the format.
15///
16/// # Example
17///
18/// ```
19/// use std::time::Duration;
20/// let x: Duration;
21/// x = "12h 5min 2ns".parse::<humantime::Duration>().unwrap().into();
22/// assert_eq!(x, Duration::new(12*3600 + 5*60, 2))
23/// ```
24///
25#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
26pub struct Duration(StdDuration);
27
28/// A wrapper for SystemTime that has `FromStr` implementation
29///
30/// This is useful if you want to use it somewhere where `FromStr` is
31/// expected.
32///
33/// See `parse_rfc3339_weak` for the description of the format. The "weak"
34/// format is used as it's more pemissive for human input as this is the
35/// expected use of the type (e.g. command-line parsing).
36///
37/// # Example
38///
39/// ```
40/// use std::time::SystemTime;
41/// let x: SystemTime;
42/// x = "2018-02-16T00:31:37Z".parse::<humantime::Timestamp>().unwrap().into();
43/// assert_eq!(humantime::format_rfc3339(x).to_string(), "2018-02-16T00:31:37Z");
44/// ```
45///
46#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
47pub struct Timestamp(SystemTime);
48
49impl Duration {
50    /// Create a new instance from a [`StdDuration`]. This can be used in a `const` context.
51    ///
52    /// ```rust
53    /// # use humantime::Duration;
54    /// const DEFAULT_TIMEOUT: Duration = Duration::new(core::time::Duration::from_secs(60));
55    /// ```
56    pub const fn new(value: StdDuration) -> Self {
57        Self(value)
58    }
59}
60
61impl AsRef<StdDuration> for Duration {
62    fn as_ref(&self) -> &StdDuration {
63        &self.0
64    }
65}
66
67impl Deref for Duration {
68    type Target = StdDuration;
69    fn deref(&self) -> &StdDuration {
70        &self.0
71    }
72}
73
74impl From<Duration> for StdDuration {
75    fn from(val: Duration) -> Self {
76        val.0
77    }
78}
79
80impl From<StdDuration> for Duration {
81    fn from(dur: StdDuration) -> Duration {
82        Duration(dur)
83    }
84}
85
86impl FromStr for Duration {
87    type Err = duration::Error;
88    fn from_str(s: &str) -> Result<Duration, Self::Err> {
89        parse_duration(s).map(Duration)
90    }
91}
92
93impl fmt::Display for Duration {
94    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
95        format_duration(self.0).fmt(f)
96    }
97}
98
99impl AsRef<SystemTime> for Timestamp {
100    fn as_ref(&self) -> &SystemTime {
101        &self.0
102    }
103}
104
105impl Deref for Timestamp {
106    type Target = SystemTime;
107    fn deref(&self) -> &SystemTime {
108        &self.0
109    }
110}
111
112impl From<Timestamp> for SystemTime {
113    fn from(val: Timestamp) -> Self {
114        val.0
115    }
116}
117
118impl From<SystemTime> for Timestamp {
119    fn from(dur: SystemTime) -> Timestamp {
120        Timestamp(dur)
121    }
122}
123
124impl FromStr for Timestamp {
125    type Err = date::Error;
126    fn from_str(s: &str) -> Result<Timestamp, Self::Err> {
127        parse_rfc3339_weak(s).map(Timestamp)
128    }
129}
130
131impl fmt::Display for Timestamp {
132    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
133        format_rfc3339(self.0).fmt(f)
134    }
135}