1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

date: display %Z alphabetic time zone abbreviation

Improve the display of dates formatted with the `%Z` specifier so that
the timezone abbreviation is displayed, not just its numeric offset.

Fixes #3756

Co-authored-by: Jeffrey Finkelstein <jeffrey.finkelstein@protonmail.com>
This commit is contained in:
Krishna Nagam 2025-01-12 20:02:31 -05:00 committed by Jeffrey Finkelstein
parent dac35129d2
commit d82d038b54
2 changed files with 28 additions and 9 deletions

View file

@ -6,10 +6,12 @@
// spell-checker:ignore (chrono) Datelike Timelike ; (format) DATEFILE MMDDhhmm ; (vars) datetime datetimes // spell-checker:ignore (chrono) Datelike Timelike ; (format) DATEFILE MMDDhhmm ; (vars) datetime datetimes
use chrono::format::{Item, StrftimeItems}; use chrono::format::{Item, StrftimeItems};
use chrono::{DateTime, FixedOffset, Local, Offset, TimeDelta, Utc}; use chrono::{DateTime, FixedOffset, Local, Offset, TimeDelta, TimeZone, Utc};
#[cfg(windows)] #[cfg(windows)]
use chrono::{Datelike, Timelike}; use chrono::{Datelike, Timelike};
use chrono_tz::{OffsetName, Tz};
use clap::{crate_version, Arg, ArgAction, Command}; use clap::{crate_version, Arg, ArgAction, Command};
use iana_time_zone::get_timezone;
#[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))] #[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))]
use libc::{clock_settime, timespec, CLOCK_REALTIME}; use libc::{clock_settime, timespec, CLOCK_REALTIME};
use std::fs::File; use std::fs::File;
@ -272,8 +274,21 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
for date in dates { for date in dates {
match date { match date {
Ok(date) => { Ok(date) => {
// TODO - Revisit when chrono 0.5 is released. https://github.com/chronotope/chrono/issues/970
let tz = match std::env::var("TZ") {
// TODO Support other time zones...
Ok(s) if s == "UTC0" => Tz::Etc__UTC,
_ => match get_timezone() {
Ok(tz_str) => tz_str.parse().unwrap(),
Err(_) => Tz::Etc__UTC,
},
};
let offset = tz.offset_from_utc_date(&Utc::now().date_naive());
let tz_abbreviation = offset.abbreviation();
// GNU `date` uses `%N` for nano seconds, however crate::chrono uses `%f` // GNU `date` uses `%N` for nano seconds, however crate::chrono uses `%f`
let format_string = &format_string.replace("%N", "%f"); let format_string = &format_string
.replace("%N", "%f")
.replace("%Z", tz_abbreviation);
// Refuse to pass this string to chrono as it is crashing in this crate // Refuse to pass this string to chrono as it is crashing in this crate
if format_string.contains("%#z") { if format_string.contains("%#z") {
return Err(USimpleError::new( return Err(USimpleError::new(
@ -403,7 +418,7 @@ fn make_format_string(settings: &Settings) -> &str {
Rfc3339Format::Ns => "%F %T.%f%:z", Rfc3339Format::Ns => "%F %T.%f%:z",
}, },
Format::Custom(ref fmt) => fmt, Format::Custom(ref fmt) => fmt,
Format::Default => "%c", Format::Default => "%a %b %e %X %Z %Y",
} }
} }

View file

@ -144,11 +144,12 @@ fn test_date_utc() {
#[test] #[test]
fn test_date_utc_issue_6495() { fn test_date_utc_issue_6495() {
new_ucmd!() new_ucmd!()
.env("TZ", "UTC0")
.arg("-u") .arg("-u")
.arg("-d") .arg("-d")
.arg("@0") .arg("@0")
.succeeds() .succeeds()
.stdout_is("Thu Jan 1 00:00:00 1970\n"); .stdout_is("Thu Jan 1 00:00:00 UTC 1970\n");
} }
#[test] #[test]
@ -423,16 +424,18 @@ fn test_invalid_date_string() {
#[test] #[test]
fn test_date_one_digit_date() { fn test_date_one_digit_date() {
new_ucmd!() new_ucmd!()
.env("TZ", "UTC0")
.arg("-d") .arg("-d")
.arg("2000-1-1") .arg("2000-1-1")
.succeeds() .succeeds()
.stdout_contains("Sat Jan 1 00:00:00 2000"); .stdout_only("Sat Jan 1 00:00:00 UTC 2000\n");
new_ucmd!() new_ucmd!()
.env("TZ", "UTC0")
.arg("-d") .arg("-d")
.arg("2000-1-4") .arg("2000-1-4")
.succeeds() .succeeds()
.stdout_contains("Tue Jan 4 00:00:00 2000"); .stdout_only("Tue Jan 4 00:00:00 UTC 2000\n");
} }
#[test] #[test]
@ -464,6 +467,7 @@ fn test_date_parse_from_format() {
#[test] #[test]
fn test_date_from_stdin() { fn test_date_from_stdin() {
new_ucmd!() new_ucmd!()
.env("TZ", "UTC0")
.arg("-f") .arg("-f")
.arg("-") .arg("-")
.pipe_in( .pipe_in(
@ -473,8 +477,8 @@ fn test_date_from_stdin() {
) )
.succeeds() .succeeds()
.stdout_is( .stdout_is(
"Mon Mar 27 08:30:00 2023\n\ "Mon Mar 27 08:30:00 UTC 2023\n\
Sat Apr 1 12:00:00 2023\n\ Sat Apr 1 12:00:00 UTC 2023\n\
Sat Apr 15 18:30:00 2023\n", Sat Apr 15 18:30:00 UTC 2023\n",
); );
} }