carl at oddbird.net
Thu Aug 27 21:10:45 CEST 2015
On 08/27/2015 12:59 PM, Alexander Belopolsky wrote:
> Dealing with simple time strings like this should really be one step:
>>>> ts = '2004-10-31 01:15 EST-05:00'
>>>> dt = datetime.strptime(ts.replace('EST', '').replace(':', ''),
> '%Y-%m-%d %H%M %z')
> 2004-10-31 01:15:00-05:00
> It is unfortunate that we need to massage the input like this before
> passing it to datetime.strptime(). Ideally, datetime.strptime() should
> have a way to at least parse -05:00 as a fixed offset timezone.
> However, this problem has nothing to do with PEP 495. Once you have the
> UTC offset - there is no ambiguity. The other 01:15 would be
> "2004-10-31 01:15 -04:00." The EST part is redundant and is dropped
> explicitly in my solution and not used in the solutions by Tim and Guido.
I don't think that's true; it's not entirely ignored in Tim and Guido's
solution, and your solution gives subtly different results. A datetime
with a fixed-offset -0500 tzinfo and a datetime with a tzinfo that knows
it is "US/Eastern" may represent the same instant, but they are
semantically different. That difference could reveal itself after some
arithmetic with the datetime (because in one case the tzinfo's utcoffset
might change after the arithmetic, and in the other case it wouldn't).
(Of course if it's a pytz timezone the offset wouldn't change until
after a `normalize()`, but that's not necessarily true of any tzinfo
Guido and Tim don't provide code to parse "EST-05:00" to "US/Eastern",
but their solutions both rely on assuming that knowledge. Tim said so
explicitly: 'where you knew "EST" meant US/Eastern'
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 819 bytes
Desc: OpenPGP digital signature
More information about the Datetime-SIG