Skip to content

Commit 999c9d5

Browse files
authored
Merge pull request #247 from ogham/exa/date_output
Fix month name widths once and for all #244
2 parents 68210c5 + cb2e94a commit 999c9d5

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/output/time.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use datetime::{LocalDateTime, TimeZone, DatePiece, TimePiece};
22
use datetime::fmt::DateFormat;
33
use locale;
4+
use std::cmp;
45

56
use fs::fields::Time;
67

@@ -60,17 +61,23 @@ impl DefaultFormat {
6061
let current_year = LocalDateTime::now().year();
6162

6263
// Some locales use a three-character wide month name (Jan to Dec);
63-
// others vary between three and four (1月 to 12月). We assume that
64-
// December is the month with the maximum width, and use the width of
65-
// that to determine how to pad the other months.
66-
let december_width = UnicodeWidthStr::width(&*locale.short_month_name(11));
67-
let date_and_time = match december_width {
68-
4 => DateFormat::parse("{2>:D} {4>:M} {2>:h}:{02>:m}").unwrap(),
64+
// others vary between three to four (1月 to 12月, juil.). We check each month width
65+
// to detect the longest and set the output format accordingly.
66+
let mut maximum_month_width = 0;
67+
for i in 0..11 {
68+
let current_month_width = UnicodeWidthStr::width(&*locale.short_month_name(i));
69+
maximum_month_width = cmp::max(maximum_month_width, current_month_width);
70+
}
71+
72+
let date_and_time = match maximum_month_width {
73+
4 => DateFormat::parse("{2>:D} {4<:M} {2>:h}:{02>:m}").unwrap(),
74+
5 => DateFormat::parse("{2>:D} {5<:M} {2>:h}:{02>:m}").unwrap(),
6975
_ => DateFormat::parse("{2>:D} {:M} {2>:h}:{02>:m}").unwrap(),
7076
};
7177

72-
let date_and_year = match december_width {
73-
4 => DateFormat::parse("{2>:D} {4>:M} {5>:Y}").unwrap(),
78+
let date_and_year = match maximum_month_width {
79+
4 => DateFormat::parse("{2>:D} {4<:M} {5>:Y}").unwrap(),
80+
5 => DateFormat::parse("{2>:D} {5<:M} {5>:Y}").unwrap(),
7481
_ => DateFormat::parse("{2>:D} {:M} {5>:Y}").unwrap()
7582
};
7683

0 commit comments

Comments
 (0)