/home/a220/proj/radnelac/src/day_count/unix.rs
Line | Count | Source |
1 | | // This Source Code Form is subject to the terms of the Mozilla Public |
2 | | // License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | | // file, You can obtain one at https://mozilla.org/MPL/2.0/. |
4 | | |
5 | | use crate::day_count::fixed::CalculatedBounds; |
6 | | use crate::day_count::fixed::Epoch; |
7 | | use crate::day_count::fixed::Fixed; |
8 | | use crate::day_count::fixed::FromFixed; |
9 | | use crate::day_count::fixed::ToFixed; |
10 | | use crate::day_count::prelude::BoundedDayCount; |
11 | | |
12 | | //LISTING 1.9 (*Calendrical Calculations: The Ultimate Edition* by Reingold & Dershowitz.) |
13 | | const UNIX_EPOCH: f64 = 719163.0; |
14 | | const UNIX_DAY: f64 = 24.0 * 60.0 * 60.0; |
15 | | |
16 | | /// Represents seconds since the Unix epoch |
17 | | /// |
18 | | /// The Unix epoch is 00:00:00 UTC on January 1, 1970 CE in the proleptic Gregorian calendar. |
19 | | /// |
20 | | /// This is internally an integer of more than 32 bits. |
21 | | /// |
22 | | /// Note that the range of dates supported by this library is different from the range of dates |
23 | | /// supported by standard Unix utilities. This library can support years beyond 2038 CE |
24 | | /// (Gregorian) but if you plan to work with times in the distant past or future, please see |
25 | | /// the documentation for `Fixed`. |
26 | | /// |
27 | | /// Further reading: |
28 | | /// + [Wikipedia](https://en.wikipedia.org/wiki/Unix_time) |
29 | | #[derive(Debug, PartialEq, PartialOrd, Clone, Copy, Default)] |
30 | | pub struct UnixMoment(i64); |
31 | | |
32 | | impl CalculatedBounds for UnixMoment {} |
33 | | |
34 | | impl FromFixed for UnixMoment { |
35 | 1.28k | fn from_fixed(t: Fixed) -> UnixMoment { |
36 | | //LISTING 1.11 (*Calendrical Calculations: The Ultimate Edition* by Reingold & Dershowitz.) |
37 | | //Modified from the original with `round()` |
38 | 1.28k | UnixMoment((UNIX_DAY * (t.get() - UNIX_EPOCH)).round() as i64) |
39 | 1.28k | } |
40 | | } |
41 | | |
42 | | impl ToFixed for UnixMoment { |
43 | 256 | fn to_fixed(self) -> Fixed { |
44 | | //LISTING 1.10 (*Calendrical Calculations: The Ultimate Edition* by Reingold & Dershowitz.) |
45 | 256 | Fixed::new(UNIX_EPOCH + ((self.0 as f64) / UNIX_DAY)) |
46 | 256 | } |
47 | | } |
48 | | |
49 | | impl Epoch for UnixMoment { |
50 | 259 | fn epoch() -> Fixed { |
51 | 259 | Fixed::new(UNIX_EPOCH) |
52 | 259 | } |
53 | | } |
54 | | |
55 | | impl BoundedDayCount<i64> for UnixMoment { |
56 | 256 | fn new(t: i64) -> UnixMoment { |
57 | 256 | debug_assert!(UnixMoment::in_effective_bounds(t).is_ok()); |
58 | 256 | UnixMoment(t) |
59 | 256 | } |
60 | 771 | fn get(self) -> i64 { |
61 | 771 | self.0 |
62 | 771 | } |
63 | | } |