Skip to content

Commit ad2e830

Browse files
committed
fix: anchor time-only test expectations to the string's own zone
Also makes parse_timestamp_pattern's time-only arm exhaustive so a new TimestampPattern variant is a compile error rather than a silent fall-through to the time-only parser.
1 parent 36c48a5 commit ad2e830

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

  • native/spark-expr/src/conversion_funcs

‎native/spark-expr/src/conversion_funcs/string.rs‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,7 +1891,18 @@ fn parse_timestamp_pattern<T: TimeZone>(
18911891
Some(TimestampPattern::Minute) => get_timestamp_values(value, "minute", tz)?,
18921892
Some(TimestampPattern::Second) => get_timestamp_values(value, "second", tz)?,
18931893
Some(TimestampPattern::Microsecond) => get_timestamp_values(value, "microsecond", tz)?,
1894-
Some(_) => parse_str_to_time_only_timestamp(value, tz)?,
1894+
// Listed out rather than caught by a `Some(_)` arm on purpose: a catch-all would route
1895+
// any future non-time-only shape into the time-only parser and produce a wrong value
1896+
// silently. Spelling out the variants makes adding one a compile error here.
1897+
Some(
1898+
TimestampPattern::TimeOnlyH
1899+
| TimestampPattern::TimeOnlyHm
1900+
| TimestampPattern::TimeOnlyHms
1901+
| TimestampPattern::TimeOnlyHmsu
1902+
| TimestampPattern::BareHm
1903+
| TimestampPattern::BareHms
1904+
| TimestampPattern::BareHmsu,
1905+
) => parse_str_to_time_only_timestamp(value, tz)?,
18951906
None => None,
18961907
};
18971908

@@ -3404,9 +3415,14 @@ mod tests {
34043415

34053416
// Time-only strings are anchored to the current date, so assert against a
34063417
// today-relative expectation rather than a fixed instant.
3407-
let today = chrono::Utc::now().date_naive();
3418+
//
3419+
// "Today" here is the current date *in the string's own zone*, not in UTC: the parser
3420+
// resolves the anchor with `tz.from_utc_datetime(Utc::now())`, and for a string carrying
3421+
// its own offset that `tz` is the offset from the string. Using the UTC date instead
3422+
// makes every `+07:30` case fail by 24 hours after 16:30 UTC, i.e. for part of each day.
34083423
let today_micros = |hour: i64, min: i64, sec: i64, micros: i64, offset: i64| {
34093424
use chrono::Datelike;
3425+
let today = (chrono::Utc::now() + chrono::Duration::seconds(offset)).date_naive();
34103426
Some(at_offset(
34113427
utc_micros(
34123428
today.year() as i64,
@@ -3421,6 +3437,13 @@ mod tests {
34213437
))
34223438
};
34233439
check("18:12:15", today_micros(18, 12, 15, 0, 0));
3440+
3441+
// Extreme offsets in both directions, so that the zone date differs from the UTC date
3442+
// whatever hour CI runs at: +14:00 crosses from 10:00 UTC onward, -12:00 crosses before
3443+
// 12:00 UTC, and together they cover every hour. Anchoring the expectation to the UTC
3444+
// date instead of the string's own zone fails one of these by exactly 24 hours.
3445+
check("T18:12:15+14:00", today_micros(18, 12, 15, 0, 14 * 3600));
3446+
check("T18:12:15-12:00", today_micros(18, 12, 15, 0, -12 * 3600));
34243447
let expected = today_micros(18, 12, 15, 123_120, plus_730);
34253448
check("T18:12:15.12312+7:30", expected);
34263449
check("T18:12:15.12312 UTC+07:30", expected);

0 commit comments

Comments
 (0)