|
|
@@ -5,8 +5,27 @@ use locale;
|
|
|
use fs::fields::Time;
|
|
|
|
|
|
|
|
|
+pub enum TimeFormat {
|
|
|
+ DefaultFormat(DefaultFormat),
|
|
|
+}
|
|
|
+
|
|
|
+impl TimeFormat {
|
|
|
+ pub fn format_local(&self, time: Time) -> String {
|
|
|
+ match *self {
|
|
|
+ TimeFormat::DefaultFormat(ref fmt) => fmt.format_local(time),
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ pub fn format_zoned(&self, time: Time, zone: &TimeZone) -> String {
|
|
|
+ match *self {
|
|
|
+ TimeFormat::DefaultFormat(ref fmt) => fmt.format_zoned(time, zone),
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
#[derive(Debug, Clone)]
|
|
|
-pub struct TimeFormat {
|
|
|
+pub struct DefaultFormat {
|
|
|
|
|
|
/// The year of the current time. This gets used to determine which date
|
|
|
/// format to use.
|
|
|
@@ -22,13 +41,42 @@ pub struct TimeFormat {
|
|
|
pub date_and_year: DateFormat<'static>,
|
|
|
}
|
|
|
|
|
|
-impl TimeFormat {
|
|
|
+impl DefaultFormat {
|
|
|
+ pub fn new() -> DefaultFormat {
|
|
|
+ use unicode_width::UnicodeWidthStr;
|
|
|
+
|
|
|
+ let locale = locale::Time::load_user_locale()
|
|
|
+ .unwrap_or_else(|_| locale::Time::english());
|
|
|
+
|
|
|
+ let current_year = LocalDateTime::now().year();
|
|
|
+
|
|
|
+ // Some locales use a three-character wide month name (Jan to Dec);
|
|
|
+ // others vary between three and four (1月 to 12月). We assume that
|
|
|
+ // December is the month with the maximum width, and use the width of
|
|
|
+ // that to determine how to pad the other months.
|
|
|
+ let december_width = UnicodeWidthStr::width(&*locale.short_month_name(11));
|
|
|
+ let date_and_time = match december_width {
|
|
|
+ 4 => DateFormat::parse("{2>:D} {4>:M} {2>:h}:{02>:m}").unwrap(),
|
|
|
+ _ => DateFormat::parse("{2>:D} {:M} {2>:h}:{02>:m}").unwrap(),
|
|
|
+ };
|
|
|
+
|
|
|
+ let date_and_year = match december_width {
|
|
|
+ 4 => DateFormat::parse("{2>:D} {4>:M} {5>:Y}").unwrap(),
|
|
|
+ _ => DateFormat::parse("{2>:D} {:M} {5>:Y}").unwrap()
|
|
|
+ };
|
|
|
+
|
|
|
+ DefaultFormat { current_year, locale, date_and_time, date_and_year }
|
|
|
+ }
|
|
|
+
|
|
|
fn is_recent(&self, date: LocalDateTime) -> bool {
|
|
|
date.year() == self.current_year
|
|
|
}
|
|
|
+}
|
|
|
+
|
|
|
+impl DefaultFormat {
|
|
|
|
|
|
#[allow(trivial_numeric_casts)]
|
|
|
- pub fn format_local(&self, time: Time) -> String {
|
|
|
+ fn format_local(&self, time: Time) -> String {
|
|
|
let date = LocalDateTime::at(time.seconds as i64);
|
|
|
|
|
|
if self.is_recent(date) {
|
|
|
@@ -40,7 +88,7 @@ impl TimeFormat {
|
|
|
}
|
|
|
|
|
|
#[allow(trivial_numeric_casts)]
|
|
|
- pub fn format_zoned(&self, time: Time, zone: &TimeZone) -> String {
|
|
|
+ fn format_zoned(&self, time: Time, zone: &TimeZone) -> String {
|
|
|
let date = zone.to_zoned(LocalDateTime::at(time.seconds as i64));
|
|
|
|
|
|
if self.is_recent(date) {
|
|
|
@@ -50,30 +98,4 @@ impl TimeFormat {
|
|
|
self.date_and_year.format(&date, &self.locale)
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- pub fn deduce() -> TimeFormat {
|
|
|
- use unicode_width::UnicodeWidthStr;
|
|
|
-
|
|
|
- let locale = locale::Time::load_user_locale()
|
|
|
- .unwrap_or_else(|_| locale::Time::english());
|
|
|
-
|
|
|
- let current_year = LocalDateTime::now().year();
|
|
|
-
|
|
|
- // Some locales use a three-character wide month name (Jan to Dec);
|
|
|
- // others vary between three and four (1月 to 12月). We assume that
|
|
|
- // December is the month with the maximum width, and use the width of
|
|
|
- // that to determine how to pad the other months.
|
|
|
- let december_width = UnicodeWidthStr::width(&*locale.short_month_name(11));
|
|
|
- let date_and_time = match december_width {
|
|
|
- 4 => DateFormat::parse("{2>:D} {4>:M} {2>:h}:{02>:m}").unwrap(),
|
|
|
- _ => DateFormat::parse("{2>:D} {:M} {2>:h}:{02>:m}").unwrap(),
|
|
|
- };
|
|
|
-
|
|
|
- let date_and_year = match december_width {
|
|
|
- 4 => DateFormat::parse("{2>:D} {4>:M} {5>:Y}").unwrap(),
|
|
|
- _ => DateFormat::parse("{2>:D} {:M} {5>:Y}").unwrap()
|
|
|
- };
|
|
|
-
|
|
|
- TimeFormat { current_year, locale, date_and_time, date_and_year }
|
|
|
- }
|
|
|
}
|