diff --git a/build.rs b/build.rs index 48e83a8..c4174b7 100644 --- a/build.rs +++ b/build.rs @@ -110,7 +110,7 @@ fn main() { } if let Some(ampm) = locale_data.ampm { - if ampm.len() == 2 { + if ampm.len() == 2 || ampm.len() == 4 { let _ = f .write_all( format!( diff --git a/locales/C.json b/locales/C.json index ee37b53..8b2a461 100644 --- a/locales/C.json +++ b/locales/C.json @@ -47,6 +47,8 @@ ], "ampm": [ "am", - "pm" + "pm", + "AM", + "PM" ] } diff --git a/src/lib.rs b/src/lib.rs index 194e361..ffe4fc0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -192,7 +192,7 @@ where ShortWeekdayName => date.map(|d| write!(w, "{}", short_weekday(d.weekday().num_days_from_monday() as usize, locale))), LongWeekdayName => date.map(|d| write!(w, "{}", long_weekday(d.weekday().num_days_from_monday() as usize, locale))), LowerAmPm => time.map(|t| write!(w, "{}", ampm(t.hour12().0 as usize, locale))), - UpperAmPm => time.map(|t| write!(w, "{}", ampm(t.hour12().0 as usize, locale).to_uppercase())), + UpperAmPm => time.map(|t| write!(w, "{}", ampm(t.hour12().0 as usize + 2, locale))), Nanosecond => time.map(|t| { let nano = t.nanosecond() % 1_000_000_000; if nano == 0 { @@ -271,47 +271,47 @@ where Ok(()) } -fn short_month(month: usize, locale: &str) -> String { +fn short_month(month: usize, locale: &str) -> &'static str { locales::LOCALES .short_months .get(locale) - .or_else(|| locales::LOCALES.short_months.get("C")) - .and_then(|res| res.get(month).map(|v| v.to_string())) - .unwrap_or_else(|| format!("{}", month)) + .and_then(|res| res.get(month)) + .or_else(|| locales::LOCALES.short_months.get("C").and_then(|res| res.get(month))) + .expect("Internal error: missing short months in the C locale") } -fn long_month(month: usize, locale: &str) -> String { +fn long_month(month: usize, locale: &str) -> &'static str { locales::LOCALES .long_months .get(locale) - .or_else(|| locales::LOCALES.long_months.get("C")) - .and_then(|res| res.get(month).map(|v| v.to_string())) - .unwrap_or_else(|| format!("{}", month)) + .and_then(|res| res.get(month)) + .or_else(|| locales::LOCALES.long_months.get("C").and_then(|res| res.get(month))) + .expect("Internal error: missing long months in the C locale") } -fn short_weekday(day: usize, locale: &str) -> String { +fn short_weekday(day: usize, locale: &str) -> &'static str { locales::LOCALES .short_weekdays .get(locale) - .or_else(|| locales::LOCALES.short_weekdays.get("C")) - .and_then(|res| res.get(day).map(|v| v.to_string())) - .unwrap_or_else(|| format!("{}", day)) + .and_then(|res| res.get(day)) + .or_else(|| locales::LOCALES.short_weekdays.get("C").and_then(|res| res.get(day))) + .expect("Internal error: missing short weekdays in the C locale") } -fn long_weekday(day: usize, locale: &str) -> String { +fn long_weekday(day: usize, locale: &str) -> &'static str { locales::LOCALES .long_weekdays .get(locale) - .or_else(|| locales::LOCALES.long_weekdays.get("C")) - .and_then(|res| res.get(day).map(|v| v.to_string())) - .unwrap_or_else(|| format!("{}", day)) + .and_then(|res| res.get(day)) + .or_else(|| locales::LOCALES.long_weekdays.get("C").and_then(|res| res.get(day))) + .expect("Internal error: missing long weekdays in the C locale") } -fn ampm(spec: usize, locale: &str) -> String { +fn ampm(spec: usize, locale: &str) -> &'static str { locales::LOCALES .ampm .get(locale) - .or_else(|| locales::LOCALES.ampm.get("C")) - .and_then(|res| res.get(spec).map(|v| v.to_string())) - .unwrap_or_else(|| format!("{}", spec)) + .and_then(|res| res.get(spec)) + .or_else(|| locales::LOCALES.ampm.get("C").and_then(|res| res.get(spec))) + .expect("Internal error: missing AM/PM in the C locale") }