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
use std::convert::TryInto;
use std::ops::Add;
use std::ops::AddAssign;
use std::ops::Sub;
use std::ops::SubAssign;

use crate::timestamp::Timestamp;

impl From<std::time::Duration> for Timestamp {
  /// Create a new timestamp from the given `std::time::Duration`.
  fn from(dur: std::time::Duration) -> Self {
    Timestamp {
      seconds: dur.as_secs().try_into().unwrap(),
      nanos: dur.subsec_nanos(),
    }
  }
}

impl From<Timestamp> for std::time::Duration {
  /// Create a new Duration from the given timestamp.
  fn from(ts: Timestamp) -> Self {
    std::time::Duration::new(ts.seconds.try_into().unwrap(), ts.nanos)
  }
}

impl Add<std::time::Duration> for Timestamp {
  type Output = Self;

  /// Add the provided duration to the timestamp.
  fn add(self, other: std::time::Duration) -> Timestamp {
    let s: i64 = other.as_secs().try_into().unwrap();
    Timestamp::new(self.seconds + s, self.nanos + other.subsec_nanos())
  }
}

impl AddAssign<std::time::Duration> for Timestamp {
  /// Add the provided duration to the timestamp, in-place.
  fn add_assign(&mut self, other: std::time::Duration) {
    let delta: i64 = other.as_secs().try_into().unwrap();
    self.seconds += delta;
    self.nanos += other.subsec_nanos();
    while self.nanos >= 1_000_000_000 {
      self.seconds += 1;
      self.nanos -= 1_000_000_000;
    }
  }
}

impl Sub<std::time::Duration> for Timestamp {
  type Output = Self;

  /// Subtract the provided duration from the timestamp.
  fn sub(self, other: std::time::Duration) -> Timestamp {
    let other_sec: i64 = other.as_secs().try_into().unwrap();
    if other.subsec_nanos() > self.nanos {
      return Timestamp::new(
        self.seconds - other_sec - 1,
        self.nanos + 1_000_000_000 - other.subsec_nanos(),
      );
    }
    Timestamp::new(self.seconds - other_sec, self.nanos - other.subsec_nanos())
  }
}

impl SubAssign<std::time::Duration> for Timestamp {
  /// Subtract the provided duration to the timestamp, in-place.
  fn sub_assign(&mut self, other: std::time::Duration) {
    let delta: i64 = other.as_secs().try_into().unwrap();
    self.seconds -= delta;
    if other.subsec_nanos() > self.nanos {
      self.seconds -= 1;
      self.nanos += 1_000_000_000;
    }
    self.nanos -= other.subsec_nanos();
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_from_duration() {
    let dur = std::time::Duration::new(1335020400, 0);
    let ts = Timestamp::from(dur);
    assert_eq!(ts.seconds, 1335020400);
  }

  #[test]
  fn test_from_timestamp() {
    let ts = Timestamp::new(1335020400, 500_000_000);
    let dur = std::time::Duration::from(ts);
    assert_eq!(dur.as_secs(), 1335020400);
    assert_eq!(dur.subsec_millis(), 500);
  }

  #[test]
  fn test_add() {
    let ts = Timestamp::new(1335020400, 0);
    let dur = std::time::Duration::new(86400, 0);
    assert_eq!(ts + dur, Timestamp::new(1335020400 + 86400, 0));
  }

  #[test]
  fn test_add_assign() {
    let mut ts = Timestamp::new(1335020400, 0);
    ts += std::time::Duration::new(86400, 0);
    assert_eq!(ts, Timestamp::new(1335020400 + 86400, 0));
  }

  #[test]
  fn test_add_assign_nano_overflow() {
    let mut ts = Timestamp::new(1335020400, 500_000_000);
    ts += std::time::Duration::new(0, 750_000_000);
    assert_eq!(ts.seconds, 1335020401);
    assert_eq!(ts.nanos, 250_000_000);
  }

  #[test]
  fn test_sub() {
    let ts = Timestamp::new(1335020400, 0);
    let dur = std::time::Duration::new(86400, 0);
    assert_eq!(ts - dur, Timestamp::new(1335020400 - 86400, 0));
  }

  #[test]
  fn test_sub_nano_overflow() {
    let ts = Timestamp::new(1335020400, 500_000_000)
      - std::time::Duration::new(0, 750_000_000);
    assert_eq!(ts.seconds, 1335020399);
    assert_eq!(ts.nanos, 750_000_000);
  }

  #[test]
  fn test_sub_assign() {
    let mut ts = Timestamp::new(1335020400, 0);
    ts -= std::time::Duration::new(86400, 0);
    assert_eq!(ts.seconds, 1335020400 - 86400);
    assert_eq!(ts.nanos, 0);
  }

  #[test]
  fn test_sub_assign_nano_overflow() {
    let mut ts = Timestamp::new(1335020400, 500_000_000);
    ts -= std::time::Duration::new(0, 750_000_000);
    assert_eq!(ts.seconds, 1335020399);
    assert_eq!(ts.nanos, 750_000_000);
  }
}