|
1 | 1 | use datetime::{LocalDateTime, TimeZone, DatePiece, TimePiece};
|
2 | 2 | use datetime::fmt::DateFormat;
|
3 | 3 | use locale;
|
| 4 | +use std::cmp; |
4 | 5 |
|
5 | 6 | use fs::fields::Time;
|
6 | 7 |
|
@@ -60,17 +61,23 @@ impl DefaultFormat {
|
60 | 61 | let current_year = LocalDateTime::now().year();
|
61 | 62 |
|
62 | 63 | // 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(), |
69 | 75 | _ => DateFormat::parse("{2>:D} {:M} {2>:h}:{02>:m}").unwrap(),
|
70 | 76 | };
|
71 | 77 |
|
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(), |
74 | 81 | _ => DateFormat::parse("{2>:D} {:M} {5>:Y}").unwrap()
|
75 | 82 | };
|
76 | 83 |
|
|
0 commit comments