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#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
26pub struct Duration(StdDuration);
27
28#[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}