Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

date: display %Z alphabetic time zone abbreviation #7134

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ chrono = { version = "0.4.38", default-features = false, features = [
] }
clap = { version = "4.5", features = ["wrap_help", "cargo"] }
clap_complete = "4.4"
chrono-tz = "0.8.3"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some special reason for using this version instead of the current 0.10.0?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason, this was just how it was in the original pull request, I didn't change the dependencies. I'm sure they can be updated

iana-time-zone = "0.1.57"
clap_mangen = "0.2"
compare = "0.1.0"
coz = { version = "0.1.3" }
Expand Down
2 changes: 2 additions & 0 deletions src/uu/date/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ chrono = { workspace = true }
clap = { workspace = true }
uucore = { workspace = true }
parse_datetime = { workspace = true }
chrono-tz = { workspace = true }
iana-time-zone = { workspace = true }

[target.'cfg(unix)'.dependencies]
libc = { workspace = true }
Expand Down
21 changes: 18 additions & 3 deletions src/uu/date/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
// spell-checker:ignore (chrono) Datelike Timelike ; (format) DATEFILE MMDDhhmm ; (vars) datetime datetimes

use chrono::format::{Item, StrftimeItems};
use chrono::{DateTime, FixedOffset, Local, Offset, TimeDelta, Utc};
use chrono::{DateTime, FixedOffset, Local, Offset, TimeDelta, TimeZone, Utc};
#[cfg(windows)]
use chrono::{Datelike, Timelike};
use chrono_tz::{OffsetName, Tz};
use clap::{crate_version, Arg, ArgAction, Command};
use iana_time_zone::get_timezone;
#[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))]
use libc::{clock_settime, timespec, CLOCK_REALTIME};
use std::fs::File;
Expand Down Expand Up @@ -272,8 +274,21 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
for date in dates {
match 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`
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
if format_string.contains("%#z") {
return Err(USimpleError::new(
Expand Down Expand Up @@ -403,7 +418,7 @@ fn make_format_string(settings: &Settings) -> &str {
Rfc3339Format::Ns => "%F %T.%f%:z",
},
Format::Custom(ref fmt) => fmt,
Format::Default => "%c",
Format::Default => "%a %b %e %X %Z %Y",
}
}

Expand Down
16 changes: 10 additions & 6 deletions tests/by-util/test_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,12 @@ fn test_date_utc() {
#[test]
fn test_date_utc_issue_6495() {
new_ucmd!()
.env("TZ", "UTC0")
.arg("-u")
.arg("-d")
.arg("@0")
.succeeds()
.stdout_is("Thu Jan 1 00:00:00 1970\n");
.stdout_is("Thu Jan 1 00:00:00 UTC 1970\n");
}

#[test]
Expand Down Expand Up @@ -423,16 +424,18 @@ fn test_invalid_date_string() {
#[test]
fn test_date_one_digit_date() {
new_ucmd!()
.env("TZ", "UTC0")
.arg("-d")
.arg("2000-1-1")
.succeeds()
.stdout_contains("Sat Jan 1 00:00:00 2000");
.stdout_only("Sat Jan 1 00:00:00 UTC 2000\n");

new_ucmd!()
.env("TZ", "UTC0")
.arg("-d")
.arg("2000-1-4")
.succeeds()
.stdout_contains("Tue Jan 4 00:00:00 2000");
.stdout_only("Tue Jan 4 00:00:00 UTC 2000\n");
}

#[test]
Expand Down Expand Up @@ -464,6 +467,7 @@ fn test_date_parse_from_format() {
#[test]
fn test_date_from_stdin() {
new_ucmd!()
.env("TZ", "UTC0")
.arg("-f")
.arg("-")
.pipe_in(
Expand All @@ -473,8 +477,8 @@ fn test_date_from_stdin() {
)
.succeeds()
.stdout_is(
"Mon Mar 27 08:30:00 2023\n\
Sat Apr 1 12:00:00 2023\n\
Sat Apr 15 18:30:00 2023\n",
"Mon Mar 27 08:30:00 UTC 2023\n\
Sat Apr 1 12:00:00 UTC 2023\n\
Sat Apr 15 18:30:00 UTC 2023\n",
);
}
Loading