/home/a220/proj/radnelac/src/display/clock.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::clock::ClockTime; |
6 | | use crate::clock::TimeOfDay; |
7 | | use crate::common::math::TermNum; |
8 | | use crate::display::prelude::PresetDisplay; |
9 | | use crate::display::prelude::HHMMSS_COLON; |
10 | | use crate::display::private::fmt_number; |
11 | | use crate::display::private::fmt_string; |
12 | | use crate::display::private::get_dict; |
13 | | use crate::display::private::DisplayItem; |
14 | | use crate::display::private::DisplayOptions; |
15 | | use crate::display::private::NumericContent; |
16 | | use crate::display::private::TextContent; |
17 | | use crate::display::text::prelude::Language; |
18 | | use std::fmt; |
19 | | |
20 | | impl DisplayItem for ClockTime { |
21 | 1 | fn supported_lang(lang: Language) -> bool { |
22 | 1 | get_dict(lang).common_clock.as_ref().is_some() |
23 | 1 | } |
24 | | |
25 | 78.5k | fn fmt_numeric(&self, n: NumericContent, opt: DisplayOptions) -> String { |
26 | 78.5k | match n { |
27 | 512 | NumericContent::Hour1to12 => fmt_number(self.hour_1_to_12() as i64, opt), |
28 | 25.8k | NumericContent::Hour0to23 => fmt_number(self.hours as i16, opt), |
29 | 26.3k | NumericContent::Minute => fmt_number(self.minutes as i16, opt), |
30 | 25.8k | NumericContent::Second => fmt_number(self.seconds as i16, opt), |
31 | 0 | _ => "".to_string(), |
32 | | } |
33 | 78.5k | } |
34 | | |
35 | 512 | fn fmt_text(&self, t: TextContent, lang: Language, opt: DisplayOptions) -> String { |
36 | 512 | let dict_opt = get_dict(lang).common_clock.as_ref(); |
37 | 512 | let before_noon = *self < TimeOfDay::noon().to_clock(); |
38 | 512 | match (t, dict_opt, before_noon) { |
39 | 0 | (TextContent::HalfDayName, Some(dict), true) => fmt_string(dict.am_full, opt), |
40 | 0 | (TextContent::HalfDayName, Some(dict), false) => fmt_string(dict.pm_full, opt), |
41 | 256 | (TextContent::HalfDayAbbrev, Some(dict), true) => fmt_string(dict.am_abr, opt), |
42 | 256 | (TextContent::HalfDayAbbrev, Some(dict), false) => fmt_string(dict.pm_abr, opt), |
43 | 0 | (_, _, _) => "".to_string(), |
44 | | } |
45 | 512 | } |
46 | | } |
47 | | |
48 | | impl PresetDisplay for ClockTime {} |
49 | | |
50 | | impl fmt::Display for ClockTime { |
51 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
52 | 0 | write!(f, "{}", self.preset_str(Language::EN, HHMMSS_COLON)) |
53 | 0 | } |
54 | | } |
55 | | |
56 | | impl DisplayItem for TimeOfDay { |
57 | 2 | fn supported_lang(lang: Language) -> bool { |
58 | 2 | get_dict(lang).common_clock.as_ref().is_some() |
59 | 2 | } |
60 | | |
61 | 16.3k | fn fmt_numeric(&self, n: NumericContent, opt: DisplayOptions) -> String { |
62 | 16.3k | self.to_clock().fmt_numeric(n, opt) |
63 | 16.3k | } |
64 | | |
65 | 512 | fn fmt_text(&self, t: TextContent, lang: Language, opt: DisplayOptions) -> String { |
66 | 512 | self.to_clock().fmt_text(t, lang, opt) |
67 | 512 | } |
68 | | } |
69 | | |
70 | | impl PresetDisplay for TimeOfDay {} |
71 | | |
72 | | impl fmt::Display for TimeOfDay { |
73 | 256 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
74 | 256 | write!(f, "{}", self.preset_str(Language::EN, HHMMSS_COLON)) |
75 | 256 | } |
76 | | } |
77 | | |
78 | | #[cfg(test)] |
79 | | mod tests { |
80 | | use super::*; |
81 | | |
82 | | #[test] |
83 | 1 | fn expected_languages() { |
84 | 1 | assert!(TimeOfDay::supported_lang(Language::EN)); |
85 | 1 | assert!(TimeOfDay::supported_lang(Language::FR)); |
86 | 1 | } |
87 | | } |