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: fix %Z specifier does not print TZ abbr #5164

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 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
48 changes: 45 additions & 3 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 @@ -268,6 +268,8 @@ chrono = { version = "^0.4.31", default-features = false, features = [
"alloc",
"clock",
] }
chrono-tz = "0.8.3"
iana-time-zone = "0.1.57"
clap = { version = "4.4", features = ["wrap_help", "cargo"] }
clap_complete = "4.4"
clap_mangen = "0.2"
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 @@ -20,6 +20,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
15 changes: 12 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, Duration, FixedOffset, Local, Offset, Utc};
use chrono::{DateTime, Duration, FixedOffset, Local, Offset, 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 @@ -265,8 +267,15 @@ 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_str = get_timezone().unwrap_or("Etc/UTC".to_string());
let tz: Tz = tz_str.parse().unwrap();
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);
Comment on lines +285 to +287
Copy link
Member

Choose a reason for hiding this comment

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

This is not quite correct, because this means that %f should be escaped. Ultimately, we might need to completely customize this. I suppose it's good enough for now though.

Copy link
Author

Choose a reason for hiding this comment

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

Actually, this line of code was already there. Cargo fmt has put it in new line 😀. I am interested in solving this though in my next PR.

I didn't get why %f need to be escaped. Did you mean, if there is %f already within the input, it should be escaped?

// 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 @@ -396,7 +405,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 %-d %X %Z %Y",
Copy link
Contributor

Choose a reason for hiding this comment

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

Layman's question: Will this not override any locales from LC_TIME we could use? That could be a blocker for #3143

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but this change doesn't make it any worse than then the current implementation 😄

}
}

Expand Down
Loading