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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
use std::{
    mem,
    ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign},
};

const NANOS_PER_MICRO: i64 = 1_000;
const NANOS_PER_MILLI: i64 = NANOS_PER_MICRO * 1_000;
const NANOS_PER_SEC: i64 = NANOS_PER_MILLI * 1_000;

fn zero_std_instant() -> std::time::Instant {
    if cfg!(unix) || cfg!(windows) {
        // https://github.com/rust-lang/rust/blob/master/src/libstd/sys/unix/time.rs
        // https://github.com/rust-lang/rust/blob/master/src/libstd/sys/windows/time.rs
        unsafe { mem::zeroed() }
    } else {
        unimplemented!()
    }
}

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(transparent)]
pub struct Duration {
    pub nanos: i64,
}

impl Duration {
    pub const fn new(nanos: i64) -> Self {
        Self { nanos }
    }

    pub const fn zero() -> Self {
        Self::new(0)
    }

    pub const fn from_secs(secs: i64) -> Self {
        Self::new(secs * NANOS_PER_SEC)
    }

    pub const fn from_millis(millis: i64) -> Self {
        Self::new(millis * NANOS_PER_MILLI)
    }

    pub const fn from_micros(micros: i64) -> Self {
        Self::new(micros * NANOS_PER_MICRO)
    }

    pub fn from_secs_f32(secs: f32) -> Self {
        Self::new((secs * NANOS_PER_SEC as f32).round() as i64)
    }

    pub fn from_secs_f64(secs: f64) -> Self {
        Self::new((secs * NANOS_PER_SEC as f64).round() as i64)
    }

    pub fn as_secs_f32(self) -> f32 {
        self.nanos as f32 / NANOS_PER_SEC as f32
    }

    pub fn as_secs_f64(self) -> f64 {
        self.nanos as f64 / NANOS_PER_SEC as f64
    }

    pub fn div_duration_f32(self, rhs: Self) -> f32 {
        self.as_secs_f32() / rhs.as_secs_f32()
    }

    pub fn div_duration_f64(self, rhs: Self) -> f64 {
        self.as_secs_f64() / rhs.as_secs_f64()
    }

    pub fn clamp(self, min: Duration, max: Duration) -> Self {
        assert!(min <= max);
        if self < min {
            min
        } else if self > max {
            max
        } else {
            self
        }
    }

    pub fn from_std_duration(std: std::time::Duration) -> Self {
        let result = std.as_nanos();
        debug_assert!(result <= i64::max_value() as u128);
        Self::new(result as _)
    }

    pub fn into_std_duration(self) -> std::time::Duration {
        debug_assert!(self.nanos >= 0);
        std::time::Duration::from_nanos(self.nanos as _)
    }
}

impl From<std::time::Duration> for Duration {
    fn from(std: std::time::Duration) -> Self {
        Self::from_std_duration(std)
    }
}

impl Into<std::time::Duration> for Duration {
    fn into(self) -> std::time::Duration {
        self.into_std_duration()
    }
}

impl Add for Duration {
    type Output = Self;
    fn add(self, rhs: Self) -> Self {
        Self::new(self.nanos + rhs.nanos)
    }
}

impl AddAssign for Duration {
    fn add_assign(&mut self, rhs: Duration) {
        *self = *self + rhs
    }
}

impl Sub for Duration {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self {
        Self::new(self.nanos - rhs.nanos)
    }
}

impl SubAssign for Duration {
    fn sub_assign(&mut self, rhs: Duration) {
        *self = *self - rhs
    }
}

impl Neg for Duration {
    type Output = Self;
    fn neg(self) -> Self {
        Duration::zero() - self
    }
}

impl Mul<i32> for Duration {
    type Output = Self;
    fn mul(self, rhs: i32) -> Self {
        Self::new(self.nanos * rhs as i64)
    }
}

impl Mul<Duration> for i32 {
    type Output = Duration;
    fn mul(self, rhs: Duration) -> Duration {
        rhs * self
    }
}

impl MulAssign<i32> for Duration {
    fn mul_assign(&mut self, rhs: i32) {
        *self = *self * rhs
    }
}

impl Mul<f32> for Duration {
    type Output = Self;
    fn mul(self, rhs: f32) -> Self {
        Self::from_secs_f32(self.as_secs_f32() * rhs)
    }
}

impl Mul<Duration> for f32 {
    type Output = Duration;
    fn mul(self, rhs: Duration) -> Duration {
        rhs * self
    }
}

impl MulAssign<f32> for Duration {
    fn mul_assign(&mut self, rhs: f32) {
        *self = *self * rhs
    }
}

impl Mul<f64> for Duration {
    type Output = Self;
    fn mul(self, rhs: f64) -> Self {
        Self::from_secs_f64(self.as_secs_f64() * rhs)
    }
}

impl Mul<Duration> for f64 {
    type Output = Duration;
    fn mul(self, rhs: Duration) -> Duration {
        rhs * self
    }
}

