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