New submission from Mario Corchero <marioc...@gmail.com>:

Currently, datetime.strptime does not support parsing utc offsets that include 
a colon. "+0000" is parsed without issues whilst it fails with "+00:00".

"+NN:NN" is not only ISO8601 valid but also the way the offset is presented to 
the user when using .isoformat on a datetime with a timezone/offset.

This lead to the users needing to go to external libraries like dateutil or 
iso8601 just to be able to parse the datetime encoded in strings that 
"datetime" produces.

Even if a long-term goal would be to provide a way to parse any isoformatted 
string this issue just aims to address the problem that the %z parsing 
presents. This already unblocks users from parsing datetime object serialized 
with isoformat.

With this change, the following will just work:

>>> import datetime as dt
>>> iso_fmt = '%Y-%m-%dT%H:%M:%S%z'
>>> d = dt.datetime.strptime('2004-01-01T10:10:10+05:00', iso_fmt)

*'2004-01-01T10:10:10+05:00' is a sample string generated via 
datetime.isoformat()

Other options like having a new %:z was proposed but having just %z seems much 
simpler for the user.



Note: There has been already conversations about adding support on datetime to 
parse any ISO-formatted string. This is a more simplistic approach. We might be 
able to get to that situation after this patch, but this aims just to unblock 
us.

Related:
http://www.loc.gov/standards/datetime/iso-tc154-wg5_n0039_iso_wd_8601-2_2016-02-16.pdf
https://mail.python.org/pipermail/python-ideas/2014-March/027018.html
https://bugs.python.org/issue15873

----------
components: Library (Lib)
messages: 304486
nosy: mariocj89
priority: normal
severity: normal
status: open
title: datetime.strptime: Support for parsing offsets with a colon
type: enhancement
versions: Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue31800>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to