/home/a220/proj/radnelac/src/display/cotsworth.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::Cotsworth; |
6 | | use crate::calendar::HasIntercalaryDays; |
7 | | use crate::calendar::Perennial; |
8 | | use crate::calendar::ToFromCommonDate; |
9 | | use crate::calendar::ToFromOrdinalDate; |
10 | | use crate::clock::TimeOfDay; |
11 | | use crate::day_count::ToFixed; |
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 crate::display::LONG_COMPL; |
26 | | use crate::display::LONG_DATE; |
27 | | use std::fmt; |
28 | | |
29 | | impl DisplayItem for Cotsworth { |
30 | 514 | fn supported_lang(lang: Language) -> bool { |
31 | 514 | get_dict(lang).cotsworth.as_ref().is_some() |
32 | 514 | } |
33 | | |
34 | 20.2k | fn fmt_numeric(&self, n: NumericContent, opt: DisplayOptions) -> String { |
35 | 20.2k | match n { |
36 | | NumericContent::Month | NumericContent::DayOfMonth | NumericContent::Year => { |
37 | 16.8k | self.to_common_date().fmt_numeric(n, opt) |
38 | | } |
39 | 512 | NumericContent::DayOfWeek => match self.weekday() { |
40 | 512 | Some(d) => d.fmt_numeric(n, opt), |
41 | 0 | None => "".to_string(), |
42 | | }, |
43 | 512 | NumericContent::DayOfYear => self.to_ordinal().fmt_numeric(n, opt), |
44 | | NumericContent::Hour1to12 |
45 | | | NumericContent::Hour0to23 |
46 | | | NumericContent::Minute |
47 | 768 | | NumericContent::Second => self.convert::<TimeOfDay>().fmt_numeric(n, opt), |
48 | 512 | NumericContent::SecondsSinceEpoch => fmt_seconds_since_epoch(*self, opt), |
49 | 0 | NumericContent::Quarter => fmt_quarter(*self, opt), |
50 | 512 | NumericContent::DaysSinceEpoch => fmt_days_since_epoch(*self, opt), |
51 | 0 | NumericContent::ComplementaryDay => match self.complementary() { |
52 | 0 | Some(d) => fmt_number(d as i8, opt), |
53 | 0 | None => "".to_string(), |
54 | | }, |
55 | 512 | NumericContent::WeekOfYear => match self.try_week_of_year() { |
56 | 512 | Some(w) => fmt_number(w as i8, opt), |
57 | 0 | None => "".to_string(), |
58 | | }, |
59 | | } |
60 | 20.2k | } |
61 | 13.8k | fn fmt_text(&self, t: TextContent, lang: Language, opt: DisplayOptions) -> String { |
62 | 13.8k | match (t, get_dict(lang).cotsworth.as_ref()) { |
63 | 4.09k | (TextContent::MonthName, Some(dict)) => { |
64 | 4.09k | let months: [&str; 13] = [ |
65 | 4.09k | dict.january, |
66 | 4.09k | dict.february, |
67 | 4.09k | dict.march, |
68 | 4.09k | dict.april, |
69 | 4.09k | dict.may, |
70 | 4.09k | dict.june, |
71 | 4.09k | dict.sol, |
72 | 4.09k | dict.july, |
73 | 4.09k | dict.august, |
74 | 4.09k | dict.september, |
75 | 4.09k | dict.october, |
76 | 4.09k | dict.november, |
77 | 4.09k | dict.december, |
78 | 4.09k | ]; |
79 | 4.09k | let name = match self.try_month() { |
80 | 4.09k | Some(m) => months[(m as usize) - 1], |
81 | 0 | None => "", |
82 | | }; |
83 | 4.09k | fmt_string(name, opt) |
84 | | } |
85 | 4.09k | (TextContent::DayOfWeekName, _) => match self.weekday() { |
86 | 4.08k | Some(m) => m.fmt_text(t, lang, opt), |
87 | 14 | None => fmt_string("", opt), |
88 | | }, |
89 | | (TextContent::HalfDayName | TextContent::HalfDayAbbrev, _) => { |
90 | 0 | self.convert::<TimeOfDay>().fmt_text(t, lang, opt) |
91 | | } |
92 | 3.07k | (TextContent::EraName, Some(dict)) => { |
93 | 3.07k | if self.to_common_date().year < 0 { |
94 | 1.55k | fmt_string(dict.before_epoch_full, opt) |
95 | | } else { |
96 | 1.51k | fmt_string(dict.after_epoch_full, opt) |
97 | | } |
98 | | } |
99 | 1.02k | (TextContent::EraAbbreviation, Some(dict)) => { |
100 | 1.02k | if self.to_common_date().year < 0 { |
101 | 486 | fmt_string(dict.before_epoch_abr, opt) |
102 | | } else { |
103 | 538 | fmt_string(dict.after_epoch_abr, opt) |
104 | | } |
105 | | } |
106 | 1.53k | (TextContent::ComplementaryDayName, Some(dict)) => { |
107 | 1.53k | let compl: [&str; 2] = [dict.year_day, dict.leap_day]; |
108 | 1.53k | let name = match self.complementary() { |
109 | 1.02k | Some(d) => compl[(d as usize) - 1], |
110 | 512 | None => "", |
111 | | }; |
112 | 1.53k | fmt_string(name, opt) |
113 | | } |
114 | 0 | (_, _) => fmt_string("", opt), |
115 | | } |
116 | 13.8k | } |
117 | | } |
118 | | |
119 | | impl PresetDisplay for Cotsworth { |
120 | 2.56k | fn long_date(&self) -> String { |
121 | | //Avoid non-existent weekday |
122 | 2.56k | self.preset_str(Language::EN, LONG_DATE).trim().to_string() |
123 | 2.56k | } |
124 | | } |
125 | | |
126 | | impl fmt::Display for Cotsworth { |
127 | 2.56k | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
128 | 2.56k | write!(f, "{}", self.long_date()) |
129 | 2.56k | } |
130 | | } |
131 | | |
132 | | impl DisplayMomentItem for Cotsworth {} |
133 | | |
134 | | #[cfg(test)] |
135 | | mod tests { |
136 | | use super::*; |
137 | | |
138 | | #[test] |
139 | 1 | fn expected_languages() { |
140 | 1 | assert!(Cotsworth::supported_lang(Language::EN)); |
141 | 1 | assert!(Cotsworth::supported_lang(Language::FR)); |
142 | 1 | } |
143 | | } |