Skip to content

Fixes socketry/db-postgres#6 - Parsing of timestamps with microsecond precison #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

cyclotron3k
Copy link
Contributor

@cyclotron3k cyclotron3k commented Nov 9, 2022

Fixes #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:MM format
  • Ruby <= 2.6 can't handle timezones in +HH format (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

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

@ioquatix
Copy link
Member

Do you think it makes sense to add tests to prevent regressions?

@cyclotron3k
Copy link
Contributor Author

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.

@cyclotron3k
Copy link
Contributor Author

cyclotron3k commented Nov 11, 2022

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)

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.

Issue getting TIMESTAMP and TIMESTAMPTZ columns from database
2 participants