Database timezone ignored for timestamptz

Open
#1,006 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
postgresql, python
Domain
databases

Research direction

Start by reproducing the reported PostgreSQL 15.1 timestamptz behavior with asyncpg 0.27.0 and Python 3.10.9 on Linux, comparing psql output with the returned datetime. Trace the timestamp decoding path and identify the existing tests covering timestamptz values. Done means the returned datetime preserves the expected database-local timezone behavior and the regression is covered by a test.

Written by the indexing model from the issue text.

Description

  • asyncpg version: 0.27.0
  • PostgreSQL version: 15.1
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    : local
  • Python version: 3.10.9
  • Platform: Linux
  • Do you use pgbouncer?: no
  • Did you install asyncpg with pip?: no
  • If you built asyncpg locally, which version of Cython did you use?: 0.29.33
  • Can the issue be reproduced under both asyncio and
    uvloop?
    : not tested, since unlikely

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'))
Dominant language
Python
Stars
8.1k
Forks
469
Avg merge
2d 20h
Merged PRs (30d)
9

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from MagicStack/asyncpg

All issues in MagicStack/asyncpg

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.