Skip to content
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

datetime: convert timestamptz to local timezone #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions codecs/datetime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ cdef timestamptz_encode(CodecContext settings, WriteBuffer buf, obj):
buf.write_int64(pg_time64_negative_infinity)
return

# convert to utc and get time since epoch
utc_dt = obj.astimezone(utc)

delta = utc_dt - pg_epoch_datetime_utc
cdef:
int64_t seconds = cpython.PyLong_AsLongLong(delta.days) * 86400 + \
Expand All @@ -244,8 +244,10 @@ cdef timestamptz_decode(CodecContext settings, FRBuffer *buf):
# negative infinity
return negative_infinity_datetime
else:
return pg_epoch_datetime_utc.__add__(
dt = pg_epoch_datetime_utc.__add__(
timedelta(0, seconds, microseconds))
# convert to current system timezone
return dt.astimezone()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is almost certainly not what you want. astimezone() creates a broken fixed-offset timezone object that does not do DST. This exists because python did not use to have a proper timezones implementation, but it has zoneinfo now. A better way would be to have a timezone object in settings, defaulting to UTC, and do astimezone(settings.timezone) here.



cdef time_encode(CodecContext settings, WriteBuffer buf, obj):
Expand Down