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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Simple & fast UTC time types.
//!
//! While [chrono](https://crates.io/crates/chrono) is great for dealing with time
//! in most cases, its 96-bit integer design can be costly when processing and storing
//! large amounts of timestamp data.
//!
//! This lib solves this problem by providing very simple UTC timestamps that can be
//! converted from and into their corresponding chrono counterpart using Rust's
//! `From` and `Into` traits. chrono is then used for all things that aren't expected
//! to occur in big batches, such as formatting and displaying the timestamps.

use core::{fmt, ops};

#[cfg(feature = "serde-support")]
use serde::{Deserialize, Serialize};

// ============================================================================================== //
// [UTC timestamp]                                                                                //
// ============================================================================================== //

/// Represents a dumb but fast UTC timestamp.
#[repr(transparent)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
pub struct UtcTimeStamp(i64);

/// Display timestamp using chrono.
impl fmt::Display for UtcTimeStamp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        chrono::DateTime::<chrono::Utc>::from(*self).fmt(f)
    }
}

impl fmt::Debug for UtcTimeStamp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "UtcTimeStamp({})", self.0)
    }
}

/// Create a dumb timestamp from a chrono date time object.
impl From<chrono::DateTime<chrono::Utc>> for UtcTimeStamp {
    fn from(other: chrono::DateTime<chrono::Utc>) -> Self {
        Self(other.timestamp_millis())
    }
}

/// Create a chrono date time object from a dumb timestamp.
impl From<UtcTimeStamp> for chrono::DateTime<chrono::Utc> {
    fn from(other: UtcTimeStamp) -> Self {
        let sec = other.0 / 1000;
        let ns = (other.0 % 1000 * 1_000_000) as u32;
        let naive = chrono::NaiveDateTime::from_timestamp(sec, ns);
        chrono::DateTime::<chrono::Utc>::from_utc(naive, chrono::Utc)
    }
}

impl UtcTimeStamp {
    /// Initialize a timestamp with 0, `1970-01-01 00:00:00 UTC`.
    #[inline]
    pub const fn zero() -> Self {
        UtcTimeStamp(0)
    }

    /// Initialize a timestamp using the current local time converted to UTC.
    pub fn now() -> Self {
        chrono::Utc::now().into()
    }

    /// Explicit conversion from `i64`.
    #[inline]
    pub const fn from_milliseconds(int: i64) -> Self {
        UtcTimeStamp(int)
    }

    /// Explicit conversion from `i64` seconds.
    #[inline]
    pub const fn from_seconds(int: i64) -> Self {
        UtcTimeStamp(int * 1000)
    }

    /// Explicit conversion to `i64`.
    #[inline]
    pub const fn as_milliseconds(self) -> i64 {
        self.0
    }

    /// Align a timestamp to a given frequency.
    pub const fn align_to(self, freq: TimeDelta) -> UtcTimeStamp {
        self.align_to_anchored(UtcTimeStamp::zero(), freq)
    }

    /// Align a timestamp to a given frequency, with a time anchor.
    pub const fn align_to_anchored(self, anchor: UtcTimeStamp, freq: TimeDelta) -> UtcTimeStamp {
        UtcTimeStamp((self.0 - anchor.0) / freq.0 * freq.0 + anchor.0)
    }

    /// Check whether the timestamp is 0 (`1970-01-01 00:00:00 UTC`).
    #[inline]
    pub const fn is_zero(self) -> bool {
        self.0 == 0
    }
}

/// Calculate the timestamp advanced by a timedelta.
impl ops::Add<TimeDelta> for UtcTimeStamp {
    type Output = UtcTimeStamp;

    fn add(self, rhs: TimeDelta) -> Self::Output {
        UtcTimeStamp(self.0 + rhs.0)
    }
}

impl ops::AddAssign<TimeDelta> for UtcTimeStamp {
    fn add_assign(&mut self, rhs: TimeDelta) {
        *self = *self + rhs;
    }
}

/// Calculate the timestamp lessened by a timedelta.
impl ops::Sub<TimeDelta> for UtcTimeStamp {
    type Output = UtcTimeStamp;

    fn sub(self, rhs: TimeDelta) -> Self::Output {
        UtcTimeStamp(self.0 - rhs.0)
    }
}

impl ops::SubAssign<TimeDelta> for UtcTimeStamp {
    fn sub_assign(&mut self, rhs: TimeDelta) {
        *self = *self - rhs;
    }
}

/// Calculate signed timedelta between two timestamps.
impl ops::Sub<UtcTimeStamp> for UtcTimeStamp {
    type Output = TimeDelta;

    fn sub(self, rhs: UtcTimeStamp) -> Self::Output {
        TimeDelta(self.0 - rhs.0)
    }
}

// /// How far away is the timestamp from being aligned to the given timedelta?
// impl ops::Rem<TimeDelta> for UtcTimeStamp {
//     type Output = TimeDelta;
//
//     fn rem(self, rhs: TimeDelta) -> Self::Output {
//         TimeDelta(self.0 % rhs.0)
//     }
// }

