kombu icon indicating copy to clipboard operation
kombu copied to clipboard

JSON serialize timezone aware datetimes with offset

Open okomarov opened this issue 5 years ago • 2 comments

PROBLEM

The json encoding of a timezone aware datetime is not parseable by datetime.fromisoformat().

SOLUTION

Please keep the canonical offset +00:00 in the date instead of removing it in https://github.com/celery/kombu/blob/e9e1edf3966803b06a75480c372992a005b64100/kombu/utils/json.py#L52

EXAMPLE

The following snippet produces the following datestr 2019-02-01T20:52:56.466990Z:

from datetime import datetime
from datetime import timezone
dt = datetime(2019, 2, 1, 20, 52, 56, 466990, tzinfo=timezone.utc)
from kombu.utils import json as _json
_json.dumps(dt)

This format is not supported by python's datetetime.fromisoformat():

>>> datetime.fromisoformat('2019-02-01T20:52:56.466990Z')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Invalid isoformat string: '2019-02-01T20:52:56.466990Z'

The isodate with +offset is parsed correctly:

>>> datetime.fromisoformat('2019-02-01T20:52:56.466990+00:00')
datetime.datetime(2019, 2, 1, 20, 52, 56, 466990, tzinfo=datetime.timezone.utc)

okomarov avatar Mar 19 '19 11:03 okomarov

I'm not sure of your use-case, but Celery includes a function to parse dates serialized by kombu: celery.utils.iso8601.parse_iso8601.

clokep avatar Mar 19 '19 12:03 clokep

Fair enough was not aware.

Still, I feel like those 2 lines that drop the '+00:00' are unnecessary:

  • datetime.fromisoformat() is core python and more discoverable than celery.utils.iso8601.parse_iso8601
  • if space is an issue, compression is the way to go than introducing a special case for UTC timezone

okomarov avatar Mar 19 '19 12:03 okomarov