Merge pull request #837 from ariasuni/fix-month-name-not-translated

Fix month name not following locale when date is in current year
This commit is contained in:
Benjamin Sago 2021-04-09 23:11:28 +01:00 committed by GitHub
commit e86f2b938b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 54 deletions

View File

@ -2,7 +2,7 @@
use std::time::{SystemTime, UNIX_EPOCH};
use datetime::{LocalDateTime, TimeZone, DatePiece, TimePiece, Month};
use datetime::{LocalDateTime, TimeZone, DatePiece, TimePiece};
use datetime::fmt::DateFormat;
use lazy_static::lazy_static;
@ -75,40 +75,25 @@ impl TimeFormat {
#[allow(trivial_numeric_casts)]
fn default_local(time: SystemTime) -> String {
let date = LocalDateTime::at(systemtime_epoch(time));
if date.year() == *CURRENT_YEAR {
format!("{:2} {} {:02}:{:02}",
date.day(), month_to_abbrev(date.month()),
date.hour(), date.minute())
}
else {
let date_format = match *MAXIMUM_MONTH_WIDTH {
4 => &*FOUR_WIDE_DATE_TIME,
5 => &*FIVE_WIDE_DATE_TIME,
_ => &*OTHER_WIDE_DATE_TIME,
};
date_format.format(&date, &*LOCALE)
}
let date_format = get_dateformat(&date);
date_format.format(&date, &*LOCALE)
}
#[allow(trivial_numeric_casts)]
fn default_zoned(time: SystemTime, zone: &TimeZone) -> String {
let date = zone.to_zoned(LocalDateTime::at(systemtime_epoch(time)));
let date_format = get_dateformat(&date);
date_format.format(&date, &*LOCALE)
}
if date.year() == *CURRENT_YEAR {
format!("{:2} {} {:02}:{:02}",
date.day(), month_to_abbrev(date.month()),
date.hour(), date.minute())
}
else {
let date_format = match *MAXIMUM_MONTH_WIDTH {
4 => &*FOUR_WIDE_DATE_YEAR,
5 => &*FIVE_WIDE_DATE_YEAR,
_ => &*OTHER_WIDE_DATE_YEAR,
};
date_format.format(&date, &*LOCALE)
fn get_dateformat(date: &LocalDateTime) -> &'static DateFormat<'static> {
match (is_recent(&date), *MAXIMUM_MONTH_WIDTH) {
(true, 4) => &FOUR_WIDE_DATE_TIME,
(true, 5) => &FIVE_WIDE_DATE_TIME,
(true, _) => &OTHER_WIDE_DATE_TIME,
(false, 4) => &FOUR_WIDE_DATE_YEAR,
(false, 5) => &FIVE_WIDE_DATE_YEAR,
(false, _) => &OTHER_WIDE_DATE_YEAR,
}
}
@ -153,7 +138,7 @@ fn full_zoned(time: SystemTime, zone: &TimeZone) -> String {
fn iso_local(time: SystemTime) -> String {
let date = LocalDateTime::at(systemtime_epoch(time));
if is_recent(date) {
if is_recent(&date) {
format!("{:02}-{:02} {:02}:{:02}",
date.month() as usize, date.day(),
date.hour(), date.minute())
@ -168,7 +153,7 @@ fn iso_local(time: SystemTime) -> String {
fn iso_zoned(time: SystemTime, zone: &TimeZone) -> String {
let date = zone.to_zoned(LocalDateTime::at(systemtime_epoch(time)));
if is_recent(date) {
if is_recent(&date) {
format!("{:02}-{:02} {:02}:{:02}",
date.month() as usize, date.day(),
date.hour(), date.minute())
@ -206,27 +191,10 @@ fn systemtime_nanos(time: SystemTime) -> u32 {
})
}
fn is_recent(date: LocalDateTime) -> bool {
fn is_recent(date: &LocalDateTime) -> bool {
date.year() == *CURRENT_YEAR
}
fn month_to_abbrev(month: Month) -> &'static str {
match month {
Month::January => "Jan",
Month::February => "Feb",
Month::March => "Mar",
Month::April => "Apr",
Month::May => "May",
Month::June => "Jun",
Month::July => "Jul",
Month::August => "Aug",
Month::September => "Sep",
Month::October => "Oct",
Month::November => "Nov",
Month::December => "Dec",
}
}
lazy_static! {
@ -250,15 +218,15 @@ lazy_static! {
};
static ref FOUR_WIDE_DATE_TIME: DateFormat<'static> = DateFormat::parse(
"{2>:D} {4<:M} {2>:h}:{02>:m}"
"{2>:D} {4<:M} {02>:h}:{02>:m}"
).unwrap();
static ref FIVE_WIDE_DATE_TIME: DateFormat<'static> = DateFormat::parse(
"{2>:D} {5<:M} {2>:h}:{02>:m}"
"{2>:D} {5<:M} {02>:h}:{02>:m}"
).unwrap();
static ref OTHER_WIDE_DATE_TIME: DateFormat<'static> = DateFormat::parse(
"{2>:D} {:M} {2>:h}:{02>:m}"
"{2>:D} {:M} {02>:h}:{02>:m}"
).unwrap();
static ref FOUR_WIDE_DATE_YEAR: DateFormat<'static> = DateFormat::parse(

View File

@ -68,7 +68,7 @@ tags = [ 'long', 'time-style' ]
[[cmd]]
name = "exa -l using a locale with 4-character-long month abbreviations (ja_JP) sizes the date column correctly"
shell = "exa -l /testcases/dates"
environment = { LC_ALL = "ja_JP.UTF-8", LANG = "ja_JP.UTF-8" }
environment = { LC_TIME = "ja_JP.UTF-8", LANG = "ja_JP.UTF-8" }
stdout = { file = "outputs/dates_long_localejp.ansitxt" }
stderr = { empty = true }
status = 0
@ -77,8 +77,17 @@ tags = [ 'long', 'locales' ]
[[cmd]]
name = "exa -l using a locale with 5-character-long month abbreviations (fr_FR) sizes the date column correctly"
shell = "exa -l /testcases/dates"
environment = { LC_ALL = "fr_FR.UTF-8", LANG = "fr_FR.UTF-8" }
environment = { LC_TIME = "fr_FR.UTF-8", LANG = "fr_FR.UTF-8" }
stdout = { file = "outputs/dates_long_localefr.ansitxt" }
stderr = { empty = true }
status = 0
tags = [ 'long', 'locales' ]
[[cmd]]
name = "exa -l using a locale (fr_FR) display dates of the current year with localized month name"
shell = "exa -l /testcases/files"
environment = { LC_TIME = "fr_FR.UTF-8", LANG = "fr_FR.UTF-8" }
stdout = { file = "outputs/dates_long_currentyear_localefr.ansitxt" }
stderr = { empty = true }
status = 0
tags = [ 'long', 'locales' ]

View File

@ -0,0 +1,39 @@
.rw-r--r-- 1 cassowary  1 janv. 12:34 1_bytes
.rw-r--r-- 1,0k cassowary  1 janv. 12:34 1_KiB
.rw-r--r-- 1,0M cassowary  1 janv. 12:34 1_MiB
.rw-r--r-- 2 cassowary  1 janv. 12:34 2_bytes
.rw-r--r-- 2,0k cassowary  1 janv. 12:34 2_KiB
.rw-r--r-- 2,1M cassowary  1 janv. 12:34 2_MiB
.rw-r--r-- 3 cassowary  1 janv. 12:34 3_bytes
.rw-r--r-- 3,1k cassowary  1 janv. 12:34 3_KiB
.rw-r--r-- 3,1M cassowary  1 janv. 12:34 3_MiB
.rw-r--r-- 4 cassowary  1 janv. 12:34 4_bytes
.rw-r--r-- 4,1k cassowary  1 janv. 12:34 4_KiB
.rw-r--r-- 4,2M cassowary  1 janv. 12:34 4_MiB
.rw-r--r-- 5 cassowary  1 janv. 12:34 5_bytes
.rw-r--r-- 5,1k cassowary  1 janv. 12:34 5_KiB
.rw-r--r-- 5,2M cassowary  1 janv. 12:34 5_MiB
.rw-r--r-- 6 cassowary  1 janv. 12:34 6_bytes
.rw-r--r-- 6,1k cassowary  1 janv. 12:34 6_KiB
.rw-r--r-- 6,3M cassowary  1 janv. 12:34 6_MiB
.rw-r--r-- 7 cassowary  1 janv. 12:34 7_bytes
.rw-r--r-- 7,2k cassowary  1 janv. 12:34 7_KiB
.rw-r--r-- 7,3M cassowary  1 janv. 12:34 7_MiB
.rw-r--r-- 8 cassowary  1 janv. 12:34 8_bytes
.rw-r--r-- 8,2k cassowary  1 janv. 12:34 8_KiB
.rw-r--r-- 8,4M cassowary  1 janv. 12:34 8_MiB
.rw-r--r-- 9 cassowary  1 janv. 12:34 9_bytes
.rw-r--r-- 9,2k cassowary  1 janv. 12:34 9_KiB
.rw-r--r-- 9,4M cassowary  1 janv. 12:34 9_MiB
.rw-r--r-- 10 cassowary  1 janv. 12:34 10_bytes
.rw-r--r-- 10k cassowary  1 janv. 12:34 10_KiB
.rw-r--r-- 10M cassowary  1 janv. 12:34 10_MiB
.rw-r--r-- 11 cassowary  1 janv. 12:34 11_bytes
.rw-r--r-- 11k cassowary  1 janv. 12:34 11_KiB
.rw-r--r-- 12M cassowary  1 janv. 12:34 11_MiB
.rw-r--r-- 12 cassowary  1 janv. 12:34 12_bytes
.rw-r--r-- 12k cassowary  1 janv. 12:34 12_KiB
.rw-r--r-- 13M cassowary  1 janv. 12:34 12_MiB
.rw-r--r-- 13 cassowary  1 janv. 12:34 13_bytes
.rw-r--r-- 13k cassowary  1 janv. 12:34 13_KiB
.rw-r--r-- 14M cassowary  1 janv. 12:34 13_MiB