Skip to content

Commit c322fb5

Browse files
authored
Merge pull request #7134 from jfinkels/date-timezone-name
date: display %Z alphabetic time zone abbreviation
2 parents d7800b5 + d82d038 commit c322fb5

File tree

5 files changed

+65
-9
lines changed

5 files changed

+65
-9
lines changed

Cargo.lock

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ chrono = { version = "0.4.38", default-features = false, features = [
280280
] }
281281
clap = { version = "4.5", features = ["wrap_help", "cargo"] }
282282
clap_complete = "4.4"
283+
chrono-tz = "0.8.3"
284+
iana-time-zone = "0.1.57"
283285
clap_mangen = "0.2"
284286
compare = "0.1.0"
285287
coz = { version = "0.1.3" }

src/uu/date/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ chrono = { workspace = true }
2222
clap = { workspace = true }
2323
uucore = { workspace = true }
2424
parse_datetime = { workspace = true }
25+
chrono-tz = { workspace = true }
26+
iana-time-zone = { workspace = true }
2527

2628
[target.'cfg(unix)'.dependencies]
2729
libc = { workspace = true }

src/uu/date/src/date.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
// spell-checker:ignore (chrono) Datelike Timelike ; (format) DATEFILE MMDDhhmm ; (vars) datetime datetimes
77

88
use chrono::format::{Item, StrftimeItems};
9-
use chrono::{DateTime, FixedOffset, Local, Offset, TimeDelta, Utc};
9+
use chrono::{DateTime, FixedOffset, Local, Offset, TimeDelta, TimeZone, Utc};
1010
#[cfg(windows)]
1111
use chrono::{Datelike, Timelike};
12+
use chrono_tz::{OffsetName, Tz};
1213
use clap::{crate_version, Arg, ArgAction, Command};
14+
use iana_time_zone::get_timezone;
1315
#[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))]
1416
use libc::{clock_settime, timespec, CLOCK_REALTIME};
1517
use std::fs::File;
@@ -272,8 +274,21 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
272274
for date in dates {
273275
match date {
274276
Ok(date) => {
277+
// TODO - Revisit when chrono 0.5 is released. https://github.com/chronotope/chrono/issues/970
278+
let tz = match std::env::var("TZ") {
279+
// TODO Support other time zones...
280+
Ok(s) if s == "UTC0" => Tz::Etc__UTC,
281+
_ => match get_timezone() {
282+
Ok(tz_str) => tz_str.parse().unwrap(),
283+
Err(_) => Tz::Etc__UTC,
284+
},
285+
};
286+
let offset = tz.offset_from_utc_date(&Utc::now().date_naive());
287+
let tz_abbreviation = offset.abbreviation();
275288
// GNU `date` uses `%N` for nano seconds, however crate::chrono uses `%f`
276-
let format_string = &format_string.replace("%N", "%f");
289+
let format_string = &format_string
290+
.replace("%N", "%f")
291+
.replace("%Z", tz_abbreviation);
277292
// Refuse to pass this string to chrono as it is crashing in this crate
278293
if format_string.contains("%#z") {
279294
return Err(USimpleError::new(
@@ -403,7 +418,7 @@ fn make_format_string(settings: &Settings) -> &str {
403418
Rfc3339Format::Ns => "%F %T.%f%:z",
404419
},
405420
Format::Custom(ref fmt) => fmt,
406-
Format::Default => "%c",
421+
Format::Default => "%a %b %e %X %Z %Y",
407422
}
408423
}
409424

tests/by-util/test_date.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,12 @@ fn test_date_utc() {
144144
#[test]
145145
fn test_date_utc_issue_6495() {
146146
new_ucmd!()
147+
.env("TZ", "UTC0")
147148
.arg("-u")
148149
.arg("-d")
149150
.arg("@0")
150151
.succeeds()
151-
.stdout_is("Thu Jan 1 00:00:00 1970\n");
152+
.stdout_is("Thu Jan 1 00:00:00 UTC 1970\n");
152153
}
153154

154155
#[test]
@@ -423,16 +424,18 @@ fn test_invalid_date_string() {
423424
#[test]
424425
fn test_date_one_digit_date() {
425426
new_ucmd!()
427+
.env("TZ", "UTC0")
426428
.arg("-d")
427429
.arg("2000-1-1")
428430
.succeeds()
429-
.stdout_contains("Sat Jan 1 00:00:00 2000");
431+
.stdout_only("Sat Jan 1 00:00:00 UTC 2000\n");
430432

431433
new_ucmd!()
434+
.env("TZ", "UTC0")
432435
.arg("-d")
433436
.arg("2000-1-4")
434437
.succeeds()
435-
.stdout_contains("Tue Jan 4 00:00:00 2000");
438+
.stdout_only("Tue Jan 4 00:00:00 UTC 2000\n");
436439
}
437440

438441
#[test]
@@ -464,6 +467,7 @@ fn test_date_parse_from_format() {
464467
#[test]
465468
fn test_date_from_stdin() {
466469
new_ucmd!()
470+
.env("TZ", "UTC0")
467471
.arg("-f")
468472
.arg("-")
469473
.pipe_in(
@@ -473,8 +477,8 @@ fn test_date_from_stdin() {
473477
)
474478
.succeeds()
475479
.stdout_is(
476-
"Mon Mar 27 08:30:00 2023\n\
477-
Sat Apr 1 12:00:00 2023\n\
478-
Sat Apr 15 18:30:00 2023\n",
480+
"Mon Mar 27 08:30:00 UTC 2023\n\
481+
Sat Apr 1 12:00:00 UTC 2023\n\
482+
Sat Apr 15 18:30:00 UTC 2023\n",
479483
);
480484
}

0 commit comments

Comments
 (0)