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