Skip to content
Snippets Groups Projects
Commit 312eaf47 authored by Art's avatar Art :lizard:
Browse files

Make datetime_to_str work on Python 3.5

parent e9ea313d
No related branches found
No related tags found
No related merge requests found
import sys
import json
import datetime
import collections
......@@ -21,6 +22,13 @@ def datetime_to_str(dt):
"""
if not dt.tzinfo:
logger.warning("Naive datetime received by serialize_datetime() and will be treated as local: {dt}. Avoid using naive datetime objects.".format(dt=dt))
if sys.version_info < (3, 3):
raise RuntimeError("Cannot consume naive datetime in python < 3.3")
elif sys.version_info < (3, 6):
local_tzinfo = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo
dt = dt.replace(tzinfo=local_tzinfo)
else:
pass # python 3.6+ allows .astimezone() on naive objects
utc_dt = dt.astimezone(datetime.timezone.utc)
return datetime.datetime.strftime(utc_dt, settings.DATETIME_FORMAT)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment