MagicStack / asyncpg

A fast PostgreSQL Database Client Library for Python/asyncio.
Apache License 2.0
6.99k stars 404 forks source link

Database timezone ignored for timestamptz #1006

Open TheJJ opened 1 year ago

TheJJ commented 1 year ago

I would expect the datetime.datetime object has the same timezone as psql or python determines (CET).

Instead, asyncpg sets the timezone to UTC.

psql shell, displays UTC+1 = CET properly:

=> select now()::timestamptz;
 2023-02-19 01:53:28.453411+01

python-shell:

>>> loop = asyncio.new_event_loop()
>>> c = loop.run_until_complete(asyncpg.connect(database='lol'))
>>> loop.run_until_complete(c.fetch("select now()::timestamptz"))
[<Record now=datetime.datetime(2023, 2, 19, 0, 54, 39, 597496, tzinfo=datetime.timezone.utc)>]
# ^ here, i'd expect it to be with tzinfo=datetime.timezone(datetime.timedelta(seconds=3600), 'CET')

# because python does the same thing when having timezoned timestamps:
>>> datetime.datetime.now().astimezone()
datetime.datetime(2023, 2, 19, 1, 57, 12, 260030, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600), 'CET'))
adriangb commented 1 year ago

Dupe of #481 ?

TheJJ commented 1 year ago

I've submitted a fix at MagicStack/py-pgproto#21

TheJJ commented 1 year ago

can this please be reviewed?