Skip to content

date,touch: resolve local timezone abbreviations before year and in touch - #16996

Open
semanym wants to merge 2 commits into
uutils:mainfrom
semanym:fix/date-touch-local-tz-abbrev
Open

semanym wants to merge 2 commits into
uutils:mainfrom
semanym:fix/date-touch-local-tz-abbrev

Conversation

@semanym

@semanym semanym commented Nov 29, 2026

Copy link
Copy Markdown

Fixes #16773

Problem

PR #16865 resolved trailing timezone abbreviations in date against the current timezone, but two paths still fell back to parse_datetime's fixed abbreviation table (where CST is -06:00 and IST is +05:30):

  1. date's own default output places the abbreviation before the year (for example Sat Jun 1 12:00:00 CST 2024), so date -d shifted timestamps by +14 hours in Asia/Shanghai / Asia/Taipei and -4h30m in Europe/Dublin.
  2. touch -d passed inputs directly to parse_datetime without checking the current timezone for either trailing or non-trailing abbreviations.

Change

  • Added uucore::time::rewrite_local_tz_abbreviation to check candidate timezone abbreviation tokens at any position against the effective timezone (including inline TZ="..." rules and DST fall-back folds) before falling back to parse_datetime's fixed table.
  • Probing with j (the local-zone item in parse_datetime) preserves existing rejections of duplicate timezones, conflicting offsets, and epoch timestamps combined with a zone; matching local abbreviations are rewritten to explicit UTC±HH:MM offsets (or j for sub-minute historical LMT offsets), while non-local abbreviations remain untouched.
  • Wired both date and touch to share this helper, and added parameterized rstest integration tests covering round-tripping date output through date -d and touch -d, DST folds, inline TZ="...", non-local fallback preservation, and invalid combinations.

@codspeed

codspeed Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning, 1 note

⚠️ 124 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

ℹ️ 1 benchmark has no flamegraph

The flamegraph wasn't generated: most of the time was spent in runtime internals, which would leave nothing useful to analyze.

See affected benchmarks

✅ 423 untouched benchmarks
⏩ 78 skipped benchmarks1


Comparing semanym:fix/date-touch-local-tz-abbrev (e510ab6) with main (3ef8203)

Open in CodSpeed

Footnotes

  1. 78 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Comment thread src/uu/date/src/date.rs
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 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.


/// Extract an optional leading `TZ="..."` rule and return the byte offset
/// immediately after it along with the effective [`TimeZone`].
fn split_leading_tz(input: &str, default_tz: &TimeZone) -> (usize, TimeZone) {

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.

this duplicates input_timezone() in date.rs, could we have only one?
also, an unknown zone gives UTC here but the current zone there

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.

Unified into uucore::time::input_timezone and removed date.rs's copy. Unknown zone names fall back to UTC to match parse_datetime and GNU date.

Comment thread src/uucore/src/lib/features/time.rs Outdated

/// Return whether `word` is a calendar, weekday, meridiem, ordinal, or relative
/// keyword in GNU date syntax rather than a timezone abbreviation.
fn is_date_keyword(word: &str) -> bool {

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.

do we really need this list? a keyword will never match the local abbreviation anyway
if it's for perf, comparing the token against the zone abbreviations before probing seems simpler

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.

Dropped is_date_keyword and replaced it with zone_uses_abbreviation, which checks the zone's current and neighboring transition abbreviations before probing (also avoids probing non-local abbreviations in file_tz_abbreviations).

Comment thread src/uucore/src/lib/features/time.rs Outdated
.eq_ignore_ascii_case(abbrev)
})
.then_some(offset),
AmbiguousOffset::Fold { before, after } => [before, after].into_iter().find(|offset| {

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.

same closure twice, could be dedup, no?

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.

Deduplicated into a single matches closure.

Comment thread src/uu/touch/src/touch.rs Outdated
}

if let Ok(parsed) = parse_datetime::parse_datetime_at_date(ref_zoned, s)
let rewritten = uucore::time::rewrite_local_tz_abbreviation(s, &ref_zoned, |probe| {

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.

same closure as in date.rs, could it live in uucore?

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.

Moved the parse_datetime call into uucore::time::rewrite_local_tz_abbreviation behind an optional parse-datetime feature, so date and touch call it directly.

Comment thread src/uucore/src/lib/features/time.rs Outdated
/// `UTC±HH:MM` offset when it matches the effective timezone's abbreviation at
/// the parsed instant.
///
/// Ambiguous abbreviations such as `CST` (China/Cuba vs US Central) or `IST`

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.

please make this doc shorter :)

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.

Shortened to two lines.

…ouch

Resolve timezone abbreviation tokens against the effective timezone before falling back to parse_datetime's fixed table, regardless of token position (including before the year as in default date output) and consistently across both date and touch.

Fixes uutils#16773
@semanym
semanym force-pushed the fix/date-touch-local-tz-abbrev branch from b986495 to e510ab6 Compare December 4, 2026 07:15

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

date, touch: parsing date’s own output in Asia/Shanghai moves the timestamp forward 14 hours

2 participants