Merge pull request #247 from ogham/exa/date_output

Fix month name widths once and for all #244
This commit is contained in:
Benjamin Sago 2017-08-06 20:23:09 +01:00 committed by GitHub
commit 999c9d56f5

View File

@ -1,6 +1,7 @@
use datetime::{LocalDateTime, TimeZone, DatePiece, TimePiece}; use datetime::{LocalDateTime, TimeZone, DatePiece, TimePiece};
use datetime::fmt::DateFormat; use datetime::fmt::DateFormat;
use locale; use locale;
use std::cmp;
use fs::fields::Time; use fs::fields::Time;
@ -60,17 +61,23 @@ impl DefaultFormat {
let current_year = LocalDateTime::now().year(); let current_year = LocalDateTime::now().year();
// Some locales use a three-character wide month name (Jan to Dec); // Some locales use a three-character wide month name (Jan to Dec);
// others vary between three and four (1月 to 12月). We assume that // others vary between three to four (1月 to 12月, juil.). We check each month width
// December is the month with the maximum width, and use the width of // to detect the longest and set the output format accordingly.
// that to determine how to pad the other months. let mut maximum_month_width = 0;
let december_width = UnicodeWidthStr::width(&*locale.short_month_name(11)); for i in 0..11 {
let date_and_time = match december_width { let current_month_width = UnicodeWidthStr::width(&*locale.short_month_name(i));
4 => DateFormat::parse("{2>:D} {4>:M} {2>:h}:{02>:m}").unwrap(), maximum_month_width = cmp::max(maximum_month_width, current_month_width);
}
let date_and_time = match maximum_month_width {
4 => DateFormat::parse("{2>:D} {4<:M} {2>:h}:{02>:m}").unwrap(),
5 => DateFormat::parse("{2>:D} {5<:M} {2>:h}:{02>:m}").unwrap(),
_ => DateFormat::parse("{2>:D} {:M} {2>:h}:{02>:m}").unwrap(), _ => DateFormat::parse("{2>:D} {:M} {2>:h}:{02>:m}").unwrap(),
}; };
let date_and_year = match december_width { let date_and_year = match maximum_month_width {
4 => DateFormat::parse("{2>:D} {4>:M} {5>:Y}").unwrap(), 4 => DateFormat::parse("{2>:D} {4<:M} {5>:Y}").unwrap(),
5 => DateFormat::parse("{2>:D} {5<:M} {5>:Y}").unwrap(),
_ => DateFormat::parse("{2>:D} {:M} {5>:Y}").unwrap() _ => DateFormat::parse("{2>:D} {:M} {5>:Y}").unwrap()
}; };