-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
sqlite3 timestamp adapter chokes on timezones #63265
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
Comments
If you use detect_types=sqlite3.PARSE_DECLTYPES with sqlite3 and insert a timezone-aware datetime instance, you will get a ValueError if you attempt to read it back out: File "/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/sqlite3/dbapi2.py", line 68, in convert_timestamp Although this immediately gets into the thorny stdlib timezone support situation, it's extremely annoying to have the out-of-the-box module break round-tripping data and it looks like support for simple UTC offsets isn't too horrible – something like https://gist.github.com/acdha/6655391 works in very limited testing: def tz_aware_timestamp_adapter(val):
datepart, timepart = val.split(b" ")
year, month, day = map(int, datepart.split(b"-"))
if b"+" in timepart:
timepart, tz_offset = timepart.rsplit(b"+", 1)
if tz_offset == b'00:00':
tzinfo = datetime.timezone.utc
else:
hours, minutes = map(int, tz_offset.split(b':', 1))
tzinfo = datetime.timezone(datetime.timedelta(hours=hours, minutes=minutes))
else:
tzinfo = None
timepart_full = timepart.split(b".")
hours, minutes, seconds = map(int, timepart_full[0].split(b":"))
if len(timepart_full) == 2:
microseconds = int('{:0<6.6}'.format(timepart_full[1].decode()))
else:
microseconds = 0
val = datetime.datetime(year, month, day, hours, minutes, seconds, microseconds, tzinfo)
return val
sqlite3.register_converter('timestamp', tz_aware_timestamp_adapter) |
This would be an useful improvement. Do you want to post a patch? See guidelines at http://docs.python.org/devguide/ |
Sounds like a reasonable request, but the proposed code does not seem to work for the Eastern hemisphere (negative tz offsets.) I am not very familiar with sqlite module. What timestamp format does it use? Isn't it some varian of ISO 3339? See bpo-15873. |
Added patch to add timezone support for sqlite3 datetime adapter. |
I just hit the problem in Python 2.7. Question 1: when will the patch be applied in Python 3? |
I'm -1 on adding timezone to the adapters. |
Can you expand on why you are -1, Gerhard? |
I'm -1 because I believe that ultimately, adapters and converters were a mistake to add to pysqlite. That's why I deprecated them in pysqlite 2.8.0. Do you know what would be the correct step to propose a deprecation in the sqlite3 module of Python proper? Is a PEP necessary? |
Just open an issue to propose the deprecation, if they aren't part of the DBAPI. We wouldn't actually remove them, though, until after 2.7 goes out of maintenance (if then...we still haven't quite decided what we're going to do about removals post 2.7). |
I've opened bpo-26651 (patch included) to deprecate them. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: