This commit is contained in:
Alessandro Pellizzari 2018-12-05 09:12:17 +00:00
parent c70481d776
commit 64ea230a2b
4 changed files with 49 additions and 30 deletions

View file

@ -2,6 +2,7 @@
name = "chrono-locale" name = "chrono-locale"
version = "0.1.0" version = "0.1.0"
authors = ["Alessandro Pellizzari <alex@amiran.it>"] authors = ["Alessandro Pellizzari <alex@amiran.it>"]
# build = "build.rs"
[dependencies] [dependencies]
chrono = "0.4" chrono = "0.4"
@ -11,4 +12,5 @@ lazy_static = "1.2"
[build-dependencies] [build-dependencies]
serde = "1" serde = "1"
serde_json = "1" serde_json = "1"
serde_derive = "1"
walkdir = "2" walkdir = "2"

View file

@ -1,5 +1,5 @@
max_width = 150 max_width = 150
hard_tabs = true hard_tabs = true
normalize_comments = false #normalize_comments = false
match_block_trailing_comma = true #match_block_trailing_comma = true
closure_block_indent_threshold = 1 #closure_block_indent_threshold = 1

View file

@ -65,8 +65,7 @@ impl<'a, I: Iterator<Item = Item<'a>> + Clone> DelayedFormatL10n<I> {
} }
/// Makes a new `DelayedFormatL10n` value out of local date and time and UTC offset. /// Makes a new `DelayedFormatL10n` value out of local date and time and UTC offset.
pub fn new_with_offset(date: Option<NaiveDate>, time: Option<NaiveTime>, offset: &FixedOffset, items: I, locale: &str) -> DelayedFormatL10n<I> pub fn new_with_offset(date: Option<NaiveDate>, time: Option<NaiveTime>, offset: &FixedOffset, items: I, locale: &str) -> DelayedFormatL10n<I> {
{
let name_and_diff = (offset.to_string(), offset.to_owned()); let name_and_diff = (offset.to_string(), offset.to_owned());
DelayedFormatL10n { DelayedFormatL10n {
date, date,
@ -273,28 +272,36 @@ where
} }
fn short_month(month: usize, locale: &str) -> String { fn short_month(month: usize, locale: &str) -> String {
let res = locales::LOCALES.short_months.get(locale) let res = locales::LOCALES
.short_months
.get(locale)
.or_else(|| locales::LOCALES.short_months.get("C")) .or_else(|| locales::LOCALES.short_months.get("C"))
.unwrap(); .unwrap();
res.get(month).map(|v| v.to_string()).unwrap_or_else(|| format!("{}", month)) res.get(month).map(|v| v.to_string()).unwrap_or_else(|| format!("{}", month))
} }
fn long_month(month: usize, locale: &str) -> String { fn long_month(month: usize, locale: &str) -> String {
let res = locales::LOCALES.long_months.get(locale) let res = locales::LOCALES
.long_months
.get(locale)
.or_else(|| locales::LOCALES.long_months.get("C")) .or_else(|| locales::LOCALES.long_months.get("C"))
.unwrap(); .unwrap();
res.get(month).map(|v| v.to_string()).unwrap_or_else(|| format!("{}", month)) res.get(month).map(|v| v.to_string()).unwrap_or_else(|| format!("{}", month))
} }
fn short_weekday(day: usize, locale: &str) -> String { fn short_weekday(day: usize, locale: &str) -> String {
let res = locales::LOCALES.short_weekdays.get(locale) let res = locales::LOCALES
.short_weekdays
.get(locale)
.or_else(|| locales::LOCALES.short_weekdays.get("C")) .or_else(|| locales::LOCALES.short_weekdays.get("C"))
.unwrap(); .unwrap();
res.get(day).map(|v| v.to_string()).unwrap_or_else(|| format!("{}", day)) res.get(day).map(|v| v.to_string()).unwrap_or_else(|| format!("{}", day))
} }
fn long_weekday(day: usize, locale: &str) -> String { fn long_weekday(day: usize, locale: &str) -> String {
let res = locales::LOCALES.long_weekdays.get(locale) let res = locales::LOCALES
.long_weekdays
.get(locale)
.or_else(|| locales::LOCALES.long_weekdays.get("C")) .or_else(|| locales::LOCALES.long_weekdays.get("C"))
.unwrap(); .unwrap();
res.get(day).map(|v| v.to_string()).unwrap_or_else(|| format!("{}", day)) res.get(day).map(|v| v.to_string()).unwrap_or_else(|| format!("{}", day))
@ -355,9 +362,9 @@ mod tests {
assert_eq!(dt.formatl("%.6f", "en").to_string(), ".026490"); assert_eq!(dt.formatl("%.6f", "en").to_string(), ".026490");
assert_eq!(dt.formatl("%.9f", "en").to_string(), ".026490708"); assert_eq!(dt.formatl("%.9f", "en").to_string(), ".026490708");
// The following formats are not exposed by chrono and cannot be formatted // The following formats are not exposed by chrono and cannot be formatted
// assert_eq!(dt.formatl("%3f", "en").to_string(), "026"); // assert_eq!(dt.formatl("%3f", "en").to_string(), "026");
// assert_eq!(dt.formatl("%6f", "en").to_string(), "026490"); // assert_eq!(dt.formatl("%6f", "en").to_string(), "026490");
// assert_eq!(dt.formatl("%9f", "en").to_string(), "026490708"); // assert_eq!(dt.formatl("%9f", "en").to_string(), "026490708");
assert_eq!(dt.formatl("%R", "en").to_string(), "00:34"); assert_eq!(dt.formatl("%R", "en").to_string(), "00:34");
assert_eq!(dt.formatl("%T", "en").to_string(), "00:34:60"); assert_eq!(dt.formatl("%T", "en").to_string(), "00:34:60");
assert_eq!(dt.formatl("%X", "en").to_string(), "00:34:60"); assert_eq!(dt.formatl("%X", "en").to_string(), "00:34:60");

View file

@ -11,33 +11,43 @@ pub struct Locales {
lazy_static! { lazy_static! {
pub static ref LOCALES: Locales = { pub static ref LOCALES: Locales = {
let mut res = Locales{ let mut res = Locales {
short_months: HashMap::new(), short_months: HashMap::new(),
long_months: HashMap::new(), long_months: HashMap::new(),
short_weekdays: HashMap::new(), short_weekdays: HashMap::new(),
long_weekdays: HashMap::new(), long_weekdays: HashMap::new(),
}; };
res.short_months.insert("C".into(), vec!["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]); res.short_months.insert(
"C".into(),
vec!["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
);
res.long_months.insert("C".into(), vec![ res.long_months.insert(
"January", "C".into(),
"February", vec![
"March", "January",
"April", "February",
"May", "March",
"June", "April",
"July", "May",
"August", "June",
"September", "July",
"October", "August",
"November", "September",
"December", "October",
]); "November",
"December",
],
);
res.short_weekdays.insert("C".into(), vec!["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]); res.short_weekdays
.insert("C".into(), vec!["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]);
res.long_weekdays.insert("C".into(), vec!["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]); res.long_weekdays.insert(
"C".into(),
vec!["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
);
res res
}; };