From cb2e94a796ac48d27ffeba2dbae4a16839419e81 Mon Sep 17 00:00:00 2001 From: Alfred Sawaya Date: Sun, 6 Aug 2017 03:59:46 +0200 Subject: [PATCH] Fix month name widths once and for all #244 To render the date, Exa now find out the month with the longest name among all months, and use the width of that. --- src/output/time.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/output/time.rs b/src/output/time.rs index 5563020..1d0b9ed 100644 --- a/src/output/time.rs +++ b/src/output/time.rs @@ -1,6 +1,7 @@ use datetime::{LocalDateTime, TimeZone, DatePiece, TimePiece}; use datetime::fmt::DateFormat; use locale; +use std::cmp; use fs::fields::Time; @@ -60,17 +61,23 @@ impl DefaultFormat { 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(), + // others vary between three to four (1月 to 12月, juil.). We check each month width + // to detect the longest and set the output format accordingly. + let mut maximum_month_width = 0; + for i in 0..11 { + let current_month_width = UnicodeWidthStr::width(&*locale.short_month_name(i)); + 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(), }; - let date_and_year = match december_width { - 4 => DateFormat::parse("{2>:D} {4>:M} {5>:Y}").unwrap(), + let date_and_year = match maximum_month_width { + 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() };