Skip to content

Commit 3a8005c

Browse files
committed
Fix month name not following locale when date is in current year
1 parent 2aaead1 commit 3a8005c

File tree

1 file changed

+20
-52
lines changed

1 file changed

+20
-52
lines changed

src/output/time.rs

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::time::{SystemTime, UNIX_EPOCH};
44

5-
use datetime::{LocalDateTime, TimeZone, DatePiece, TimePiece, Month};
5+
use datetime::{LocalDateTime, TimeZone, DatePiece, TimePiece};
66
use datetime::fmt::DateFormat;
77

88
use lazy_static::lazy_static;
@@ -75,40 +75,25 @@ impl TimeFormat {
7575
#[allow(trivial_numeric_casts)]
7676
fn default_local(time: SystemTime) -> String {
7777
let date = LocalDateTime::at(systemtime_epoch(time));
78-
79-
if date.year() == *CURRENT_YEAR {
80-
format!("{:2} {} {:02}:{:02}",
81-
date.day(), month_to_abbrev(date.month()),
82-
date.hour(), date.minute())
83-
}
84-
else {
85-
let date_format = match *MAXIMUM_MONTH_WIDTH {
86-
4 => &*FOUR_WIDE_DATE_TIME,
87-
5 => &*FIVE_WIDE_DATE_TIME,
88-
_ => &*OTHER_WIDE_DATE_TIME,
89-
};
90-
91-
date_format.format(&date, &*LOCALE)
92-
}
78+
let date_format = get_dateformat(&date);
79+
date_format.format(&date, &*LOCALE)
9380
}
9481

9582
#[allow(trivial_numeric_casts)]
9683
fn default_zoned(time: SystemTime, zone: &TimeZone) -> String {
9784
let date = zone.to_zoned(LocalDateTime::at(systemtime_epoch(time)));
85+
let date_format = get_dateformat(&date);
86+
date_format.format(&date, &*LOCALE)
87+
}
9888

99-
if date.year() == *CURRENT_YEAR {
100-
format!("{:2} {} {:02}:{:02}",
101-
date.day(), month_to_abbrev(date.month()),
102-
date.hour(), date.minute())
103-
}
104-
else {
105-
let date_format = match *MAXIMUM_MONTH_WIDTH {
106-
4 => &*FOUR_WIDE_DATE_YEAR,
107-
5 => &*FIVE_WIDE_DATE_YEAR,
108-
_ => &*OTHER_WIDE_DATE_YEAR,
109-
};
110-
111-
date_format.format(&date, &*LOCALE)
89+
fn get_dateformat(date: &LocalDateTime) -> &'static DateFormat<'static> {
90+
match (is_recent(&date), *MAXIMUM_MONTH_WIDTH) {
91+
(true, 4) => &FOUR_WIDE_DATE_TIME,
92+
(true, 5) => &FIVE_WIDE_DATE_TIME,
93+
(true, _) => &OTHER_WIDE_DATE_TIME,
94+
(false, 4) => &FOUR_WIDE_DATE_YEAR,
95+
(false, 5) => &FIVE_WIDE_DATE_YEAR,
96+
(false, _) => &OTHER_WIDE_DATE_YEAR,
11297
}
11398
}
11499

@@ -153,7 +138,7 @@ fn full_zoned(time: SystemTime, zone: &TimeZone) -> String {
153138
fn iso_local(time: SystemTime) -> String {
154139
let date = LocalDateTime::at(systemtime_epoch(time));
155140

156-
if is_recent(date) {
141+
if is_recent(&date) {
157142
format!("{:02}-{:02} {:02}:{:02}",
158143
date.month() as usize, date.day(),
159144
date.hour(), date.minute())
@@ -168,7 +153,7 @@ fn iso_local(time: SystemTime) -> String {
168153
fn iso_zoned(time: SystemTime, zone: &TimeZone) -> String {
169154
let date = zone.to_zoned(LocalDateTime::at(systemtime_epoch(time)));
170155

171-
if is_recent(date) {
156+
if is_recent(&date) {
172157
format!("{:02}-{:02} {:02}:{:02}",
173158
date.month() as usize, date.day(),
174159
date.hour(), date.minute())
@@ -206,27 +191,10 @@ fn systemtime_nanos(time: SystemTime) -> u32 {
206191
})
207192
}
208193

209-
fn is_recent(date: LocalDateTime) -> bool {
194+
fn is_recent(date: &LocalDateTime) -> bool {
210195
date.year() == *CURRENT_YEAR
211196
}
212197

213-
fn month_to_abbrev(month: Month) -> &'static str {
214-
match month {
215-
Month::January => "Jan",
216-
Month::February => "Feb",
217-
Month::March => "Mar",
218-
Month::April => "Apr",
219-
Month::May => "May",
220-
Month::June => "Jun",
221-
Month::July => "Jul",
222-
Month::August => "Aug",
223-
Month::September => "Sep",
224-
Month::October => "Oct",
225-
Month::November => "Nov",
226-
Month::December => "Dec",
227-
}
228-
}
229-
230198

231199
lazy_static! {
232200

@@ -250,15 +218,15 @@ lazy_static! {
250218
};
251219

252220
static ref FOUR_WIDE_DATE_TIME: DateFormat<'static> = DateFormat::parse(
253-
"{2>:D} {4<:M} {2>:h}:{02>:m}"
221+
"{2>:D} {4<:M} {02>:h}:{02>:m}"
254222
).unwrap();
255223

256224
static ref FIVE_WIDE_DATE_TIME: DateFormat<'static> = DateFormat::parse(
257-
"{2>:D} {5<:M} {2>:h}:{02>:m}"
225+
"{2>:D} {5<:M} {02>:h}:{02>:m}"
258226
).unwrap();
259227

260228
static ref OTHER_WIDE_DATE_TIME: DateFormat<'static> = DateFormat::parse(
261-
"{2>:D} {:M} {2>:h}:{02>:m}"
229+
"{2>:D} {:M} {02>:h}:{02>:m}"
262230
).unwrap();
263231

264232
static ref FOUR_WIDE_DATE_YEAR: DateFormat<'static> = DateFormat::parse(

0 commit comments

Comments
 (0)