calendrical_calculations/
rata_die.rs1use core::fmt;
11use core::ops::{Add, AddAssign, Sub, SubAssign};
12#[allow(unused_imports)]
13use core_maths::*;
14
15#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
24pub struct RataDie(i64);
25
26impl RataDie {
27 pub const fn new(fixed_date: i64) -> Self {
33 let result = Self(fixed_date);
34 #[cfg(debug_assertions)]
35 result.check();
36 result
37 }
38
39 #[cfg(debug_assertions)]
41 const fn check(self) {
42 if self.0 > i64::MAX / 256 {
43 debug_assert!(
44 false,
45 "RataDie is not designed to store values near to the overflow boundary"
46 );
47 }
48 if self.0 < i64::MIN / 256 {
49 debug_assert!(
50 false,
51 "RataDie is not designed to store values near to the overflow boundary"
52 );
53 }
54 }
55
56 #[doc(hidden)] pub const fn big_negative() -> Self {
59 Self::new(i64::MIN / 256 / 256)
60 }
61
62 pub const fn to_i64_date(self) -> i64 {
64 self.0
65 }
66
67 pub(crate) const fn to_f64_date(self) -> f64 {
69 self.0 as f64
70 }
71
72 pub const fn until(self, rhs: Self) -> i64 {
74 self.0 - rhs.0
75 }
76
77 pub const fn add(self, rhs: i64) -> Self {
79 let result = Self(self.0 + rhs);
80 #[cfg(debug_assertions)]
81 result.check();
82 result
83 }
84
85 pub(crate) const fn as_moment(self) -> Moment {
87 Moment::new(self.0 as f64)
88 }
89}
90
91impl fmt::Debug for RataDie {
92 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
93 let rd = self.0;
94 if let Ok((y, m, d)) = crate::iso::iso_from_fixed(*self) {
95 write!(f, "{rd} R.D. ({y}-{m:02}-{d:02})")
96 } else {
97 write!(f, "{rd} R.D. (out of bounds)")
98 }
99 }
100}
101
102impl Add<i64> for RataDie {
104 type Output = Self;
105 fn add(self, rhs: i64) -> Self::Output {
106 self.add(rhs)
107 }
108}
109
110impl AddAssign<i64> for RataDie {
111 fn add_assign(&mut self, rhs: i64) {
112 self.0 += rhs;
113 }
114}
115
116impl Sub<i64> for RataDie {
118 type Output = Self;
119 fn sub(self, rhs: i64) -> Self::Output {
120 let result = Self(self.0 - rhs);
121 #[cfg(debug_assertions)]
122 result.check();
123 result
124 }
125}
126
127impl SubAssign<i64> for RataDie {
128 fn sub_assign(&mut self, rhs: i64) {
129 self.0 -= rhs;
130 }
131}
132
133impl Sub for RataDie {
135 type Output = i64;
136 fn sub(self, rhs: Self) -> Self::Output {
137 self.until(rhs)
138 }
139}
140
141#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
146pub(crate) struct Moment(f64);
147
148impl Add<f64> for Moment {
150 type Output = Self;
151 fn add(self, rhs: f64) -> Self::Output {
152 Self(self.0 + rhs)
153 }
154}
155
156impl AddAssign<f64> for Moment {
157 fn add_assign(&mut self, rhs: f64) {
158 self.0 += rhs;
159 }
160}
161
162impl Sub<f64> for Moment {
164 type Output = Self;
165 fn sub(self, rhs: f64) -> Self::Output {
166 Self(self.0 - rhs)
167 }
168}
169
170impl SubAssign<f64> for Moment {
171 fn sub_assign(&mut self, rhs: f64) {
172 self.0 -= rhs;
173 }
174}
175
176impl Sub for Moment {
178 type Output = f64;
179 fn sub(self, rhs: Self) -> Self::Output {
180 self.0 - rhs.0
181 }
182}
183
184impl Moment {
185 pub const fn new(value: f64) -> Moment {
187 Moment(value)
188 }
189
190 pub const fn inner(self) -> f64 {
192 self.0
193 }
194
195 pub fn as_rata_die(self) -> RataDie {
197 RataDie::new(self.0.floor() as i64)
198 }
199}
200
201#[test]
202fn test_moment_to_rata_die_conversion() {
203 for i in -1000..=1000 {
204 let moment = Moment::new(i as f64);
205 let rata_die = moment.as_rata_die();
206 assert_eq!(rata_die.to_i64_date(), i);
207 }
208}