Skip to content

Instantly share code, notes, and snippets.

@danlindow
Created August 16, 2017 08:46
Show Gist options
  • Save danlindow/88d349dfd26b8b7d484f7ad8543f1b70 to your computer and use it in GitHub Desktop.
Save danlindow/88d349dfd26b8b7d484f7ad8543f1b70 to your computer and use it in GitHub Desktop.
>>> import six
>>> import datetime
>>> from dateutil.parser import parse
>>> now = datetime.datetime.utcnow()
>>> now
datetime.datetime(2017, 8, 16, 8, 38, 31, 766840)
>>> from dateutil.tz import tzutc
>>> test = now.replace(tzinfo=tzutc())
>>> test
datetime.datetime(2017, 8, 16, 8, 38, 31, 766840, tzinfo=tzutc())
>>> fmt = test.astimezone(tzutc()).strftime('%Y-%m-%dT%H:%M:%S.%f%z')
>>> fmt = six.u(fmt)
>>> fmt
u'2017-08-16T08:38:31.766840+0000'
>>> test2 = datetime.datetime.strptime(fmt, '%Y-%m-%dT%H:%M:%S.%f%z')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 317, in _strptime
(bad_directive, format))
ValueError: 'z' is a bad directive in format '%Y-%m-%dT%H:%M:%S.%f%z'
>>> test2 = parse(fmt)
>>> test2
datetime.datetime(2017, 8, 16, 8, 38, 31, 766840, tzinfo=tzutc())
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment