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 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
510c73a
fix: replace %Z specifier with locale TZ abbr
KrishnaNagam Aug 15, 2023
1fad29c
formatted code with cargo fmt
KrishnaNagam Aug 15, 2023
f5a4e54
date: added cargo.lock changes
KrishnaNagam Aug 16, 2023
ab2b30c
Moved iana-time-zone, chrono-tz to root cargo.toml
KrishnaNagam Aug 19, 2023
c1ca29e
Merge branch 'uutils:main' into main
KrishnaNagam Aug 19, 2023
39f3318
Merge branch 'uutils:main' into main
KrishnaNagam Aug 19, 2023
be50227
Merge branch 'main' into main
KrishnaNagam Aug 19, 2023
1b78a3a
Merge branch 'main' into main
sylvestre Oct 6, 2023
6c636d8
Merge branch 'main' into main
sylvestre Dec 25, 2023
c118bdc
Merge branch 'main' into main
tertsdiepraam Feb 2, 2024
ba41cdb
Merge remote-tracking branch 'upstream/main'
KrishnaNagam Apr 20, 2024
140e108
fix chrono imports
KrishnaNagam Apr 20, 2024
4dd0dfc
fix test cases;
KrishnaNagam Apr 20, 2024
9f97969
cargo fmt date.rs
KrishnaNagam Apr 20, 2024
a196ea6
Seperated test case for macos
KrishnaNagam Apr 20, 2024
543b017
cargo fmt
KrishnaNagam Apr 20, 2024
2609582
fix Cargo.lock
KrishnaNagam Apr 21, 2024
4e434ea
Merge pull request #3 fix Cargo.lock
KrishnaNagam Apr 21, 2024
3246522
Merge branch 'main' into main
KrishnaNagam Apr 21, 2024
06158ac
Merge branch 'main' into main
KrishnaNagam Apr 21, 2024
2033816
Merge branch 'main' into main
sylvestre Apr 30, 2024
00db4cf
Merge branch 'main' into main
sylvestre Nov 25, 2024
ff43473
Merge branch 'main' into main
sylvestre Dec 2, 2024
3e23381
Merge branch 'main' into main
sylvestre Dec 16, 2024
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 +283 to +285
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