Skip to content
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

Datetime serialization and deserialization fixed #1515

Merged
merged 14 commits into from
May 31, 2022
Merged
11 changes: 11 additions & 0 deletions docs/reference/kombu.utils.iso8601.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
==========================================================
JSON Utilities - ``kombu.utils.iso8601``
==========================================================

.. contents::
:local:
.. currentmodule:: kombu.utils.iso8601

.. automodule:: kombu.utils.iso8601
:members:
:undoc-members:
dobosevych marked this conversation as resolved.
Show resolved Hide resolved
76 changes: 76 additions & 0 deletions kombu/utils/iso8601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""Parse ISO8601 dates.

Originally taken from :pypi:`pyiso8601`
(https://bitbucket.org/micktwomey/pyiso8601)

Modified to match the behavior of ``dateutil.parser``:

- raise :exc:`ValueError` instead of ``ParseError``
- return naive :class:`~datetime.datetime` by default
- uses :class:`pytz.FixedOffset`

This is the original License:

Copyright (c) 2007 Michael Twomey

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sub-license, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import re
from datetime import datetime

from pytz import FixedOffset

__all__ = ('parse_iso8601',)

# Adapted from http://delete.me.uk/2005/03/iso8601.html
ISO8601_REGEX = re.compile(
r'(?P<year>[0-9]{4})(-(?P<month>[0-9]{1,2})(-(?P<day>[0-9]{1,2})'
r'((?P<separator>.)(?P<hour>[0-9]{2}):(?P<minute>[0-9]{2})'
r'(:(?P<second>[0-9]{2})(\.(?P<fraction>[0-9]+))?)?'
r'(?P<timezone>Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?'
)
TIMEZONE_REGEX = re.compile(
r'(?P<prefix>[+-])(?P<hours>[0-9]{2}).(?P<minutes>[0-9]{2})'
)


def parse_iso8601(datestring):
"""Parse and convert ISO-8601 string to datetime."""
m = ISO8601_REGEX.match(datestring)
if not m:
raise ValueError('unable to parse date string %r' % datestring)
groups = m.groupdict()
tz = groups['timezone']
if tz == 'Z':
tz = FixedOffset(0)
elif tz:
m = TIMEZONE_REGEX.match(tz)
prefix, hours, minutes = m.groups()
hours, minutes = int(hours), int(minutes)
if prefix == '-':
hours = -hours
minutes = -minutes
tz = FixedOffset(minutes + hours * 60)
return datetime(
int(groups['year']), int(groups['month']),
int(groups['day']), int(groups['hour'] or 0),
int(groups['minute'] or 0), int(groups['second'] or 0),
int(groups['fraction'] or 0), tz
)
dobosevych marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 5 additions & 1 deletion kombu/utils/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import json as stdjson
import uuid

from kombu.utils.iso8601 import parse_iso8601

try:
from django.utils.functional import Promise as DjangoPromise
except ImportError: # pragma: no cover
Expand Down Expand Up @@ -51,7 +53,7 @@ def default(self, o,
r = o.isoformat()
auvipy marked this conversation as resolved.
Show resolved Hide resolved
if r.endswith("+00:00"):
r = r[:-6] + "Z"
dobosevych marked this conversation as resolved.
Show resolved Hide resolved
return r
return {"datetime": r, "__datetime__": True}
elif isinstance(o, times):
return o.isoformat()
elif isinstance(o, textual):
Expand Down Expand Up @@ -80,6 +82,8 @@ def dumps(s, _dumps=json.dumps, cls=None, default_kwargs=None, **kwargs):

def object_hook(dct):
"""Hook function to perform custom deserialization."""
if "__datetime__" in dct:
return parse_iso8601(dct["datetime"])
dobosevych marked this conversation as resolved.
Show resolved Hide resolved
if "__bytes__" in dct:
return dct["bytes"].encode("utf-8")
if "__base64__" in dct:
Expand Down
7 changes: 3 additions & 4 deletions t/unit/utils/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@ class test_JSONEncoder:
def test_datetime(self):
now = datetime.utcnow()
now_utc = now.replace(tzinfo=pytz.utc)
stripped = datetime(*now.timetuple()[:3])
serialized = loads(dumps({
'datetime': now,
'tz': now_utc,
'date': now.date(),
'time': now.time()},
))
assert serialized == {
'datetime': now.isoformat(),
'tz': '{}Z'.format(now_utc.isoformat().split('+', 1)[0]),
'datetime': now,
'tz': now_utc,
'time': now.time().isoformat(),
'date': stripped.isoformat(),
'date': datetime(now.year, now.month, now.day, 0, 0, 0, 0),
}
dobosevych marked this conversation as resolved.
Show resolved Hide resolved

@given(message=st.binary())
Expand Down