Skip to content

fix(datetime): raise specific "Missing zone offset" error in timestamptz_to_nanos#3505

Open
vishnuprakaz wants to merge 1 commit into
apache:mainfrom
vishnuprakaz:fix/timestamptz-nanos-missing-zone-offset-error
Open

fix(datetime): raise specific "Missing zone offset" error in timestamptz_to_nanos#3505
vishnuprakaz wants to merge 1 commit into
apache:mainfrom
vishnuprakaz:fix/timestamptz-nanos-missing-zone-offset-error

Conversation

@vishnuprakaz

Copy link
Copy Markdown
Contributor

Rationale for this change

timestamptz_to_nanos should raise a clear "Missing zone
offset" error when it's given a nanosecond timestamp that has no
timezone. Today it can't: the check meant to detect a zone-less
timestamp accidentally tests the same pattern as the success
check right above it, so it never matches. The input falls
through to the vague "Invalid timestamp with zone" error
instead.

The fix is one line check the zone-less pattern
(ISO_TIMESTAMP_NANO) instead, mirroring
timestamptz_to_micros, which already does this correctly.
(Introduced in #1632.)

Are these changes tested?

Yes added tests covering the error messages for both
timestamptz_to_nanos and timestamp_to_nanos.

Are there any user-facing changes?

Only a clearer error message: a zone-less nanosecond timestamp
now reports "Missing zone offset" instead of "Invalid timestamp
with zone". No API changes.

  offset' error in timestamptz_to_nanos
Comment on lines +138 to +140
def test_timestamp_to_nanos_unexpected_zone_offset() -> None:
with pytest.raises(ValueError, match="Zone offset provided, but not expected: 2025-02-23T16:21:44.375612001-04:00"):
timestamp_to_nanos("2025-02-23T16:21:44.375612001-04:00")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: I'd move this test under test_timestamp_to_nanos so related timestamp_to_nanos tests stay close together.

if ISO_TIMESTAMP_NANO.fullmatch(timestamptz_str):
# When we can match a timestamp without a zone, we can give a more specific error
raise ValueError(f"Missing zone offset: {timestamptz_str} (must be ISO-8601)")
raise ValueError(f"Invalid timestamp with zone: {timestamptz_str} (must be ISO-8601)")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: We could add test cases for Invalid timestamp with zone in this method and Invalid timestamp without zone in timestamp_to_nanos as a follow-up.

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.

2 participants