Skip to content

Commit c9099cb

Browse files
authored
Merge pull request #9997 from CrazyRoka/date-optimize-abbreviation
date: avoid double parsing when resolving timezone abbreviations
2 parents ce58025 + fe65d17 commit c9099cb

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/uu/date/src/date.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -671,9 +671,12 @@ fn tz_abbrev_to_iana(abbrev: &str) -> Option<&str> {
671671
cache.get(abbrev).map(|s| s.as_str())
672672
}
673673

674-
/// Resolve timezone abbreviation in date string and replace with numeric offset.
675-
/// Returns the modified string with offset, or original if no abbreviation found.
676-
fn resolve_tz_abbreviation<S: AsRef<str>>(date_str: S) -> String {
674+
/// Attempts to parse a date string that contains a timezone abbreviation (e.g. "EST").
675+
///
676+
/// If an abbreviation is found and the date is parsable, returns `Some(Zoned)`.
677+
/// Returns `None` if no abbreviation is detected or if parsing fails, indicating
678+
/// that standard parsing should be attempted.
679+
fn try_parse_with_abbreviation<S: AsRef<str>>(date_str: S) -> Option<Zoned> {
677680
let s = date_str.as_ref();
678681

679682
// Look for timezone abbreviation at the end of the string
@@ -697,19 +700,15 @@ fn resolve_tz_abbreviation<S: AsRef<str>>(date_str: S) -> String {
697700
let ts = parsed.timestamp();
698701

699702
// Get the offset for this specific timestamp in the target timezone
700-
let zoned = ts.to_zoned(tz);
701-
let offset_str = format!("{}", zoned.offset());
702-
703-
// Replace abbreviation with offset
704-
return format!("{date_part} {offset_str}");
703+
return Some(ts.to_zoned(tz));
705704
}
706705
}
707706
}
708707
}
709708
}
710709

711710
// No abbreviation found or couldn't resolve, return original
712-
s.to_string()
711+
None
713712
}
714713

715714
/// Parse a `String` into a `DateTime`.
@@ -724,10 +723,12 @@ fn resolve_tz_abbreviation<S: AsRef<str>>(date_str: S) -> String {
724723
fn parse_date<S: AsRef<str> + Clone>(
725724
s: S,
726725
) -> Result<Zoned, (String, parse_datetime::ParseDateTimeError)> {
727-
// First, try to resolve any timezone abbreviations
728-
let resolved = resolve_tz_abbreviation(s.as_ref());
726+
// First, try to parse any timezone abbreviations
727+
if let Some(zoned) = try_parse_with_abbreviation(s.as_ref()) {
728+
return Ok(zoned);
729+
}
729730

730-
match parse_datetime::parse_datetime(&resolved) {
731+
match parse_datetime::parse_datetime(s.as_ref()) {
731732
Ok(date) => {
732733
// Convert to system timezone for display
733734
// (parse_datetime 0.13 returns Zoned in the input's timezone)

0 commit comments

Comments
 (0)