Skip to content

Got an unexpected number of seconds in datetime from asyncpg #363

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No 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? No Sign in to your account

Closed
rossnomann opened this issue Sep 18, 2018 · 0 comments
Closed

Got an unexpected number of seconds in datetime from asyncpg #363

rossnomann opened this issue Sep 18, 2018 · 0 comments

Comments

@rossnomann
Copy link

  • asyncpg version: 0.17.0
  • PostgreSQL version: 10.3
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    : reproduced with a local install
  • Python version: 3.6.5
  • Platform: Linux x86_64
  • Do you use pgbouncer?: no
  • Did you install asyncpg with pip?: yes
from datetime import datetime

import asyncio
import asyncpg


async def main():
    pool = await asyncpg.create_pool(
        database='postgres',
        user='postgres',
        host='127.0.0.1',
        password='1234',
        port=2000
    )
    async with pool.acquire() as connection:
        dt_in = datetime(1970, 1, 1, 20, 31, 23, 648000)
        dt_out = await connection.fetchval("SELECT '%s'::timestamp" % dt_in)
        op = '==' if dt_in == dt_out else '!='
        print('%s %s %s' % (dt_in, op, dt_out))


loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Output:

1970-01-01 20:31:23.648000 != 1970-01-01 20:31:24.648000

But in psql all works fine:

postgres=# select '1970-01-01 20:31:23.648000'::timestamp;
        timestamp        
-------------------------
 1970-01-01 20:31:23.648
(1 row)

I guess there is a problem with time parsing in asyncpg.
Maybe seconds were rounded somewhere, like this:

>>> d
datetime.datetime(1970, 1, 1, 20, 31, 23, 648000)
>>> d.timestamp()
63083.648
>>> int(d.timestamp()) % 60
23
>>> round(d.timestamp()) % 60
24
elprans added a commit that referenced this issue Sep 18, 2018
The datetime decoder currently incorrectly uses an unsigned integer type
for microseconds, which leads to incorrect decoding of timestamps with
fractional seconds which represent dates before Jan 1 2000.  Same issue
affects negative intervals with fractional seconds.

Fixes: #363
No Sign up for free to join this conversation on GitHub. Already have an account? No Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant