Skip to content

Commit

Permalink
feat: add format::locale module
Browse files Browse the repository at this point in the history
  • Loading branch information
ar3s3ru committed May 8, 2020
1 parent 8060089 commit b54296e
Show file tree
Hide file tree
Showing 2 changed files with 226 additions and 15 deletions.
203 changes: 203 additions & 0 deletions src/format/locale.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/// Locale is a locale
#[derive(Debug, Clone, Copy, Default)]
pub struct Locale {
days: Days,
months: Months,
}

impl Locale {
/// Returns days
pub fn days(&self) -> Days {
self.days
}

/// Returns months
pub fn months(&self) -> Months {
self.months
}
}

#[derive(Debug, Clone, Copy)]
pub struct Days {
pub monday: Elem,
pub tuesday: Elem,
pub wednesday: Elem,
pub thursday: Elem,
pub friday: Elem,
pub saturday: Elem,
pub sunday: Elem,
}

impl Days {
pub fn into_long_array(self) -> [&'static str; 7] {
[
self.monday.long,
self.tuesday.long,
self.wednesday.long,
self.thursday.long,
self.friday.long,
self.saturday.long,
self.sunday.long,
]
}

pub fn into_short_array(self) -> [&'static str; 7] {
[
self.monday.short,
self.tuesday.short,
self.wednesday.short,
self.thursday.short,
self.friday.short,
self.saturday.short,
self.sunday.short,
]
}
}

impl Default for Days {
fn default() -> Self {
Days {
monday: Elem {
long: "Monday",
short: "Mon",
},
tuesday: Elem {
long: "Tuesday",
short: "Tue",
},
wednesday: Elem {
long: "Wednesday",
short: "Wed",
},
thursday: Elem {
long: "Thursday",
short: "Thu",
},
friday: Elem {
long: "Friday",
short: "Fri",
},
saturday: Elem {
long: "Saturday",
short: "Sat",
},
sunday: Elem {
long: "Sunday",
short: "Sun",
},
}
}
}

#[derive(Debug, Clone, Copy)]
pub struct Months {
pub january: Elem,
pub february: Elem,
pub march: Elem,
pub april: Elem,
pub may: Elem,
pub june: Elem,
pub july: Elem,
pub august: Elem,
pub september: Elem,
pub october: Elem,
pub november: Elem,
pub december: Elem,
}

impl Months {
pub fn into_long_array(self) -> [&'static str; 12] {
[
self.january.long,
self.february.long,
self.march.long,
self.april.long,
self.may.long,
self.june.long,
self.july.long,
self.august.long,
self.september.long,
self.october.long,
self.november.long,
self.december.long,
]
}

pub fn into_short_array(self) -> [&'static str; 12] {
[
self.january.short,
self.february.short,
self.march.short,
self.april.short,
self.may.short,
self.june.short,
self.july.short,
self.august.short,
self.september.short,
self.october.short,
self.november.short,
self.december.short,
]
}
}

impl Default for Months {
fn default() -> Self {
Months {
january: Elem {
long: "January",
short: "Jan",
},
february: Elem {
long: "February",
short: "Feb",
},
march: Elem {
long: "March",
short: "Mar",
},
april: Elem {
long: "April",
short: "Apr",
},
may: Elem {
long: "May",
short: "May",
},
june: Elem {
long: "June",
short: "Jun",
},
july: Elem {
long: "July",
short: "Jul",
},
august: Elem {
long: "August",
short: "Aug",
},
september: Elem {
long: "September",
short: "Sep",
},
october: Elem {
long: "October",
short: "Oct",
},
november: Elem {
long: "November",
short: "Nov",
},
december: Elem {
long: "December",
short: "Dec",
},
}
}
}

#[derive(Debug, Clone, Copy)]
pub struct Elem {
long: &'static str,
short: &'static str,
}
38 changes: 23 additions & 15 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use naive::{NaiveDate, NaiveTime};
pub use self::strftime::StrftimeItems;
pub use self::parsed::Parsed;
pub use self::parse::parse;
pub use self::locale::Locale;

/// An uninhabited type used for `InternalNumeric` and `InternalFixed` below.
#[derive(Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -361,15 +362,15 @@ pub fn format_item<'a>(
) -> fmt::Result
{
// full and abbreviated month and weekday names
static SHORT_MONTHS: [&'static str; 12] =
["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
static LONG_MONTHS: [&'static str; 12] =
["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
static SHORT_WEEKDAYS: [&'static str; 7] =
["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
static LONG_WEEKDAYS: [&'static str; 7] =
["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
// static SHORT_MONTHS: [&'static str; 12] =
// ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
// static LONG_MONTHS: [&'static str; 12] =
// ["January", "February", "March", "April", "May", "June",
// "July", "August", "September", "October", "November", "December"];
// static SHORT_WEEKDAYS: [&'static str; 7] =
// ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
// static LONG_WEEKDAYS: [&'static str; 7] =
// ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];

use core::fmt::Write;
let mut result = String::new();
Expand Down Expand Up @@ -470,28 +471,34 @@ pub fn format_item<'a>(
}
}

let locale = Locale::default();
let short_months = locale.months().into_short_array();
let long_months = locale.months().into_long_array();
let short_weekdays = locale.days().into_short_array();
let long_weekdays = locale.days().into_long_array();

let ret = match spec {
&ShortMonthName =>
date.map(|d| {
result.push_str(SHORT_MONTHS[d.month0() as usize]);
result.push_str(short_months[d.month0() as usize]);
Ok(())
}),
&LongMonthName =>
date.map(|d| {
result.push_str(LONG_MONTHS[d.month0() as usize]);
result.push_str(long_months[d.month0() as usize]);
Ok(())
}),
&ShortWeekdayName =>
date.map(|d| {
result.push_str(
SHORT_WEEKDAYS[d.weekday().num_days_from_monday() as usize]
short_weekdays[d.weekday().num_days_from_monday() as usize]
);
Ok(())
}),
&LongWeekdayName =>
date.map(|d| {
result.push_str(
LONG_WEEKDAYS[d.weekday().num_days_from_monday() as usize]
long_weekdays[d.weekday().num_days_from_monday() as usize]
);
Ok(())
}),
Expand Down Expand Up @@ -569,8 +576,8 @@ pub fn format_item<'a>(
write!(
result,
"{}, {:02} {} {:04} {:02}:{:02}:{:02} ",
SHORT_WEEKDAYS[d.weekday().num_days_from_monday() as usize],
d.day(), SHORT_MONTHS[d.month0() as usize], d.year(),
short_weekdays[d.weekday().num_days_from_monday() as usize],
d.day(), short_months[d.month0() as usize], d.year(),
t.hour(), t.minute(), sec
)?;
Some(write_local_minus_utc(&mut result, off, false, false))
Expand Down Expand Up @@ -627,6 +634,7 @@ mod parsed;
// due to the size of parsing routines, they are in separate modules.
mod scan;
mod parse;
mod locale;

pub mod strftime;

Expand Down

0 comments on commit b54296e

Please sign in to comment.