time_fmt/
util.rs

1use time::{Month, Weekday};
2
3const MONTH_LONG: [&str; 12] = [
4    "January",
5    "February",
6    "March",
7    "April",
8    "May",
9    "June",
10    "July",
11    "August",
12    "September",
13    "October",
14    "November",
15    "December",
16];
17const MONTH_SHORT: [&str; 12] = [
18    "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
19];
20const MONTHS0: [Month; 12] = [
21    Month::January,
22    Month::February,
23    Month::March,
24    Month::April,
25    Month::May,
26    Month::June,
27    Month::July,
28    Month::August,
29    Month::September,
30    Month::October,
31    Month::November,
32    Month::December,
33];
34const WEEKDAY_LONG: [&str; 7] = [
35    "Monday",
36    "Tuesday",
37    "Wednesday",
38    "Thursday",
39    "Friday",
40    "Saturday",
41    "Sunday",
42];
43const WEEKDAY_SHORT: [&str; 7] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
44const AMPM_UPPER: [&str; 2] = ["AM", "PM"];
45const AMPM_LOWER: [&str; 2] = ["am", "pm"];
46
47#[inline]
48pub(crate) const fn ampm_upper(hour: u8) -> &'static str {
49    AMPM_UPPER[if hour < 12 { 0 } else { 1 }]
50}
51#[inline]
52pub(crate) const fn ampm_lower(hour: u8) -> &'static str {
53    AMPM_LOWER[if hour < 12 { 0 } else { 1 }]
54}
55#[inline]
56pub(crate) const fn month_long_str(month: Month) -> &'static str {
57    MONTH_LONG[(month as u8 - 1) as usize]
58}
59#[inline]
60pub(crate) const fn month_short_str(month: Month) -> &'static str {
61    MONTH_SHORT[(month as u8 - 1) as usize]
62}
63#[inline]
64pub(crate) const fn weekday_long_str(weekday: Weekday) -> &'static str {
65    WEEKDAY_LONG[weekday as u8 as usize]
66}
67#[inline]
68pub(crate) const fn weekday_short_str(weekday: Weekday) -> &'static str {
69    WEEKDAY_SHORT[weekday as u8 as usize]
70}
71#[inline]
72pub(crate) const fn get_month(month: u8) -> Option<Month> {
73    // TODO: Use MONTHD0.get((month - 1) as usize).copied() once copied() get to a const fn in
74    // stable.
75    if 1 <= month && month <= 12 {
76        Some(MONTHS0[(month - 1) as usize])
77    } else {
78        None
79    }
80}