Skip to content
Merged
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
25 changes: 13 additions & 12 deletions src/uu/date/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,12 @@ fn tz_abbrev_to_iana(abbrev: &str) -> Option<&str> {
cache.get(abbrev).map(|s| s.as_str())
}

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

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

// Get the offset for this specific timestamp in the target timezone
let zoned = ts.to_zoned(tz);
let offset_str = format!("{}", zoned.offset());

// Replace abbreviation with offset
return format!("{date_part} {offset_str}");
return Some(ts.to_zoned(tz));
}
}
}
}
}

// No abbreviation found or couldn't resolve, return original
s.to_string()
None
}

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

match parse_datetime::parse_datetime(&resolved) {
match parse_datetime::parse_datetime(s.as_ref()) {
Ok(date) => {
// Convert to system timezone for display
// (parse_datetime 0.13 returns Zoned in the input's timezone)
Expand Down
Loading