MagicStack / asyncpg

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

negative interval encoding issues due to python's timedelta implementation details #1114

Open takeda opened 10 months ago

takeda commented 10 months ago

Python's timedelta implementation only allows to store negative number of days, hours, minutes, seconds, microseconds are always positive values. So when a negative value smaller than a day is interpreted it is stored in a strange way, example from Python's documentation:

Note that normalization of negative values may be surprising at first. For example:

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)

https://docs.python.org/3/library/datetime.html?highlight=timedelta#datetime.timedelta

In my particular case when I'm trying to store -3 hours, that's getting translated in python to:

datetime.timedelta(days=-1, seconds=75600)

And ends up stored in postgresql as: "0 years 0 mons -1 days 21 hours 0 mins 0.0 secs"

Is there also a workaround where I can skip timedelta for interval?

elprans commented 10 months ago

Is there also a workaround where I can skip timedelta for interval?

You can set up a custom codec. See an example here