/home/a220/proj/radnelac/src/day_cycle/week.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::BoundedDayCount; |
6 | | use crate::day_count::Fixed; |
7 | | use crate::day_count::FromFixed; |
8 | | use crate::day_cycle::BoundedCycle; |
9 | | use crate::day_cycle::OnOrBefore; |
10 | | #[allow(unused_imports)] //FromPrimitive is needed for derive |
11 | | use num_traits::FromPrimitive; |
12 | | |
13 | | /// Represents a day in the common week cycle |
14 | | /// |
15 | | /// Note that some calendars such as the ISO calendar assign different numbers to each day. |
16 | | /// See `ISO` for details about that. |
17 | | /// |
18 | | /// Additionally, some calendars use different week cycles than the common week cycle. Some |
19 | | /// calendars have seperate data types representing their weekdays, such as `FrenchRevArith` |
20 | | /// which has a ten-day week. Other calendars which use the same names for their weekdays |
21 | | /// re-use the `Weekday` struct such as `Cotsworth`. Monday in the common week cycle does |
22 | | /// not necessarily correspond to Monday in the Cotsworth calendar. |
23 | | #[derive(Debug, PartialEq, PartialOrd, Clone, Copy, FromPrimitive, ToPrimitive)] |
24 | | pub enum Weekday { |
25 | | //LISTING 1.53-1.59 (*Calendrical Calculations: The Ultimate Edition* by Reingold & Dershowitz.) |
26 | | Sunday = 0, |
27 | | Monday, |
28 | | Tuesday, |
29 | | Wednesday, |
30 | | Thursday, |
31 | | Friday, |
32 | | Saturday, |
33 | | } |
34 | | |
35 | | impl BoundedCycle<7, 0> for Weekday {} |
36 | | |
37 | | impl FromFixed for Weekday { |
38 | 8.42k | fn from_fixed(t: Fixed) -> Weekday { |
39 | | //LISTING 1.60 (*Calendrical Calculations: The Ultimate Edition* by Reingold & Dershowitz.) |
40 | | //Modified to remove superfluous terms, modulus is in BoundedCycle |
41 | 8.42k | Weekday::from_unbounded(t.get_day_i()) |
42 | 8.42k | } |
43 | | } |
44 | | |
45 | | impl OnOrBefore<7, 0> for Weekday { |
46 | 20.0k | fn raw_on_or_before(self, date: i64) -> Fixed { |
47 | | //LISTING 1.62 (*Calendrical Calculations: The Ultimate Edition* by Reingold & Dershowitz.) |
48 | 20.0k | let k = self.to_unbounded(); |
49 | 20.0k | Fixed::cast_new(date - (Weekday::from_unbounded(date - k) as i64)) |
50 | 20.0k | } |
51 | | } |
52 | | |
53 | | #[cfg(test)] |
54 | | mod tests { |
55 | | use super::*; |
56 | | use crate::day_count::FIXED_MAX; |
57 | | use crate::day_count::FIXED_MIN; |
58 | | use proptest::proptest; |
59 | | |
60 | | proptest! { |
61 | | #[test] |
62 | | fn day_of_week_sequence(x in (FIXED_MIN+14.0)..(FIXED_MAX - 14.0)) { |
63 | | let w = Weekday::Sunday; |
64 | | let d0 = w.on_or_before(Fixed::new(x)); |
65 | | let d1 = Fixed::new(d0.get() + 1.0); |
66 | | let d2 = Fixed::new(d0.get() + 2.0); |
67 | | let d3 = Fixed::new(d0.get() + 3.0); |
68 | | let d4 = Fixed::new(d0.get() + 4.0); |
69 | | let d5 = Fixed::new(d0.get() + 5.0); |
70 | | let d6 = Fixed::new(d0.get() + 6.0); |
71 | | assert_eq!(Weekday::from_fixed(d0), Weekday::Sunday); |
72 | | assert_eq!(Weekday::from_fixed(d1), Weekday::Monday); |
73 | | assert_eq!(Weekday::from_fixed(d2), Weekday::Tuesday); |
74 | | assert_eq!(Weekday::from_fixed(d3), Weekday::Wednesday); |
75 | | assert_eq!(Weekday::from_fixed(d4), Weekday::Thursday); |
76 | | assert_eq!(Weekday::from_fixed(d5), Weekday::Friday); |
77 | | assert_eq!(Weekday::from_fixed(d6), Weekday::Saturday); |
78 | | } |
79 | | } |
80 | | } |