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 Duration {
50 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}