Fixes socketry/db-postgres#6 - Parsing of timestamps with microsecond precison
Fixes socketry/db-postgres#6
Postgres can return timestamps with microsecond precision, and db-postgres is failing to handle these well.
This is an attempt to fix that behaviour.
Notes:
- An invalid timestamp will return a
nil(as before), but maybe an explicit raise would be better? - Ruby <= 2.6 couldn't handle a timezone of
UTC- only+HH:MMformat - Ruby <= 2.6 can't handle timezones in
+HHformat (i.e. optional minutes) - Ruby <= 2.5 can handle microsecond precision, but typically hides it when testing in
irb:/
Types of Changes
- Bug fix.
Contribution
- [x] I added tests for my changes.
- [x] I tested my changes locally.
- [x] I agree to the Developer's Certificate of Origin 1.1.
I tested with the following values (collected from Postgres):
strings = [
"2022-01-02 12:13:14",
"2022-01-02 12:13:14-11",
"2018-09-02 03:39:19+04:30",
"2020-09-25 03:16:48.648682+00",
"2022-01-02 12:13:14.123456",
"2022-01-02 12:13:14.123456+08"
]
And I tested in Ruby 2.3 -> 3.1
Do you think it makes sense to add tests to prevent regressions?
Do you think it makes sense to add tests to prevent regressions?
Good idea. Just added some.
I could have just written unit tests for the Types, but seeing as your tests already depend on a real db connection, I decided to make use of that, and round-trip the test timestamps via the database to provide some guarantees that what I'm testing is valid.
Happy to swap it out for unit tests though. And let me know if you're not a fan of the indentation/coding style/etc.
Oh just a thought; infinity, -infinity and null are also values that Postgres can return in a timestamp column.
With the infinities, Sequel raises and exception, and ActiveRecord just returns the strings "infinity" and "-infinity".
Date has the concept of Infinity, but it doesn't seem to be very useful in practice:
irb(main):001:0> require 'date'
=> true
irb(main):002:0> Date::Infinity.new < Date.today
(irb):2:in `<': comparison of Date::Infinity with Date failed (ArgumentError)