/home/a220/proj/radnelac/src/display/holocene.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::calendar::CommonWeekOfYear; |
6 | | use crate::calendar::Holocene; |
7 | | use crate::calendar::ToFromCommonDate; |
8 | | use crate::calendar::ToFromOrdinalDate; |
9 | | use crate::clock::TimeOfDay; |
10 | | use crate::day_count::ToFixed; |
11 | | use crate::day_cycle::Weekday; |
12 | | use crate::display::moment::DisplayMomentItem; |
13 | | use crate::display::prelude::PresetDisplay; |
14 | | use crate::display::prelude::YYYYYMMDD_DASH; |
15 | | use crate::display::private::fmt_days_since_epoch; |
16 | | use crate::display::private::fmt_number; |
17 | | use crate::display::private::fmt_quarter; |
18 | | use crate::display::private::fmt_seconds_since_epoch; |
19 | | use crate::display::private::fmt_string; |
20 | | use crate::display::private::get_dict; |
21 | | use crate::display::private::DisplayItem; |
22 | | use crate::display::private::DisplayOptions; |
23 | | use crate::display::private::NumericContent; |
24 | | use crate::display::private::TextContent; |
25 | | use crate::display::text::prelude::Language; |
26 | | use std::fmt; |
27 | | //use crate::calendar::HoloceneMonth; |
28 | | |
29 | | impl DisplayItem for Holocene { |
30 | 514 | fn supported_lang(lang: Language) -> bool { |
31 | 514 | get_dict(lang).holocene.as_ref().is_some() |
32 | 514 | } |
33 | | |
34 | 18.7k | fn fmt_numeric(&self, n: NumericContent, opt: DisplayOptions) -> String { |
35 | 18.7k | match n { |
36 | | NumericContent::Month | NumericContent::DayOfMonth | NumericContent::Year => { |
37 | 16.4k | self.to_common_date().fmt_numeric(n, opt) |
38 | | } |
39 | 0 | NumericContent::DayOfWeek => self.convert::<Weekday>().fmt_numeric(n, opt), |
40 | 512 | NumericContent::DayOfYear => self.to_ordinal().fmt_numeric(n, opt), |
41 | | NumericContent::Hour1to12 |
42 | | | NumericContent::Hour0to23 |
43 | | | NumericContent::Minute |
44 | 768 | | NumericContent::Second => self.convert::<TimeOfDay>().fmt_numeric(n, opt), |
45 | 512 | NumericContent::SecondsSinceEpoch => fmt_seconds_since_epoch(*self, opt), |
46 | 0 | NumericContent::Quarter => fmt_quarter(*self, opt), |
47 | 512 | NumericContent::DaysSinceEpoch => fmt_days_since_epoch(*self, opt), |
48 | 0 | NumericContent::ComplementaryDay => String::from(""), |
49 | 0 | NumericContent::WeekOfYear => fmt_number(self.week_of_year() as i16, opt), |
50 | | } |
51 | 18.7k | } |
52 | | |
53 | 12.5k | fn fmt_text(&self, t: TextContent, lang: Language, opt: DisplayOptions) -> String { |
54 | 12.5k | match (t, get_dict(lang).holocene.as_ref()) { |
55 | 4.10k | (TextContent::MonthName, Some(dict)) => { |
56 | 4.10k | let months: [&str; 12] = [ |
57 | 4.10k | dict.january, |
58 | 4.10k | dict.february, |
59 | 4.10k | dict.march, |
60 | 4.10k | dict.april, |
61 | 4.10k | dict.may, |
62 | 4.10k | dict.june, |
63 | 4.10k | dict.july, |
64 | 4.10k | dict.august, |
65 | 4.10k | dict.september, |
66 | 4.10k | dict.october, |
67 | 4.10k | dict.november, |
68 | 4.10k | dict.december, |
69 | 4.10k | ]; |
70 | 4.10k | let name = months[self.to_common_date().month as usize - 1]; |
71 | 4.10k | fmt_string(name, opt) |
72 | | } |
73 | 0 | (TextContent::DayOfMonthName, _) => fmt_string("", opt), |
74 | 4.10k | (TextContent::DayOfWeekName, _) => self.convert::<Weekday>().fmt_text(t, lang, opt), |
75 | | (TextContent::HalfDayName | TextContent::HalfDayAbbrev, _) => { |
76 | 0 | self.convert::<TimeOfDay>().fmt_text(t, lang, opt) |
77 | | } |
78 | 3.07k | (TextContent::EraName, Some(dict)) => { |
79 | 3.07k | if self.to_common_date().year < 0 { |
80 | 1.49k | fmt_string(dict.before_human_era_full, opt) |
81 | | } else { |
82 | 1.58k | fmt_string(dict.human_era_full, opt) |
83 | | } |
84 | | } |
85 | 1.02k | (TextContent::EraAbbreviation, Some(dict)) => { |
86 | 1.02k | if self.to_common_date().year < 0 { |
87 | 502 | fmt_string(dict.before_human_era_abr, opt) |
88 | | } else { |
89 | 522 | fmt_string(dict.human_era_abr, opt) |
90 | | } |
91 | | } |
92 | 256 | (_, _) => String::from(""), |
93 | | } |
94 | 12.5k | } |
95 | | } |
96 | | |
97 | | impl PresetDisplay for Holocene { |
98 | 12 | fn short_date(&self) -> String { |
99 | 12 | self.preset_str(Language::EN, YYYYYMMDD_DASH) |
100 | 12 | } |
101 | | } |
102 | | |
103 | | impl fmt::Display for Holocene { |
104 | 2.56k | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
105 | 2.56k | write!(f, "{}", self.long_date()) |
106 | 2.56k | } |
107 | | } |
108 | | |
109 | | impl DisplayMomentItem for Holocene {} |
110 | | |
111 | | #[cfg(test)] |
112 | | mod tests { |
113 | | use super::*; |
114 | | use crate::calendar::CommonDate; |
115 | | |
116 | | #[test] |
117 | 1 | fn expected_languages() { |
118 | 1 | assert!(Holocene::supported_lang(Language::EN)); |
119 | 1 | assert!(Holocene::supported_lang(Language::FR)); |
120 | 1 | } |
121 | | |
122 | | #[test] |
123 | 1 | fn long_date() { |
124 | 1 | let d_list = [ |
125 | 1 | ( |
126 | 1 | CommonDate::new(11582, 10, 15), |
127 | 1 | "Friday October 15, 11582 Human Era", |
128 | 1 | ), |
129 | 1 | ( |
130 | 1 | CommonDate::new(12012, 12, 21), |
131 | 1 | "Friday December 21, 12012 Human Era", |
132 | 1 | ), |
133 | 1 | ( |
134 | 1 | CommonDate::new(12025, 1, 1), |
135 | 1 | "Wednesday January 1, 12025 Human Era", |
136 | 1 | ), |
137 | 1 | ( |
138 | 1 | CommonDate::new(12025, 6, 29), |
139 | 1 | "Sunday June 29, 12025 Human Era", |
140 | 1 | ), |
141 | 1 | ( |
142 | 1 | CommonDate::new(12025, 6, 30), |
143 | 1 | "Monday June 30, 12025 Human Era", |
144 | 1 | ), |
145 | 1 | ( |
146 | 1 | CommonDate::new(12025, 7, 1), |
147 | 1 | "Tuesday July 1, 12025 Human Era", |
148 | 1 | ), |
149 | 1 | ]; |
150 | | |
151 | 7 | for item6 in d_list { |
152 | 6 | let d = Holocene::try_from_common_date(item.0).unwrap(); |
153 | 6 | let s = d.long_date(); |
154 | 6 | assert_eq!(s, item.1); |
155 | | } |
156 | 1 | } |
157 | | |
158 | | #[test] |
159 | 1 | fn short_date() { |
160 | 1 | let d_list = [ |
161 | 1 | (CommonDate::new(1582, 10, 15), "01582-10-15"), |
162 | 1 | (CommonDate::new(2012, 12, 21), "02012-12-21"), |
163 | 1 | (CommonDate::new(2025, 1, 1), "02025-01-01"), |
164 | 1 | (CommonDate::new(2025, 6, 29), "02025-06-29"), |
165 | 1 | (CommonDate::new(2025, 6, 30), "02025-06-30"), |
166 | 1 | (CommonDate::new(2025, 7, 1), "02025-07-01"), |
167 | 1 | (CommonDate::new(11582, 10, 15), "11582-10-15"), |
168 | 1 | (CommonDate::new(12012, 12, 21), "12012-12-21"), |
169 | 1 | (CommonDate::new(12025, 1, 1), "12025-01-01"), |
170 | 1 | (CommonDate::new(12025, 6, 29), "12025-06-29"), |
171 | 1 | (CommonDate::new(12025, 6, 30), "12025-06-30"), |
172 | 1 | (CommonDate::new(12025, 7, 1), "12025-07-01"), |
173 | 1 | ]; |
174 | | |
175 | 13 | for item12 in d_list { |
176 | 12 | let d = Holocene::try_from_common_date(item.0).unwrap(); |
177 | 12 | let s = d.short_date(); |
178 | 12 | assert_eq!(s, item.1); |
179 | | } |
180 | 1 | } |
181 | | } |