Respect calendar type of Ruby and PostgreSQL - #736
Open
larskanis wants to merge 1 commit into
Open
Conversation
larskanis
marked this pull request as ready for review
August 8, 2026 20:14
PostgreSQL uses gregorian calendar for all dates. It is described here: https://www.postgresql.org/docs/10/datetime-units-history.html Ruby treats dates before 1852-10-05 as julian calendar and after 1582-10-14 as gregorian calendar. It is described here: https://docs.ruby-lang.org/en/master/language/calendars_rdoc.html#argument-start In order to encode and decode the correct date, this PR encodes Date values send to the server as gregorian calendar. Non-gregorian dates are converted to gregorian calender before sent. The other way around, Date values decoded from the server are explicit interpret and labeled as gregorian calendar. The new behavior could be surprising when a julian date is passed through the server and changed to gregorian like so. But it is how the two parties represent one and the same date: ```ruby conn.exec_params("SELECT $1::date", [Date.new(1581, 5, 15)]).values => [[#<Date: 1581-05-25 ((2298653j,0s,0n),+0s,-Infj)>]] ``` So far the PG::BasicTypeMapForQueries didn't use the Date encoder. This was because it didn't have an advantage over standard `to_s` conversion. Now that the encoder ensures that the date is sent as gregorian calender value, it makes sense to enable it for query parameters. Fixes ged#725
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PostgreSQL uses gregorian calendar for all dates.
It is described here:
https://www.postgresql.org/docs/10/datetime-units-history.html
Ruby treats dates before 1852-10-05 as julian calendar and after 1582-10-14 as gregorian calendar. It is described here:
https://docs.ruby-lang.org/en/master/language/calendars_rdoc.html#argument-start
In order to encode and decode the correct date, this PR encodes Date values send to the server as gregorian calendar. Non-gregorian dates are converted to gregorian calender before sent. The other way around, Date values decoded from the server are explicit interpret and labeled as gregorian calendar.
The new behavior could be surprising when a julian date is passed through the server and changed to gregorian like so. But it is how the two parties represent one and the same date:
So far the PG::BasicTypeMapForQueries didn't use the Date encoder. This was because it didn't have an advantage over standard
to_sconversion. Now that the encoder ensures that the date is sent as gregorian calender value, it makes sense to enable it for query parameters.Fixes #725