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 since(self, rhs: Self) -> i64 {
83 self.0 - rhs.0
84 }
85
86 pub const fn until(self, rhs: Self) -> i64 {
97 rhs.0 - self.0
98 }
99
100 pub const fn add(self, rhs: i64) -> Self {
102 let result = Self(self.0 + rhs);
103 #[cfg(debug_assertions)]
104 result.check();
105 result
106 }
107
108 pub(crate) const fn as_moment(self) -> Moment {
110 Moment::new(self.0 as f64)
111 }
112}
113
114impl fmt::Debug for RataDie {
115 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
116 let rd = self.0;
117 if let Ok((y, m, d)) = crate::gregorian::gregorian_from_fixed(*self) {
118 write!(f, "{rd} R.D. ({y}-{m:02}-{d:02})")
119 } else {
120 write!(f, "{rd} R.D. (out of bounds)")
121 }
122 }
123}
124
125impl Add<i64> for RataDie {
127 type Output = Self;
128 fn add(self, rhs: i64) -> Self::Output {
129 self.add(rhs)
130 }
131}
132
133impl AddAssign<i64> for RataDie {
134 fn add_assign(&mut self, rhs: i64) {
135 self.0 += rhs;
136 }
137}
138
139impl Sub<i64> for RataDie {
141 type Output = Self;
142 fn sub(self, rhs: i64) -> Self::Output {
143 let result = Self(self.0 - rhs);
144 #[cfg(debug_assertions)]
145 result.check();
146 result
147 }
148}
149
150impl SubAssign<i64> for RataDie {
151 fn sub_assign(&mut self, rhs: i64) {
152 self.0 -= rhs;
153 }
154}
155
156impl Sub for RataDie {
158 type Output = i64;
159 fn sub(self, rhs: Self) -> Self::Output {
160 self.since(rhs)
161 }
162}
163
164#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
169pub(crate) struct Moment(f64);
170
171impl Add<f64> for Moment {
173 type Output = Self;
174 fn add(self, rhs: f64) -> Self::Output {
175 Self(self.0 + rhs)
176 }
177}
178
179impl AddAssign<f64> for Moment {
180 fn add_assign(&mut self, rhs: f64) {
181 self.0 += rhs;
182 }
183}
184
185impl Sub<f64> for Moment {
187 type Output = Self;
188 fn sub(self, rhs: f64) -> Self::Output {
189 Self(self.0 - rhs)
190 }
191}
192
193impl SubAssign<f64> for Moment {
194 fn sub_assign(&mut self, rhs: f64) {
195 self.0 -= rhs;
196 }
197}
198
199impl Sub for Moment {
201 type Output = f64;
202 fn sub(self, rhs: Self) -> Self::Output {
203 self.0 - rhs.0
204 }
205}
206
207impl Moment {
208 pub const fn new(value: f64) -> Moment {
210 Moment(value)
211 }
212
213 pub const fn inner(self) -> f64 {
215 self.0
216 }
217
218 pub fn as_rata_die(self) -> RataDie {
220 RataDie::new(self.0.floor() as i64)
221 }
222}
223
224#[test]
225fn test_moment_to_rata_die_conversion() {
226 for i in -1000..=1000 {
227 let moment = Moment::new(i as f64);
228 let rata_die = moment.as_rata_die();
229 assert_eq!(rata_die.to_i64_date(), i);
230 }
231}