/home/a220/proj/radnelac/src/day_count/rd.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 | | const RD_EPOCH: f64 = 0.0; |
13 | | |
14 | | /// Represents a Rata Die |
15 | | /// |
16 | | /// The Rata Die is the count of days since midnight December 31, 0 CE in the |
17 | | /// proleptic Gregorian Calendar. |
18 | | /// |
19 | | /// This is internally a floating point number, where the integer portion represents a |
20 | | /// particular day and the fractional portion represents a particular time of day. |
21 | | /// |
22 | | /// Note that equality and ordering operations are subject to limitations similar to |
23 | | /// equality and ordering operations on a floating point number. Two `RataDie` values represent |
24 | | /// the same day or even the same second, but still appear different on the sub-second level. |
25 | | /// |
26 | | /// Further reading: |
27 | | /// + [Wikipedia](https://en.m.wikipedia.org/wiki/Rata_Die) |
28 | | #[derive(Debug, PartialEq, PartialOrd, Clone, Copy, Default)] |
29 | | pub struct RataDie(f64); |
30 | | |
31 | | impl CalculatedBounds for RataDie {} |
32 | | |
33 | | impl FromFixed for RataDie { |
34 | 1.46M | fn from_fixed(t: Fixed) -> RataDie { |
35 | | //LISTING 1.1 (*Calendrical Calculations: The Ultimate Edition* by Reingold & Dershowitz.) |
36 | 1.46M | RataDie(t.get() - RD_EPOCH) |
37 | 1.46M | } |
38 | | } |
39 | | |
40 | | impl ToFixed for RataDie { |
41 | 732k | fn to_fixed(self) -> Fixed { |
42 | 732k | Fixed::new(RD_EPOCH + self.0) |
43 | 732k | } |
44 | | } |
45 | | |
46 | | impl Epoch for RataDie { |
47 | 256 | fn epoch() -> Fixed { |
48 | 256 | Fixed::new(RD_EPOCH) |
49 | 256 | } |
50 | | } |
51 | | |
52 | | impl BoundedDayCount<f64> for RataDie { |
53 | 731k | fn new(t: f64) -> RataDie { |
54 | 731k | debug_assert!(RataDie::in_effective_bounds(t).is_ok()); |
55 | 731k | RataDie(t) |
56 | 731k | } |
57 | 1.46M | fn get(self) -> f64 { |
58 | 1.46M | self.0 |
59 | 1.46M | } |
60 | | } |
61 | | |
62 | | #[cfg(test)] |
63 | | mod tests { |
64 | | use super::*; |
65 | | |
66 | | #[test] |
67 | 1 | fn rd_is_epoch() { |
68 | 1 | assert_eq!(RataDie::new(0.0), RataDie::from_fixed(Fixed::new(0.0))); |
69 | 1 | } |
70 | | } |