impl MulAssign<f64> for Duration {
    fn mul_assign(&mut self, rhs: f64) {
        *self = *self * rhs
    }
}

impl Div<i32> for Duration {
    type Output = Self;
    fn div(self, rhs: i32) -> Self {
        Self::new(self.nanos / rhs as i64)
    }
}

impl DivAssign<i32> for Duration {
    fn div_assign(&mut self, rhs: i32) {
        *self = *self / rhs
    }
}

impl Div<f32> for Duration {
    type Output = Self;
    fn div(self, rhs: f32) -> Self {
        Self::from_secs_f32(self.as_secs_f32() / rhs)
    }
}

impl DivAssign<f32> for Duration {
    fn div_assign(&mut self, rhs: f32) {
        *self = *self / rhs
    }
}

impl Div<f64> for Duration {
    type Output = Self;
    fn div(self, rhs: f64) -> Self {
        Self::from_secs_f64(self.as_secs_f64() / rhs)
    }
}

impl DivAssign<f64> for Duration {
    fn div_assign(&mut self, rhs: f64) {
        *self = *self / rhs
    }
}

impl Rem for Duration {
    type Output = Self;
    fn rem(self, rhs: Self) -> Self {
        Self::new(self.nanos % rhs.nanos)
    }
}

impl RemAssign for Duration {
    fn rem_assign(&mut self, rhs: Self) {
        *self = *self % rhs
    }
}

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(transparent)]
pub struct TimePoint {
    pub nanos_since_zero: i64,
}

impl TimePoint {
    /// This does not specifically require using unix time, however, that's probably expected.
    pub const fn new(nanos_since_zero: i64) -> Self {
        Self { nanos_since_zero }
    }

    pub const fn zero() -> Self {
        Self::new(0)
    }

    pub fn from_secs_f32(secs_since_zero: f32) -> Self {
        Self::zero() + Duration::from_secs_f32(secs_since_zero)
    }

    pub fn from_secs_f64(secs_since_zero: f64) -> Self {
        Self::zero() + Duration::from_secs_f64(secs_since_zero)
    }

    pub fn as_secs_f32(self) -> f32 {
        self.duration_from_zero().as_secs_f32()
    }

    pub fn as_secs_f64(self) -> f64 {
        self.duration_from_zero().as_secs_f64()
    }

    pub fn duration_from_zero(self) -> Duration {
        self - Self::zero()
    }

    pub fn from_std_instant(rhs: std::time::Instant) -> Self {
        Self::new(Duration::from_std_duration(rhs - zero_std_instant()).nanos)
    }

    pub fn into_std_instant(self) -> std::time::Instant {
        zero_std_instant() + self.duration_from_zero().into_std_duration()
    }
}

impl From<std::time::Instant> for TimePoint {
    fn from(std: std::time::Instant) -> Self {
        Self::from_std_instant(std)
    }
}

impl Into<std::time::Instant> for TimePoint {
    fn into(self) -> std::time::Instant {
        self.into_std_instant()
    }
}

impl Add<Duration> for TimePoint {
    type Output = TimePoint;
    fn add(self, rhs: Duration) -> Self {
        Self::new(self.nanos_since_zero + rhs.nanos)
    }
}

impl AddAssign<Duration> for TimePoint {
    fn add_assign(&mut self, rhs: Duration) {
        *self = *self + rhs
    }
}

impl Add<TimePoint> for Duration {
    type Output = TimePoint;
    fn add(self, rhs: TimePoint) -> TimePoint {
        rhs + self
    }
}

impl Sub<Duration> for TimePoint {
    type Output = TimePoint;
    fn sub(self, rhs: Duration) -> Self {
        self + -rhs
    }
}

impl SubAssign<Duration> for TimePoint {
    fn sub_assign(&mut self, rhs: Duration) {
        *self = *self - rhs
    }
}

impl Sub<TimePoint> for TimePoint {
    type Output = Duration;
    fn sub(self, rhs: TimePoint) -> Duration {
        Duration::new(self.nanos_since_zero - rhs.nanos_since_zero)
    }
}

#[test]
fn std_compat() {
    let now = std::time::Instant::now();
    let now2 = TimePoint::from_std_instant(now).into_std_instant();
    assert_eq!(now, now2);

    let duration = std::time::Duration::from_millis(1_000_042);
    let duration2 = Duration::from_std_duration(duration).into_std_duration();
    assert_eq!(duration, duration2);

    let std_now = std::time::Instant::now();
    let std_now_plus = std_now + std::time::Duration::from_millis(1_000_042);
    let now = TimePoint::from_std_instant(std_now);
    let now_plus = now + Duration::from_millis(1_000_042);
    assert_eq!(std_now_plus, now_plus.into_std_instant());
    assert_eq!(TimePoint::from_std_instant(std_now_plus), now_plus);
}