Fix Date timezone handling for PostgreSQL PreferSimpleProtocol #312
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.
The
datatypes.Date
type was incorrectly handling timezones when using PostgreSQL withPreferSimpleProtocol: true
, causing dates to be stored as the previous day.Problem
When using
postgres.Config{PreferSimpleProtocol: true}
, thedatatypes.Date
would send the wrong date to the database. For example:The debug log would show the correct date, but the actual value inserted would be the previous day.
Root Cause
The issue was in the
Date.Value()
method, which preserved the original timezone of the input time:When PostgreSQL uses simple protocol mode, timezone-aware dates can be interpreted incorrectly, especially when the original time is in a timezone ahead of UTC.
Solution
Changed the
Date.Value()
method to always return dates in UTC timezone:This ensures that dates are timezone-independent, which is the correct behavior for date-only values.
Testing
Added comprehensive tests to verify:
Fixes #309.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.