/home/a220/proj/radnelac/src/display/egyptian.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::Egyptian; |
7 | | use crate::calendar::HasIntercalaryDays; |
8 | | use crate::calendar::ToFromCommonDate; |
9 | | use crate::calendar::ToFromOrdinalDate; |
10 | | use crate::clock::TimeOfDay; |
11 | | use crate::day_count::ToFixed; |
12 | | use crate::day_cycle::Weekday; |
13 | | use crate::display::moment::DisplayMomentItem; |
14 | | use crate::display::prelude::PresetDisplay; |
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::NumericContent; |
23 | | use crate::display::text::prelude::Language; |
24 | | use crate::display::LONG_COMPL; |
25 | | use crate::display::LONG_DATE; |
26 | | use std::fmt; |
27 | | |
28 | | use crate::display::private::TextContent; |
29 | | |
30 | | use crate::display::private::DisplayOptions; |
31 | | |
32 | | impl DisplayItem for Egyptian { |
33 | 1 | fn supported_lang(lang: Language) -> bool { |
34 | 1 | get_dict(lang).egyptian.as_ref().is_some() |
35 | 1 | } |
36 | | |
37 | 17.5k | fn fmt_numeric(&self, n: NumericContent, opt: DisplayOptions) -> String { |
38 | 17.5k | match n { |
39 | | NumericContent::Month | NumericContent::DayOfMonth | NumericContent::Year => { |
40 | 15.2k | self.to_common_date().fmt_numeric(n, opt) |
41 | | } |
42 | 0 | NumericContent::DayOfWeek => self.convert::<Weekday>().fmt_numeric(n, opt), |
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 i16, opt), |
53 | 0 | None => "".to_string(), |
54 | | }, |
55 | 0 | NumericContent::WeekOfYear => fmt_number(self.week_of_year() as i16, opt), |
56 | | } |
57 | 17.5k | } |
58 | 10.6k | fn fmt_text(&self, t: TextContent, lang: Language, opt: DisplayOptions) -> String { |
59 | 10.6k | match (t, get_dict(lang).egyptian.as_ref()) { |
60 | 3.51k | (TextContent::MonthName, Some(dict)) => { |
61 | 3.51k | let months: [&str; 13] = [ |
62 | 3.51k | dict.thoth, |
63 | 3.51k | dict.phaophi, |
64 | 3.51k | dict.athyr, |
65 | 3.51k | dict.choiak, |
66 | 3.51k | dict.tybi, |
67 | 3.51k | dict.mechir, |
68 | 3.51k | dict.phamenoth, |
69 | 3.51k | dict.pharmuthi, |
70 | 3.51k | dict.pachon, |
71 | 3.51k | dict.payni, |
72 | 3.51k | dict.epiphi, |
73 | 3.51k | dict.mesori, |
74 | 3.51k | dict.epagomenae, |
75 | 3.51k | ]; |
76 | 3.51k | let m = self.to_common_date().month; |
77 | 3.51k | let name = months[m as usize - 1]; |
78 | 3.51k | fmt_string(name, opt) |
79 | | } |
80 | 0 | (TextContent::DayOfMonthName, _) => fmt_string("", opt), |
81 | 3.51k | (TextContent::DayOfWeekName, _) => self.convert::<Weekday>().fmt_text(t, lang, opt), |
82 | | (TextContent::HalfDayName | TextContent::HalfDayAbbrev, _) => { |
83 | 0 | self.convert::<TimeOfDay>().fmt_text(t, lang, opt) |
84 | | } |
85 | 2.52k | (TextContent::EraName, Some(dict)) => { |
86 | 2.52k | if self.to_common_date().year < 0 { |
87 | 1.26k | fmt_string(dict.before_nabonassar_full, opt) |
88 | | } else { |
89 | 1.26k | fmt_string(dict.after_nabonassar_full, opt) |
90 | | } |
91 | | } |
92 | 1.02k | (TextContent::EraAbbreviation, Some(dict)) => { |
93 | 1.02k | if self.to_common_date().year < 0 { |
94 | 504 | fmt_string(dict.before_nabonassar_abr, opt) |
95 | | } else { |
96 | 520 | fmt_string(dict.after_nabonassar_abr, opt) |
97 | | } |
98 | | } |
99 | 38 | (TextContent::ComplementaryDayName, Some(dict)) => { |
100 | | // https://helda.helsinki.fi/server/api/core/bitstreams/4ed34849-903b-416b-97d6-a9a76eb1fb1d/content |
101 | 38 | let days: [&str; 5] = [ |
102 | 38 | dict.birth_of_osiris, |
103 | 38 | dict.birth_of_horus, |
104 | 38 | dict.birth_of_seth, |
105 | 38 | dict.birth_of_isis, |
106 | 38 | dict.birth_of_nephthys, |
107 | 38 | ]; |
108 | 38 | match self.complementary() { |
109 | 38 | Some(d) => fmt_string(days[d as usize - 1], opt), |
110 | 0 | None => fmt_string("", opt), |
111 | | } |
112 | | } |
113 | 0 | (_, _) => fmt_string("", opt), |
114 | | } |
115 | 10.6k | } |
116 | | } |
117 | | |
118 | | impl PresetDisplay for Egyptian { |
119 | 2.52k | fn long_date(&self) -> String { |
120 | 2.52k | let p = match self.complementary() { |
121 | 2.48k | None => LONG_DATE, |
122 | 38 | Some(_) => LONG_COMPL, |
123 | | }; |
124 | 2.52k | self.preset_str(Language::EN, p) |
125 | 2.52k | } |
126 | | } |
127 | | |
128 | | impl fmt::Display for Egyptian { |
129 | 2.52k | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
130 | 2.52k | write!(f, "{}", self.long_date()) |
131 | 2.52k | } |
132 | | } |
133 | | |
134 | | impl DisplayMomentItem for Egyptian {} |
135 | | |
136 | | #[cfg(test)] |
137 | | mod tests { |
138 | | use super::*; |
139 | | |
140 | | #[test] |
141 | 1 | fn expected_languages() { |
142 | 1 | assert!(Egyptian::supported_lang(Language::EN)); |
143 | 1 | } |
144 | | } |