// ============================================================================================== //
// [TimeDelta]                                                                                    //
// ============================================================================================== //

/// Millisecond precision time delta.
#[repr(transparent)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
pub struct TimeDelta(i64);

/// Display timedelta using chrono.
impl fmt::Display for TimeDelta {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        chrono::Duration::from(*self).fmt(f)
    }
}

/// Create a simple timedelta from a chrono duration.
impl From<chrono::Duration> for TimeDelta {
    fn from(other: chrono::Duration) -> Self {
        Self(other.num_milliseconds())
    }
}

/// Create a chrono duration from a simple timedelta.
impl From<TimeDelta> for chrono::Duration {
    fn from(other: TimeDelta) -> Self {
        chrono::Duration::milliseconds(other.0)
    }
}

impl ops::Add<TimeDelta> for TimeDelta {
    type Output = TimeDelta;

    fn add(self, rhs: TimeDelta) -> Self::Output {
        TimeDelta(self.0 + rhs.0)
    }
}

impl ops::Sub<TimeDelta> for TimeDelta {
    type Output = TimeDelta;

    fn sub(self, rhs: TimeDelta) -> Self::Output {
        TimeDelta(self.0 - rhs.0)
    }
}

/// Multiply the delta to be n times as long.
impl ops::Mul<i64> for TimeDelta {
    type Output = TimeDelta;

    fn mul(self, rhs: i64) -> Self::Output {
        TimeDelta(self.0 * rhs)
    }
}

/// Shorten the delta by a given factor. Integer div.
impl ops::Div<i64> for TimeDelta {
    type Output = TimeDelta;

    fn div(self, rhs: i64) -> Self::Output {
        TimeDelta(self.0 / rhs)
    }
}

/// How many times does the timestamp fit into another?
impl ops::Div<TimeDelta> for TimeDelta {
    type Output = i64;

    fn div(self, rhs: TimeDelta) -> Self::Output {
        self.0 / rhs.0
    }
}

/// How far away is the delta from being aligned to another delta?
impl ops::Rem<TimeDelta> for TimeDelta {
    type Output = TimeDelta;

    fn rem(self, rhs: TimeDelta) -> Self::Output {
        TimeDelta(self.0 % rhs.0)
    }
}

/// Explicit conversion from and to `i64`.
impl TimeDelta {
    #[inline]
    pub const fn zero() -> Self {
        TimeDelta(0)
    }

    #[inline]
    pub const fn from_hours(int: i64) -> Self {
        TimeDelta::from_minutes(int * 60)
    }

    #[inline]
    pub const fn from_minutes(int: i64) -> Self {
        TimeDelta::from_seconds(int * 60)
    }

    #[inline]
    pub const fn from_seconds(int: i64) -> Self {
        TimeDelta(int * 1000)
    }

    #[inline]
    pub const fn from_milliseconds(int: i64) -> Self {
        TimeDelta(int)
    }

    #[inline]
    pub const fn as_milliseconds(self) -> i64 {
        self.0
    }

    /// Check whether the timedelta is 0.
    #[inline]
    pub const fn is_zero(self) -> bool {
        self.0 == 0
    }

    /// Returns `true` if the timedelta is positive and
    /// `false` if it is zero or negative.
    #[inline]
    pub const fn is_positive(self) -> bool {
        self.0 > 0
    }

    /// Returns `true` if the timedelta is negative and
    /// `false` if it is zero or positive.
    #[inline]
    pub const fn is_negative(self) -> bool {
        self.0 < 0
    }
}

// ============================================================================================== //
// [TimeRange]                                                                                    //
// ============================================================================================== //

/// An iterator looping over dates given a time delta as step.
///
/// The range is either right open or right closed depending on the
/// constructor chosen, but always left closed.
///
/// Examples:
///
/// ```
/// use utctimestamp::TimeRange;
/// use chrono::{offset::TimeZone, Duration, Utc};
///
/// let start = Utc.ymd(2019, 4, 14).and_hms(0, 0, 0);
/// let end = Utc.ymd(2019, 4, 16).and_hms(0, 0, 0);
/// let step = Duration::hours(12);
/// let tr: Vec<_> = TimeRange::right_closed(start, end, step).collect();
///
/// assert_eq!(tr, vec![
///     Utc.ymd(2019, 4, 14).and_hms(0, 0, 0).into(),
///     Utc.ymd(2019, 4, 14).and_hms(12, 0, 0).into(),
///     Utc.ymd(2019, 4, 15).and_hms(0, 0, 0).into(),
///     Utc.ymd(2019, 4, 15).and_hms(12, 0, 0).into(),
///     Utc.ymd(2019, 4, 16).and_hms(0, 0, 0).into(),
/// ]);
/// ```
#[derive(Debug)]
pub struct TimeRange {
    cur: UtcTimeStamp,
    end: UtcTimeStamp,
    step: TimeDelta,
    right_closed: bool,
}

