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 AsRef<StdDuration> for Duration {
50    fn as_ref(&self) -> &StdDuration {
51        &self.0
52    }
53}
54
55impl Deref for Duration {
56    type Target = StdDuration;
57    fn deref(&self) -> &StdDuration {
58        &self.0
59    }
60}
61
62impl From<Duration> for StdDuration {
63    fn from(val: Duration) -> Self {
64        val.0
65    }
66}
67
68impl From<StdDuration> for Duration {
69    fn from(dur: StdDuration) -> Duration {
70        Duration(dur)
71    }
72}
73
74impl FromStr for Duration {
75    type Err = duration::Error;
76    fn from_str(s: &str) -> Result<Duration, Self::Err> {
77        parse_duration(s).map(Duration)
78    }
79}
80
81impl fmt::Display for Duration {
82    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83        format_duration(self.0).fmt(f)
84    }
85}
86
87impl AsRef<SystemTime> for Timestamp {
88    fn as_ref(&self) -> &SystemTime {
89        &self.0
90    }
91}
92
93impl Deref for Timestamp {
94    type Target = SystemTime;
95    fn deref(&self) -> &SystemTime {
96        &self.0
97    }
98}
99
100impl From<Timestamp> for SystemTime {
101    fn from(val: Timestamp) -> Self {
102        val.0
103    }
104}
105
106impl From<SystemTime> for Timestamp {
107    fn from(dur: SystemTime) -> Timestamp {
108        Timestamp(dur)
109    }
110}
111
112impl FromStr for Timestamp {
113    type Err = date::Error;
114    fn from_str(s: &str) -> Result<Timestamp, Self::Err> {
115        parse_rfc3339_weak(s).map(Timestamp)
116    }
117}
118
119impl fmt::Display for Timestamp {
120    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
121        format_rfc3339(self.0).fmt(f)
122    }
123}