Skip to content
Open
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions fuzz/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 src/uu/date/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ parse_datetime = { workspace = true }
thiserror = { workspace = true }
uucore = { workspace = true, features = [
"i18n-datetime",
"parse-datetime",
"parser",
"quoting-style",
"time",
] }

[target.'cfg(unix)'.dependencies]
Expand Down
40 changes: 7 additions & 33 deletions src/uu/date/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1635,20 +1635,6 @@ fn try_parse_with_abbreviation<S: AsRef<str>>(
parse_datetime::parse_datetime_at_date(now.clone(), format!("{date_part} EST")).ok()?;
}

// An abbreviation can name several zones: "CST" is US Central, China and
// Cuba; "IST" is India, Israel and Ireland. GNU resolves it against the
// current zone before consulting its own table, so "CST" is +0800 under
// `TZ=Asia/Shanghai` and "IST" is +0100 under `TZ=Europe/Dublin`. Only when
// the current zone does not go by that abbreviation at this instant does
// the table decide. `zoned` is `date_part` read in `now`'s zone, so its
// timestamp is the instant to ask about.
let local = now.time_zone();
let tz = if local.to_offset_info(zoned.timestamp()).abbreviation() == last_word {
local.clone()
} else {
tz
};

let zoned = zoned.datetime().to_zoned_datetime(tz).ok()?;

// The trailing abbreviation only describes the *input* timezone. For display,
Expand Down Expand Up @@ -2135,20 +2121,6 @@ fn parse_local_datetime(input: &str) -> Option<jiff::civil::DateTime> {
jiff::civil::DateTime::new(year, month, day, hour, minute, second, nanosecond).ok()
}

fn input_timezone(input: &str, now: &ZonedDateTime) -> TimeZone {
let trimmed = input.trim_start();
if let Some(rest) = trimmed.strip_prefix("TZ=\"")
&& let Some(end) = rest.find('"')
{
let name = &rest[..end];
TimeZone::get(name)
.or_else(|_| TimeZone::posix(name))
.unwrap_or_else(|_| now.time_zone().clone())
} else {
now.time_zone().clone()
}
}

fn has_explicit_numeric_timezone(input: &str) -> bool {
input.split_whitespace().any(|token| {
if is_numeric_timezone(token) {
Expand Down Expand Up @@ -2180,7 +2152,7 @@ fn is_nonexistent_local_time(input: &str, normalized: &str, now: &ZonedDateTime)
return false;
};
matches!(
input_timezone(input, now)
uucore::time::input_timezone(input, now.time_zone())
.to_ambiguous_zoned_datetime(local)
.offset(),
AmbiguousOffset::Gap { .. }
Expand All @@ -2200,10 +2172,12 @@ fn parse_date<S: AsRef<str>>(

// Normalize parser-specific compatibility forms before parsing.
let normalized = normalize_parse_input(input_str);
let parse_input = normalized.as_ref();
let local_tz_rewritten = uucore::time::rewrite_local_tz_abbreviation(normalized.as_ref(), now);
let has_local_tz_abbrev = local_tz_rewritten.is_some();
let parse_input = local_tz_rewritten.as_deref().unwrap_or(normalized.as_ref());

// First, try to parse any timezone abbreviations
if let Some(zoned) = try_parse_with_abbreviation(parse_input, now) {
// First, try to parse any non-local trailing timezone abbreviations

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

with this, the local zone check at the end of try_parse_with_abbreviation (the CST/IST block) is now dead, no?
please remove it, we shouldn't keep two ways of doing the same thing

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Removed — local abbreviations are already rewritten beforehand.

if !has_local_tz_abbrev && let Some(zoned) = try_parse_with_abbreviation(parse_input, now) {
if dbg_opts.debug {
let mut err = stderr().lock();
let _ = writeln!(
Expand Down Expand Up @@ -2233,7 +2207,7 @@ fn parse_date<S: AsRef<str>>(
Ok(ParsedDateTime::InRange(date)) => {
// parse_datetime may normalize a nonexistent wall-clock time;
// reject that result so DST gaps are not silently accepted.
if is_nonexistent_local_time(input_str, parse_input, now) {
if !has_local_tz_abbrev && is_nonexistent_local_time(input_str, parse_input, now) {
return Err((
escape_for_error(input_str.as_bytes()),
parse_datetime::ParseDateTimeError::InvalidInput,
Expand Down
7 changes: 6 additions & 1 deletion src/uu/touch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ fluent = { workspace = true }
jiff = { workspace = true }
parse_datetime = { workspace = true }
thiserror = { workspace = true }
uucore = { workspace = true, features = ["libc", "parser"] }
uucore = { workspace = true, features = [
"libc",
"parse-datetime",
"parser",
"time",
] }

[dev-dependencies]
temp-env = { workspace = true }
Expand Down
5 changes: 4 additions & 1 deletion src/uu/touch/src/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,10 @@ fn parse_date(ref_zoned: ZonedDateTime, s: &str) -> Result<FileTime, TouchError>
return Ok(FileTime::from_unix_time(*ts, 0));
}

if let Ok(parsed) = parse_datetime::parse_datetime_at_date(ref_zoned, s)
let rewritten = uucore::time::rewrite_local_tz_abbreviation(s, &ref_zoned);
let date_str = rewritten.as_deref().unwrap_or(s);

if let Ok(parsed) = parse_datetime::parse_datetime_at_date(ref_zoned, date_str)
&& let Some(zoned) = parsed.into_zoned()
{
return Ok(timestamp_to_filetime(zoned.timestamp()));
Expand Down
4 changes: 3 additions & 1 deletion src/uucore/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# spell-checker:ignore (features) extendedbigdecimal tzdb zoneinfo logind libcrypto xxhash
# spell-checker:ignore (features) extendedbigdecimal tzdb zoneinfo logind libcrypto xxhash datetime

[package]
name = "uucore"
Expand Down Expand Up @@ -34,6 +34,7 @@ jiff = { workspace = true, optional = true, features = [
] }
libc = { workspace = true, optional = true }
os_display = { workspace = true }
parse_datetime = { workspace = true, optional = true }
rustc-hash = { workspace = true }
# Not optional: os_display already pulls unicode-width into every uucore build,
# so making it a direct dependency here is free and keeps char_width available
Expand Down Expand Up @@ -246,6 +247,7 @@ version-cmp = []
wide = []
zeroed-alloc = ["dep:zerocopy"]
time = ["jiff"]
parse-datetime = ["dep:parse_datetime", "time"]
uptime = ["jiff", "libc", "rustix/time", "utmp-classic", "utmpx", "windows-sys"]
benchmark = ["itertools", "tempfile"]

Expand Down
Loading
Loading