/home/a220/proj/radnelac/src/day_count/mjd.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.6 (*Calendrical Calculations: The Ultimate Edition* by Reingold & Dershowitz.) |
13 | | const MJD_EPOCH: f64 = 678576.0; |
14 | | |
15 | | /// Represents a Modified Julian Day Number (not to be confused with the Julian Calendar) |
16 | | /// |
17 | | /// The Modified Julian Day Number is the count of days since midnight November 17, |
18 | | /// 1858 CE in the proleptic Gregorian Calendar. |
19 | | /// |
20 | | /// This is internally a floating point number, where the integer portion represents a |
21 | | /// particular day and the fractional portion represents a particular time of day. |
22 | | /// |
23 | | /// Note that equality and ordering operations are subject to limitations similar to |
24 | | /// equality and ordering operations on a floating point number. Two `ModifiedJulianDay` values represent |
25 | | /// the same day or even the same second, but still appear different on the sub-second level. |
26 | | /// |
27 | | /// Further reading: |
28 | | /// + [Wikipedia](https://en.m.wikipedia.org/wiki/Julian_day#Variants) |
29 | | #[derive(Debug, PartialEq, PartialOrd, Clone, Copy, Default)] |
30 | | pub struct ModifiedJulianDay(f64); |
31 | | |
32 | | impl CalculatedBounds for ModifiedJulianDay {} |
33 | | |
34 | | impl FromFixed for ModifiedJulianDay { |
35 | 1.28k | fn from_fixed(t: Fixed) -> ModifiedJulianDay { |
36 | | //LISTING 1.8 (*Calendrical Calculations: The Ultimate Edition* by Reingold & Dershowitz.) |
37 | 1.28k | ModifiedJulianDay(t.get() - MJD_EPOCH) |
38 | 1.28k | } |
39 | | } |
40 | | |
41 | | impl ToFixed for ModifiedJulianDay { |
42 | 256 | fn to_fixed(self) -> Fixed { |
43 | | //LISTING 1.7 (*Calendrical Calculations: The Ultimate Edition* by Reingold & Dershowitz.) |
44 | 256 | Fixed::new(MJD_EPOCH + self.0) |
45 | 256 | } |
46 | | } |
47 | | |
48 | | impl Epoch for ModifiedJulianDay { |
49 | 3 | fn epoch() -> Fixed { |
50 | 3 | Fixed::new(MJD_EPOCH) |
51 | 3 | } |
52 | | } |
53 | | |
54 | | impl BoundedDayCount<f64> for ModifiedJulianDay { |
55 | 256 | fn new(t: f64) -> ModifiedJulianDay { |
56 | 256 | debug_assert!(ModifiedJulianDay::in_effective_bounds(t).is_ok()); |
57 | 256 | ModifiedJulianDay(t) |
58 | 256 | } |
59 | 771 | fn get(self) -> f64 { |
60 | 771 | self.0 |
61 | 771 | } |
62 | | } |