impl TimeRange {
    /// Create a time range that includes the end date.
    pub fn right_closed(
        start: impl Into<UtcTimeStamp>,
        end: impl Into<UtcTimeStamp>,
        step: impl Into<TimeDelta>,
    ) -> Self {
        TimeRange {
            cur: start.into(),
            end: end.into(),
            step: step.into(),
            right_closed: true,
        }
    }

    /// Create a time range that excludes the end date.
    pub fn right_open(
        start: impl Into<UtcTimeStamp>,
        end: impl Into<UtcTimeStamp>,
        step: impl Into<TimeDelta>,
    ) -> Self {
        TimeRange {
            cur: start.into(),
            end: end.into(),
            step: step.into(),
            right_closed: false,
        }
    }
}

impl Iterator for TimeRange {
    type Item = UtcTimeStamp;

    fn next(&mut self) -> Option<Self::Item> {
        let exhausted = if self.right_closed {
            self.cur > self.end
        } else {
            self.cur >= self.end
        };

        if exhausted {
            None
        } else {
            let cur = self.cur;
            self.cur += self.step;
            Some(cur)
        }
    }
}

// ============================================================================================== //
// [Tests]                                                                                        //
// ============================================================================================== //

#[cfg(test)]
mod tests {
    use crate::*;
    use chrono::{offset::TimeZone, Duration, Utc};

    #[test]
    fn open_time_range() {
        let start = Utc.ymd(2019, 4, 14).and_hms(0, 0, 0);
        let end = Utc.ymd(2019, 4, 16).and_hms(0, 0, 0);
        let step = Duration::hours(12);
        let tr: Vec<_> = Iterator::collect(TimeRange::right_closed(start, end, step));
        assert_eq!(tr, vec![
            Utc.ymd(2019, 4, 14).and_hms(0, 0, 0).into(),
            Utc.ymd(2019, 4, 14).and_hms(12, 0, 0).into(),
            Utc.ymd(2019, 4, 15).and_hms(0, 0, 0).into(),
            Utc.ymd(2019, 4, 15).and_hms(12, 0, 0).into(),
            Utc.ymd(2019, 4, 16).and_hms(0, 0, 0).into(),
        ]);
    }

    #[test]
    fn timestamp_and_delta_vs_chrono() {
        let c_dt = Utc.ymd(2019, 3, 13).and_hms(16, 14, 9);
        let c_td = Duration::milliseconds(123456);

        let my_dt = UtcTimeStamp::from(c_dt.clone());
        let my_td = TimeDelta::from_milliseconds(123456);
        assert_eq!(TimeDelta::from(c_td.clone()), my_td);

        let c_result = c_dt + c_td * 555;
        let my_result = my_dt + my_td * 555;
        assert_eq!(UtcTimeStamp::from(c_result.clone()), my_result);
    }

    #[test]
    fn timestamp_ord_eq() {
        let ts1: UtcTimeStamp = UtcTimeStamp::from_milliseconds(111);
        let ts2: UtcTimeStamp = UtcTimeStamp::from_milliseconds(222);
        let ts3: UtcTimeStamp = UtcTimeStamp::from_milliseconds(222);

        assert!(ts1 < ts2);
        assert!(ts2 > ts1);
        assert!(ts1 <= ts2);
        assert!(ts2 >= ts3);
        assert!(ts2 <= ts3);
        assert!(ts2 >= ts3);
        assert_eq!(ts2, ts3);
        assert_ne!(ts1, ts3);
    }

    #[test]
    fn align_to_anchored() {
        let day = Utc.ymd(2020, 9, 28);
        let ts: UtcTimeStamp = day.and_hms(19, 32, 51).into();

        assert_eq!(
            ts.align_to_anchored(day.and_hms(0, 0, 0).into(), TimeDelta::from_seconds(60 * 5)),
            day.and_hms(19, 30, 0).into(),
        );

        assert_eq!(
            ts.align_to_anchored(
                day.and_hms(9 /* irrelevant */, 1, 3).into(),
                TimeDelta::from_seconds(60 * 5)
            ),
            day.and_hms(19, 31, 3).into(),
        );
    }

    #[test]
    fn align_to_anchored_eq() {
        let day = Utc.ymd(2020, 1, 1);
        let anchor: UtcTimeStamp = day.and_hms(0, 0, 0).into();
        let freq = TimeDelta::from_seconds(5 * 60);

        let ts1: UtcTimeStamp = day.and_hms(12, 1, 11).into();
        let ts2: UtcTimeStamp = day.and_hms(12, 4, 11).into();
        assert_eq!(
            ts1.align_to_anchored(anchor, freq),
            ts2.align_to_anchored(anchor, freq),
        );
    }
}

// ============================================================================================== //