datetime/
interval.rs

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

use crate::DateTime;

/// An interval of time between two timestamps.
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
pub struct TimeInterval {
  seconds: i64,
  nanos: u32,
}

impl TimeInterval {
  /// Create a new time interval from seconds and nanoseconds.
  pub const fn new(seconds: i64, nanos: u32) -> Self {
    Self { seconds, nanos }
  }

  /// The number of seconds this interval represents.
  ///
  /// Note that the nanoseconds value is always positive, even if seconds is negative. For example,
  /// an interval representing -2.5 seconds will be represented as -3 seconds and 500,000,000
  /// nanos.
  pub const fn seconds(&self) -> i64 {
    self.seconds
  }

  /// The number of nanoseconds this interval represents.
  ///
  /// Note that the nanoseconds value is always positive, even if seconds is negative. For example,
  /// an interval representing -2.5 seconds will be represented as -3 seconds and 500,000,000
  /// nanos.
  pub const fn nanoseconds(&self) -> u32 {
    self.nanos
  }

  /// The number of milliseconds this interval represents.
  pub const fn as_milliseconds(&self) -> i64 {
    self.seconds * 1_000 + (self.nanos / 1_000_000) as i64
  }

  /// The number of microseconds this interval represents.
  pub const fn as_microseconds(&self) -> i64 {
    self.seconds * 1_000_000 + (self.nanos / 1_000) as i64
  }

  /// The number of nanoseconds this interval represents.
  pub const fn as_nanoseconds(&self) -> i128 {
    self.seconds as i128 * 1_000_000_000 + self.nanos as i128
  }
}

impl Add<TimeInterval> for DateTime {
  type Output = DateTime;

  fn add(self, rhs: TimeInterval) -> Self::Output {
    let seconds = self.seconds + rhs.seconds;
    let nanos = self.nanos + rhs.nanos;
    Self {
      seconds,
      nanos,
      #[cfg(feature = "tz")]
      tz: self.tz,
    }
  }
}

impl AddAssign<TimeInterval> for DateTime {
  fn add_assign(&mut self, rhs: TimeInterval) {
    self.seconds += rhs.seconds;
    self.nanos += rhs.nanos;
    while self.nanos >= 1_000_000_000 {
      self.seconds += 1;
      self.nanos -= 1_000_000_000;
    }
  }
}

impl Sub<TimeInterval> for DateTime {
  type Output = DateTime;

  #[allow(clippy::suspicious_arithmetic_impl)]
  fn sub(self, rhs: TimeInterval) -> Self::Output {
    let mut seconds = self.seconds - rhs.seconds;
    let nanos = self.nanos.checked_sub(rhs.nanos).unwrap_or_else(|| {
      seconds -= 1;
      self.nanos + 1_000_000_000 - rhs.nanos
    });
    Self {
      seconds,
      nanos,
      #[cfg(feature = "tz")]
      tz: self.tz,
    }
  }
}

impl SubAssign<TimeInterval> for DateTime {
  fn sub_assign(&mut self, rhs: TimeInterval) {
    self.seconds -= rhs.seconds;
    match self.nanos >= rhs.nanos {
      true => self.nanos -= rhs.nanos,
      false => {
        self.nanos += 1_000_000_000;
        self.seconds -= 1;
        self.nanos -= rhs.nanos;
      },
    }
  }
}

impl Sub for DateTime {
  type Output = TimeInterval;

  #[allow(clippy::suspicious_arithmetic_impl)]
  fn sub(self, rhs: Self) -> Self::Output {
    let mut seconds = self.seconds - rhs.seconds;
    let nanos = self.nanos.checked_sub(rhs.nanos).unwrap_or_else(|| {
      seconds -= 1;
      self.nanos + 1_000_000_000 - rhs.nanos
    });
    TimeInterval { seconds, nanos }
  }
}

impl Div for TimeInterval {
  type Output = f64;

  fn div(self, rhs: Self) -> Self::Output {
    self.as_nanoseconds() as f64 / rhs.as_nanoseconds() as f64
  }
}

#[cfg(test)]
mod tests {
  use assert2::check;

  use super::*;
  use crate::DateTime;
  use crate::datetime;

  #[test]
  fn test_add() {
    check!(
      datetime! { 2012-04-21 11:00:00 } + TimeInterval::new(3600, 0)
        == datetime! { 2012-04-21 12:00:00 }
    );
    check!(
      datetime! { 2012-04-21 11:00:00 } + TimeInterval::new(1800, 0)
        == datetime! { 2012-04-21 11:30:00 }
    );
    check!(
      datetime! { 2012-04-21 11:00:00 } + TimeInterval::new(0, 500_000_000)
        == DateTime::ymd(2012, 4, 21).hms(11, 0, 0).nanos(500_000_000).build()
    );
  }

  #[test]
  fn test_add_assign() {
    let mut dt = datetime! { 2012-04-21 11:00:00 };
    dt += TimeInterval::new(3600, 0);
    check!(dt == datetime! { 2012-04-21 12:00:00 });
    dt += TimeInterval::new(0, 750_000_000);
    dt += TimeInterval::new(0, 250_000_000);
    check!(dt == datetime! { 2012-04-21 12:00:01 });
  }

  #[test]
  fn test_sub() {
    check!(
      datetime! { 2012-04-21 11:00:00 } - TimeInterval::new(3600, 0)
        == datetime! { 2012-04-21 10:00:00 }
    );
    check!(
      datetime! { 2012-04-21 11:00:00 } - TimeInterval::new(0, 500_000_000)
        == DateTime::ymd(2012, 4, 21).hms(10, 59, 59).nanos(500_000_000).build()
    );
  }

  #[test]
  fn test_sub_assign() {
    let mut dt = datetime! { 2012-04-21 11:00:00 };
    dt -= TimeInterval::new(3600, 0);
    check!(dt == datetime! { 2012-04-21 10:00:00 });
    dt -= TimeInterval::new(0, 750_000_000);
    dt -= TimeInterval::new(0, 350_000_000);
    dt -= TimeInterval::new(0, 900_000_000);
    check!(dt == datetime! { 2012-04-21 09:59:58 });
  }

  #[test]
  fn test_sub_dt() {
    check!(
      datetime! { 2012-04-21 11:00:00 } - datetime! { 2012-04-21 10:00:00 }
        == TimeInterval::new(3600, 0)
    );
    check!(
      datetime! { 2012-04-21 11:00:00 } - datetime! { 2012-04-21 12:00:00 }
        == TimeInterval::new(-3600, 0)
    );
  }

  #[test]
  fn test_div() {
    check!(TimeInterval::new(3600, 0) / TimeInterval::new(1800, 0) == 2.0);
    check!(TimeInterval::new(-1800, 0) / TimeInterval::new(-3600, 0) == 0.5);
    check!(TimeInterval::new(-1800, 0) / TimeInterval::new(3600, 0) == -0.5);
    check!(TimeInterval::new(0, 3600) / TimeInterval::new(0, 1800) == 2.0);
  }

  #[test]
  fn test_as() {
    let dur = TimeInterval::new(5, 0);
    check!(dur.as_milliseconds() == 5_000);
    check!(dur.as_microseconds() == 5_000_000);
    check!(dur.as_nanoseconds() == 5_000_000_000);
  }
}