[issue27741] datetime.datetime.strptime functionality description incorrect

Tal Einat report at bugs.python.org
Fri Oct 12 04:52:17 EDT 2018


Tal Einat <taleinat at gmail.com> added the comment:

At least with Python 3.7.0, the equivalence is not complete: datetime.strptime() is better, since it retains both microseconds and timezone data. See examples below.

>>> from datetime import datetime, timezone
>>> import time
>>> s = datetime.strftime(datetime.now(), '%a %b %d %H:%M:%S %f')
>>> s
'Fri Oct 12 11:33:32 999810'
>>> datetime.strptime(s, '%a %b %d %H:%M:%S %f')
datetime.datetime(1900, 10, 12, 11, 33, 32, 999810)
>>> datetime(*time.strptime(s, '%a %b %d %H:%M:%S %f')[:6])
datetime.datetime(1900, 10, 12, 11, 33, 32)
>>> s2 = datetime.strftime(datetime.now(timezone(timedelta(hours=1))), '%a %b %d %H:%M:%S %f%z')
>>> s2
'Fri Oct 12 09:48:40 347076+0100'
>>> datetime.strptime(s2, '%a %b %d %H:%M:%S %f%z')
datetime.datetime(1900, 10, 12, 9, 48, 40, 347076, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600)))
>>> datetime(*time.strptime(s2, '%a %b %d %H:%M:%S %f%z')[:6])
datetime.datetime(1900, 10, 12, 9, 48, 40)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue27741>
_______________________________________


More information about the Python-bugs-list mailing list