-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: main
Are you sure you want to change the base?
Changes from 9 commits
510c73a
1fad29c
f5a4e54
ab2b30c
c1ca29e
39f3318
be50227
1b78a3a
6c636d8
c118bdc
ba41cdb
140e108
4dd0dfc
9f97969
a196ea6
543b017
2609582
4e434ea
3246522
06158ac
2033816
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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); | ||
// Refuse to pass this string to chrono as it is crashing in this crate | ||
if format_string.contains("%#z") { | ||
return Err(USimpleError::new( | ||
|
@@ -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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layman's question: Will this not override any locales from There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 😄 |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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?