[Python-Dev] Aware datetime from naive local time Was: Status on PEP-431 Timezones (original) (raw)

Alexander Belopolsky alexander.belopolsky at gmail.com
Fri Apr 10 04:21:35 CEST 2015


On Thu, Apr 9, 2015 at 4:51 PM, Isaac Schwabacher <ischwabacher at wisc.edu> wrote:

> > > Well, you are right, but at least we do have a localtime utility hidden in the email package: > > > > > > >>> from datetime import * > > > >>> from email.utils import localtime > > > >>> print(localtime(datetime.now())) > > > 2015-04-09 15:19:12.840000-04:00 > > > > > > You can read <http://bugs.python.org/issue9527> for the reasons it did not make into datetime. > > > > But that's restricted to the system time zone. Nothing good ever comes from the system time zone... > > Let's solve one problem at a time. ...

PEP 431 proposes to import zoneinfo into the stdlib, ...

I am changing the subject so that we can focus on one question without diverting to PEP-size issues that are better suited for python ideas.

I would like to add a functionality to the datetime module that would solve a seemingly simple problem: given a naive datetime instance assumed to be in local time, construct the corresponding aware datetime object with tzinfo set to an appropriate fixed offset datetime.timezone instance.

Python 3 has this functionality implemented in the email package since version 3.3, and it appears to work well even in the ambiguous hour

from email.utils import localtime from datetime import datetime localtime(datetime(2014,11,2,1,30)).strftime('%c %z %Z') 'Sun Nov 2 01:30:00 2014 -0400 EDT' localtime(datetime(2014,11,2,1,30), isdst=0).strftime('%c %z %Z') 'Sun Nov 2 01:30:00 2014 -0500 EST'

However, in a location with a more interesting history, you can get a situation tha -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20150409/644f1b63/attachment.html>



More information about the Python-Dev mailing list