tlocke / pg8000

A Pure-Python PostgreSQL Driver
BSD 3-Clause "New" or "Revised" License
515 stars 46 forks source link

Fix timestamptz_in breaking on half-hour timezones #117

Closed WarrenHood closed 2 years ago

WarrenHood commented 2 years ago

timestamptz_in would naively add `00' to the end of a timestamp before parsing it. This caused issues when timestamps with half-hour timezones were used.

For example, a timestamp ending in '+01:30' would become '+01:3000', which is invalid because the use of ':' is inconsistent. This results in errors like ValueError: Inconsistent use of : in +01:3000.

This PR fixes this by right padding the supplied timezone offset with zeros to a length of at least 4, and removes all colons from the offset for consistency. Timestamps without any timezone offset will be treated as UTC.

I have also added some unit tests to test the timestamptz_in converter with +/- timezone offsets, including half-hour and no timezone offsets.

tlocke commented 2 years ago

Hi @WarrenHood, thanks for your work on this, I've used it in https://github.com/tlocke/pg8000/commit/eca52a45877ba80a62de62c95a38f1a042f783bd. In the end I went with using dateutil's parse function, but trying the original method first for performance reasons. Anyway, the bug should be fixed now, but feel free to re-open if there's anything wrong.