Debian Codegraph @

third_party/rust/calendrical_calculations/src/rata_die.rs

raw
1 // This file is part of ICU4X.
2 //
3 // The contents of this file implement algorithms from Calendrical Calculations
4 // by Reingold & Dershowitz, Cambridge University Press, 4th edition (2018),
5 // which have been released as Lisp code at <https://github.com/EdReingold/calendar-code2/>
6 // under the Apache-2.0 license. Accordingly, this file is released under
7 // the Apache License, Version 2.0 which can be found at the calendrical_calculations
8 // package root or at http://www.apache.org/licenses/LICENSE-2.0.
9
10 use core::fmt;
11 use core::ops::{Add, AddAssign, Sub, SubAssign};
12 #[allow(unused_imports)]
13 use core_maths::*;
14
15 /// The *Rata Die*, or *R.D.*, or `fixed_date`: number of days since January 1, 1 CE.
16 ///
17 /// See: <https://en.wikipedia.org/wiki/Rata_Die>
18 ///
19 /// It is a logic error to construct a RataDie
20 /// except from a date that is in range of one of the official calendars.
21 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
22 pub struct RataDie(i64);
23
24 impl RataDie {
25 /// Create a RataDie
26 pub const fn new(fixed_date: i64) -> Self {
27 let result = Self(fixed_date);
28 #[cfg(debug_assertions)]
29 result.check();
30 result
31 }
32
33 /// Check that it is in range
34 #[cfg(debug_assertions)]
35 pub const fn check(&self) {
36 if self.0 > i64::MAX / 256 {
37 debug_assert!(
38 false,
39 "RataDie is not designed to store values near to the overflow boundary"
40 );
41 }
42 if self.0 < i64::MIN / 256 {
43 debug_assert!(
44 false,
45 "RataDie is not designed to store values near to the overflow boundary"
46 );
47 }
48 }
49
50 /// A valid RataDie that is intended to be below all dates representable in calendars
51 ///
52 /// For testing only
53 #[doc(hidden)]
54 pub const fn big_negative() -> Self {
55 Self::new(i64::MIN / 256 / 256)
56 }
57
58 /// Convert this to an i64 value representing the RataDie
59 pub const fn to_i64_date(self) -> i64 {
60 self.0
61 }
62
63 /// Convert this to an f64 value representing the RataDie
64 pub const fn to_f64_date(self) -> f64 {
65 self.0 as f64
66 }
67
68 /// Calculate the number of days between two RataDie in a const-friendly way
69 pub const fn const_diff(self, rhs: Self) -> i64 {
70 self.0 - rhs.0
71 }
72
73 /// Convert this to a [`Moment`]
74 pub const fn as_moment(&self) -> Moment {
75 Moment::new(self.0 as f64)
76 }
77 }
78
79 impl fmt::Debug for RataDie {
80 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
81 let rd = self.0;
82 if let Ok((y, m, d)) = crate::iso::iso_from_fixed(*self) {
83 write!(f, "{rd} R.D. ({y}-{m:02}-{d:02})")
84 } else {
85 write!(f, "{rd} R.D. (out of bounds)")
86 }
87 }
88 }
89
90 /// Shift a RataDie N days into the future
91 impl Add<i64> for RataDie {
92 type Output = Self;
93 fn add(self, rhs: i64) -> Self::Output {
94 let result = Self(self.0 + rhs);
95 #[cfg(debug_assertions)]
96 result.check();
97 result
98 }
99 }
100
101 impl AddAssign<i64> for RataDie {
102 fn add_assign(&mut self, rhs: i64) {
103 self.0 += rhs;
104 #[cfg(debug_assertions)]
105 self.check();
106 }
107 }
108
109 /// Shift a RataDie N days into the past
110 impl Sub<i64> for RataDie {
111 type Output = Self;
112 fn sub(self, rhs: i64) -> Self::Output {
113 let result = Self(self.0 - rhs);
114 #[cfg(debug_assertions)]
115 result.check();
116 result
117 }
118 }
119
120 impl SubAssign<i64> for RataDie {
121 fn sub_assign(&mut self, rhs: i64) {
122 self.0 -= rhs;
123 #[cfg(debug_assertions)]
124 self.check();
125 }
126 }
127
128 /// Calculate the number of days between two RataDie
129 impl Sub for RataDie {
130 type Output = i64;
131 fn sub(self, rhs: Self) -> Self::Output {
132 self.0 - rhs.0
133 }
134 }
135
136 /// A moment is a RataDie with a fractional part giving the time of day.
137 ///
138 /// NOTE: This should not cause overflow errors for most cases, but consider
139 /// alternative implementations if necessary.
140 #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
141 pub struct Moment(f64);
142
143 /// Add a number of days to a Moment
144 impl Add<f64> for Moment {
145 type Output = Self;
146 fn add(self, rhs: f64) -> Self::Output {
147 Self(self.0 + rhs)
148 }
149 }
150
151 impl AddAssign<f64> for Moment {
152 fn add_assign(&mut self, rhs: f64) {
153 self.0 += rhs;
154 }
155 }
156
157 /// Subtract a number of days from a Moment
158 impl Sub<f64> for Moment {
159 type Output = Self;
160 fn sub(self, rhs: f64) -> Self::Output {
161 Self(self.0 - rhs)
162 }
163 }
164
165 impl SubAssign<f64> for Moment {
166 fn sub_assign(&mut self, rhs: f64) {
167 self.0 -= rhs;
168 }
169 }
170
171 /// Calculate the number of days between two moments
172 impl Sub for Moment {
173 type Output = f64;
174 fn sub(self, rhs: Self) -> Self::Output {
175 self.0 - rhs.0
176 }
177 }
178
179 impl Moment {
180 /// Create a new moment
181 pub const fn new(value: f64) -> Moment {
182 Moment(value)
183 }
184
185 /// Get the inner field of a Moment
186 pub const fn inner(&self) -> f64 {
187 self.0
188 }
189
190 /// Get the RataDie of a Moment
191 pub fn as_rata_die(&self) -> RataDie {
192 RataDie::new(self.0.floor() as i64)
193 }
194 }
195
196 #[test]
197 fn test_moment_to_rata_die_conversion() {
198 for i in -1000..=1000 {
199 let moment = Moment::new(i as f64);
200 let rata_die = moment.as_rata_die();
201 assert_eq!(rata_die.to_i64_date(), i);
202 }
203 }