Skip to content

Decode text timestamps with timezone offsets of a day or more (SET TIME ZONE 30 no longer raises) - #766

Open
czj wants to merge 2 commits into
ged:masterfrom
czj:fix/text-timestamp-offset-beyond-a-day
Open

czj wants to merge 2 commits into
ged:masterfrom
czj:fix/text-timestamp-offset-beyond-a-day

Conversation

@czj

@czj czj commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Another one from the #761 review. PostgreSQL accepts SET TIME ZONE offsets up to ±167:59:59 and prints them as is, so after SET TIME ZONE 30, SELECT now() comes back as 2024-01-02 18:00:00+30. PG::TextDecoder::Timestamp raises ArgumentError: utc_offset out of range on that, because Ruby's Time only takes offsets under a day, and the whole query fails. From 100 hours on, the offset has a third digit, which the parser doesn't expect, so the value comes back as a String.

This reads the third digit, only when the first two aren't 00, since PostgreSQL prints hours with %02d and the existing spec rejects +000:25:21. When the offset can't go into a Time, the decoder keeps the instant and returns it in UTC, or in local time with TIMESTAMP_APP_LOCAL, the way it already does for values without an offset. That's the one trade-off: the original offset is lost, but I couldn't find a Time that holds it. Offsets under a day go through the same code as before.

To check it against the server, I decoded 5 timestamps from 1901 to 294276 under every quarter-hour offset from -167 to +167 hours, with BasicTypeMapForResults on PostgreSQL 18.6 and Ruby 4.0.9 (arm64-darwin), and compared them with to_char(t AT TIME ZONE 'UTC', …). With this patch, all 6,685 values match, and so do 4,020 more from 4713 BC to 1883 compared with extract(epoch). On master, 608 offsets make the query raise, and 2,690 values are wrong. I also compiled it with the fast path disabled to exercise the 32-bit time_t path, and the timestamp specs pass there too, in UTC and in Europe/Paris. The new spec fails on master.

Is UTC the fallback you'd expect here, or would you rather raise a clearer error?


🤖 Generated with Claude Code, but prompted, read, and verified by @czj, a caring human :)

czj and others added 2 commits October 27, 2026 21:50
PostgreSQL accepts SET TIME ZONE offsets up to 167:59:59 and prints them
as is ("+30", "+100", "-24:00:01"). Ruby's Time can't carry an offset of
a day or more, so the decoder raised "utc_offset out of range", and
three digit hours fell back to a String.

Read the third hour digit, and keep the instant of values whose offset
doesn't fit, returned as UTC or local time per TIMESTAMP_APP_*.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TruffleRuby's Time raises ArgumentError on a utc_offset of 86399, which
CRuby accepts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant