1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#[cfg(feature = "serde")]
use serde::{de, ser, Serializer};
use std::{
fmt,
ops::{Add, Sub},
time::{Duration, SystemTime, UNIX_EPOCH},
};
#[derive(Debug, PartialEq, Copy, Clone)]
pub struct Seconds(f64);
impl fmt::Display for Seconds {
fn fmt(
&self,
f: &mut fmt::Formatter,
) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl Seconds {
pub fn now() -> Self {
Self::from_duration(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default(),
)
}
pub fn trunc(self) -> Self {
Self(self.0.trunc())
}
fn from_duration(dur: Duration) -> Self {
Seconds(dur.as_secs() as f64 + (f64::from(dur.subsec_nanos()) / 1.0e9))
}
}
impl Default for Seconds {
fn default() -> Self {
Seconds::now()
}
}
impl Into<f64> for Seconds {
fn into(self) -> f64 {
let Seconds(secs) = self;
secs
}
}
impl Add<Duration> for Seconds {
type Output = Seconds;
fn add(
self,
rhs: Duration,
) -> Self::Output {
let lhs: Duration = self.into();
Seconds::from_duration(lhs + rhs)
}
}
impl Sub<Duration> for Seconds {
type Output = Seconds;
fn sub(
self,
rhs: Duration,
) -> Self::Output {
let lhs: Duration = self.into();
Seconds::from_duration(lhs - rhs)
}
}
impl Into<Duration> for Seconds {
fn into(self) -> Duration {
let Seconds(secs) = self;
Duration::new(secs.trunc() as u64, (secs.fract() * 1.0e9) as u32)
}
}
#[cfg(feature = "serde")]
struct SecondsVisitor;
#[cfg(feature = "serde")]
impl<'de> de::Visitor<'de> for SecondsVisitor {
type Value = Seconds;
fn expecting(
&self,
formatter: &mut fmt::Formatter,
) -> fmt::Result {
formatter.write_str("floating point seconds")
}
fn visit_f64<E>(
self,
value: f64,
) -> Result<Seconds, E>
where
E: de::Error,
{
Ok(Seconds(value))
}
}
#[cfg(feature = "serde")]
impl ser::Serialize for Seconds {
fn serialize<S>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let Seconds(seconds) = self;
serializer.serialize_f64(*seconds)
}
}
#[cfg(feature = "serde")]
impl<'de> de::Deserialize<'de> for Seconds {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
deserializer.deserialize_f64(SecondsVisitor)
}
}
#[cfg(test)]
mod tests {
use super::Seconds;
use std::time::Duration;
#[test]
fn seconds_default() {
let (now, default) = (Seconds::default(), Seconds::now());
assert_eq!(now.trunc(), default.trunc());
}
#[test]
fn seconds_deref() {
let secs = Seconds(1_545_136_342.711_932);
let _f: f64 = secs.into();
}
#[test]
fn seconds_display() {
let secs = Seconds(1_545_136_342.711_932);
assert_eq!(format!("{}", secs), "1545136342.711932");
}
#[test]
fn seconds_duration_interop() {
let secs = Seconds(1_545_136_342.711_932);
let duration: Duration = secs.into();
assert_eq!(duration.as_secs(), 1_545_136_342);
}
#[test]
fn seconds_add_duration() {
let secs = Seconds(1_545_136_342.711_932);
assert_eq!(
secs + Duration::from_secs(1),
Seconds(1_545_136_343.711_932)
);
}
#[test]
fn seconds_sub_duration() {
let secs = Seconds(1_545_136_342.711_932);
assert_eq!(
secs - Duration::from_secs(1),
Seconds(1_545_136_341.711_932)
);
}
#[cfg(feature = "serde")]
#[test]
fn seconds_serialize() {
assert_eq!(
serde_json::to_string(&Seconds(1_545_136_342.711_932)).expect("failed to serialize"),
"1545136342.711932"
);
}
#[cfg(feature = "serde")]
#[test]
fn seconds_deserialize_floats() {
assert_eq!(
serde_json::from_slice::<Seconds>(b"1545136342.711932").expect("failed to serialize"),
Seconds(1_545_136_342.711_932)
);
}
#[cfg(feature = "serde")]
#[test]
fn seconds_fails_to_deserialize() {
match serde_json::from_slice::<Seconds>(b"{\"foo\":\"bar\"}") {
Err(err) => assert_eq!(
format!("{}", err),
"invalid type: map, expected floating point seconds at line 1 column 0"
),
Ok(other) => panic!("unexpected result {}", other),
}
}
}