I am trying to dateutil.parser.parse()
to parse the default str(datetime.datetime.now())
output using timezone-aware datetimes. However, parse()
seems to lose the timezone information and replace it with the local time timezone. Below is the IPython output:
In [1]: from django.utils.timezone import now
In [3]: import dateutil
In [4]: t = now()
In [6]: print t
2014-07-14 08:51:49.123342+00:00
In [7]: st = unicode(t)
In [8]: print dateutil.parser.parse(st)
2014-07-14 08:51:49.123342+02:00
As far I understood dateutil
does some heurestics when guessing the date format and it might go wrong here.
How to give exact datetime format for parsing timezone-aware datetimes?
Even better - if the format is known how to parse this datetime using only Python stdlib, without dateutil dependency?
timezone.now
will give you an aware datetime object if USE_TZ
is true. You don't need to parse it further.
In this example, USE_TZ is True, and the timezone is set to UTC:
>>> from django.utils import timezone as djtz
>>> i = djtz.now()
>>> type(i)
<type 'datetime.datetime'>
>>> i.tzinfo
<UTC>
As far as dateutil goes, it will parse your string correctly:
>>> from dateutil.parser import parse
>>> s = '2014-07-14 08:51:49.123342+00:00'
>>> parse(s).tzinfo
tzutc()
>>> z = parse(s)
>>> z
datetime.datetime(2014, 7, 14, 8, 51, 49, 123342, tzinfo=tzutc())
You can see its picking up the correct timezone (UTC in this case).
The default format specifiers only accept +0000 as the offset format with %z
, or the three letter timezone name with %Z
; but you cannot use this to parse, only to format:
>>> datetime.datetime.strptime('2014-07-14 08:51:49.123342+0000', '%Y-%m-%d %H:%M:%S.%f%z')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/_strptime.py", line 317, in _strptime
(bad_directive, format))
ValueError: 'z' is a bad directive in format '%Y-%m-%d %H:%M:%S.%f%z'
>>> datetime.datetime.strftime(z, '%Z')
'UTC'
>>> datetime.datetime.strftime(z, '%z')
'+0000'