mirror of
https://github.com/chronotope/chrono.git
synced 2025-10-01 06:52:15 +00:00
extract out locales for a later change
This commit is contained in:
parent
66491783a2
commit
8706fe0862
@ -441,6 +441,63 @@ const TOO_SHORT: ParseError = ParseError(ParseErrorKind::TooShort);
|
||||
const TOO_LONG: ParseError = ParseError(ParseErrorKind::TooLong);
|
||||
const BAD_FORMAT: ParseError = ParseError(ParseErrorKind::BadFormat);
|
||||
|
||||
#[cfg(any(feature = "alloc", feature = "std", test))]
|
||||
struct Locales {
|
||||
short_months: &'static [&'static str],
|
||||
long_months: &'static [&'static str],
|
||||
short_weekdays: &'static [&'static str],
|
||||
long_weekdays: &'static [&'static str],
|
||||
am_pm: &'static [&'static str],
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "alloc", feature = "std", test))]
|
||||
impl Locales {
|
||||
fn new(_locale: Option<Locale>) -> Self {
|
||||
#[cfg(feature = "unstable-locales")]
|
||||
{
|
||||
let locale = _locale.unwrap_or(Locale::POSIX);
|
||||
Self {
|
||||
short_months: locales::short_months(locale),
|
||||
long_months: locales::long_months(locale),
|
||||
short_weekdays: locales::short_weekdays(locale),
|
||||
long_weekdays: locales::long_weekdays(locale),
|
||||
am_pm: locales::am_pm(locale),
|
||||
}
|
||||
}
|
||||
#[cfg(not(feature = "unstable-locales"))]
|
||||
Self {
|
||||
short_months: &[
|
||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
|
||||
],
|
||||
long_months: &[
|
||||
"January",
|
||||
"February",
|
||||
"March",
|
||||
"April",
|
||||
"May",
|
||||
"June",
|
||||
"July",
|
||||
"August",
|
||||
"September",
|
||||
"October",
|
||||
"November",
|
||||
"December",
|
||||
],
|
||||
short_weekdays: &["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
|
||||
long_weekdays: &[
|
||||
"Sunday",
|
||||
"Monday",
|
||||
"Tuesday",
|
||||
"Wednesday",
|
||||
"Thursday",
|
||||
"Friday",
|
||||
"Saturday",
|
||||
],
|
||||
am_pm: &["AM", "PM"],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Formats single formatting item
|
||||
#[cfg(any(feature = "alloc", feature = "std", test))]
|
||||
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
|
||||
@ -463,45 +520,9 @@ fn format_inner<'a>(
|
||||
time: Option<&NaiveTime>,
|
||||
off: Option<&(String, FixedOffset)>,
|
||||
item: &Item<'a>,
|
||||
_locale: Option<Locale>,
|
||||
locale: Option<Locale>,
|
||||
) -> fmt::Result {
|
||||
#[cfg(feature = "unstable-locales")]
|
||||
let (short_months, long_months, short_weekdays, long_weekdays, am_pm, am_pm_lowercase) = {
|
||||
let locale = _locale.unwrap_or(Locale::POSIX);
|
||||
let am_pm = locales::am_pm(locale);
|
||||
(
|
||||
locales::short_months(locale),
|
||||
locales::long_months(locale),
|
||||
locales::short_weekdays(locale),
|
||||
locales::long_weekdays(locale),
|
||||
am_pm,
|
||||
&[am_pm[0].to_lowercase(), am_pm[1].to_lowercase()],
|
||||
)
|
||||
};
|
||||
#[cfg(not(feature = "unstable-locales"))]
|
||||
let (short_months, long_months, short_weekdays, long_weekdays, am_pm, am_pm_lowercase) = {
|
||||
(
|
||||
&["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
|
||||
&[
|
||||
"January",
|
||||
"February",
|
||||
"March",
|
||||
"April",
|
||||
"May",
|
||||
"June",
|
||||
"July",
|
||||
"August",
|
||||
"September",
|
||||
"October",
|
||||
"November",
|
||||
"December",
|
||||
],
|
||||
&["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
|
||||
&["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
|
||||
&["AM", "PM"],
|
||||
&["am", "pm"],
|
||||
)
|
||||
};
|
||||
let locale = Locales::new(locale);
|
||||
|
||||
use num_integer::{div_floor, mod_floor};
|
||||
|
||||
@ -581,35 +602,38 @@ fn format_inner<'a>(
|
||||
let ret =
|
||||
match *spec {
|
||||
ShortMonthName => date.map(|d| {
|
||||
result.push_str(short_months[d.month0() as usize]);
|
||||
result.push_str(locale.short_months[d.month0() as usize]);
|
||||
Ok(())
|
||||
}),
|
||||
LongMonthName => date.map(|d| {
|
||||
result.push_str(long_months[d.month0() as usize]);
|
||||
result.push_str(locale.long_months[d.month0() as usize]);
|
||||
Ok(())
|
||||
}),
|
||||
ShortWeekdayName => date.map(|d| {
|
||||
result
|
||||
.push_str(short_weekdays[d.weekday().num_days_from_sunday() as usize]);
|
||||
result.push_str(
|
||||
locale.short_weekdays[d.weekday().num_days_from_sunday() as usize],
|
||||
);
|
||||
Ok(())
|
||||
}),
|
||||
LongWeekdayName => date.map(|d| {
|
||||
result.push_str(long_weekdays[d.weekday().num_days_from_sunday() as usize]);
|
||||
result.push_str(
|
||||
locale.long_weekdays[d.weekday().num_days_from_sunday() as usize],
|
||||
);
|
||||
Ok(())
|
||||
}),
|
||||
LowerAmPm => time.map(|t| {
|
||||
#[cfg_attr(feature = "cargo-clippy", allow(clippy::useless_asref))]
|
||||
{
|
||||
result.push_str(if t.hour12().0 {
|
||||
am_pm_lowercase[1].as_ref()
|
||||
} else {
|
||||
am_pm_lowercase[0].as_ref()
|
||||
});
|
||||
let ampm = if t.hour12().0 { locale.am_pm[1] } else { locale.am_pm[0] };
|
||||
for char in ampm.chars() {
|
||||
result.extend(char.to_lowercase())
|
||||
}
|
||||
Ok(())
|
||||
}),
|
||||
UpperAmPm => time.map(|t| {
|
||||
result.push_str(if t.hour12().0 { am_pm[1] } else { am_pm[0] });
|
||||
result.push_str(if t.hour12().0 {
|
||||
locale.am_pm[1]
|
||||
} else {
|
||||
locale.am_pm[0]
|
||||
});
|
||||
Ok(())
|
||||
}),
|
||||
Nanosecond => time.map(|t| {
|
||||
@ -680,9 +704,9 @@ fn format_inner<'a>(
|
||||
write!(
|
||||
result,
|
||||
"{}, {:02} {} {:04} {:02}:{:02}:{:02} ",
|
||||
short_weekdays[d.weekday().num_days_from_sunday() as usize],
|
||||
locale.short_weekdays[d.weekday().num_days_from_sunday() as usize],
|
||||
d.day(),
|
||||
short_months[d.month0() as usize],
|
||||
locale.short_months[d.month0() as usize],
|
||||
d.year(),
|
||||
t.hour(),
|
||||
t.minute(),
|
||||
|
Loading…
x
Reference in New Issue
Block a user