Repository navigation
Add aware local time support to datetime module #53736
Copy link
Copy link
Closed
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Description
| BPO | 9527 |
|---|---|
| Nosy | @tim-one, @loewis, @warsaw, @jribbens, @rhettinger, @jcea, @amauryfa, @mdickinson, @ncoghlan, @abalkin, @pitrou, @catlee, @vstinner, @cameron-simpson, @merwok, @bitdancer |
| Dependencies | |
| Files |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = 'https://github.com/abalkin'
closed_at = <Date 2012-06-22.17:37:39.663>
created_at = <Date 2010-08-06.03:48:18.582>
labels = ['extension-modules', 'type-feature', 'library']
title = 'Add aware local time support to datetime module'
updated_at = <Date 2012-06-22.20:09:58.207>
user = 'https://github.com/abalkin'bugs.python.org fields:
activity = <Date 2012-06-22.20:09:58.207>
actor = 'python-dev'
assignee = 'belopolsky'
closed = True
closed_date = <Date 2012-06-22.17:37:39.663>
closer = 'belopolsky'
components = ['Extension Modules', 'Library (Lib)']
creation = <Date 2010-08-06.03:48:18.582>
creator = 'belopolsky'
dependencies = ['1647654', '1667546']
files = ['18410', '20410', '25939', '25940', '26062']
hgrepos = []
issue_num = 9527
keywords = ['patch']
message_count = 21.0
messages = ['113067', '113069', '118675', '126292', '126295', '127516', '127517', '135227', '153928', '161643', '162631', '162658', '162660', '163136', '163137', '163297', '163429', '163442', '163465', '163467', '163476']
nosy_count = 29.0
nosy_names = ['tim.peters', 'loewis', 'barry', 'jribbens', 'rhettinger', 'jcea', 'pboddie', 'jamesh', 'guettli', 'amaury.forgeotdarc', 'mark.dickinson', 'ncoghlan', 'davidfraser', 'belopolsky', 'pitrou', 'andersjm', 'catlee', 'vstinner', 'techtonik', 'tomster', 'werneck', 'hodgestar', 'Neil Muller', 'cameron', 'eric.araujo', 'erik.stephens', 'steve.roberts', 'r.david.murray', 'python-dev']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue9527'
versions = ['Python 3.2']Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Activity
abalkin commentedon Aug 6, 2010
See python-dev post for motivation.
http://mail.python.org/pipermail/python-dev/2010-August/102842.html
I am attaching a patch implementing the proposed method in datetime.py. I will also paste the code below. Note that this is only prototype. Real implementation will use tm_zone and tm_gmtoff components of tm structure on platforms that supply them.
abalkin commentedon Aug 6, 2010
Merging nosy lists from issues bpo-1647654 and bpo-2736. If datetime.localtime() is implemented, I argue that the features requested in these two issues will become unnecessary.
abalkin commentedon Oct 14, 2010
Would anyone like to review this? The Rietveld link works (thanks Martin!) and I would like to get some feedback on the python version before I invest effort into coding this in C.
abalkin commentedon Jan 14, 2011
Following Anatoly's review, I renamed datetime argument and a local variable, added comments and expanded docstring. I am uploading a new patch: datetime-localtime-proto-1.diff.
Martin, I could not figure out how to add the new patch to rietveld and I don't think auto-review feature works.
abalkin commentedon Jan 14, 2011
Forwarding Rietveld conversation to the tracker. It looks like
Rietveld integration has a bug and sends reviews to
None@psf.upfronthosting.co.za rather than to report@bugs.python.org.
Forwarded conversation
Subject: Add aware local time support to datetime module (bpo-9527)
------------------------
From: <techtonik@gmail.com>
Date: Thu, Jan 13, 2011 at 7:14 PM
To: belopolsky@users.sourceforge.net
Cc: None@psf.upfronthosting.co.za
I'm not proficient in C and not an expert in datetime issues either, but
because nobody else stepped in to do review, I've left some comments.
Perhaps making the issue more acceptable by general public will help to
close it faster.
http://bugs.python.org/review/9527/diff/1568/2704
File Lib/datetime.py (right):
http://bugs.python.org/review/9527/diff/1568/2704#newcode1420
Lib/datetime.py:1420: def localtime(cls, t=None, isdst=-1):
The t param should probably be called 'time'.
http://bugs.python.org/review/9527/diff/1568/2704#newcode1421
Lib/datetime.py:1421: """Return local time as an aware datetime object.
ISTM that we need an object hierarchy like DateTime->DateTimeTZ, because
debugging which object is TZ aware and which is not is rather hard.
http://bugs.python.org/review/9527/diff/1568/2704#newcode1435
Lib/datetime.py:1435: t = _time.time()
Here we should always receive aware object, but I couldn't find
reference to C standard to ensure that all systems return this
correctly.
http://bugs.python.org/review/9527/diff/1568/2704#newcode1437
Lib/datetime.py:1437: if t.tzinfo is None:
I'd replace this with elif and place comment that here we detect
non-aware time object. Docstring above is nice, but it will probably be
parsed into documentation, and I am not sure if these details should be
present in user manual.
http://bugs.python.org/review/9527/diff/1568/2704#newcode1438
Lib/datetime.py:1438: tm = t.timetuple()[:-1] + (isdst,)
IIUC return result of time() value is time.struct_time which doesn't
have timetuple() method. If you work with datetime objects only, then
you need to name variables accordingly.
http://bugs.python.org/review/9527/show
----------
From: <techtonik@gmail.com>
Date: Fri, Jan 14, 2011 at 12:28 PM
To: belopolsky@users.sourceforge.net
Cc: None@psf.upfronthosting.co.za
On 2011/01/14 04:30:11, sasha wrote:
I prefer to threat UTC time and timezone separately. My API wannabe:
global.time() - returns value that can be printed and it will be in UTC
global.offset() - difference in seconds between current timezone and UTC
(so that +2 will be 7200 seconds and -2 is -7200)
global.fromiso() - returns value parsed from ISO 8601 format, probably
pair (UTC time, offset)
global.toiso(time, [offset]) - for symmetry
Maybe its even better if global.time() returns tuple (UTC time, offset)
As you may see I am not interested in DST details etc. All I need is the
ability to parse and generate timestamps. If your datetime.localtime()
returns datetime object, how should I use it to generate Atom timestamp
with proper TZ modifier?
http://www.atomenabled.org/developers/syndication/atom-format-spec.php#date.constructs
In my low-level and non-OOP API it is:
global.toiso(global.time(), global.offset())
epoch as an
At first I thought about datetime.toaware(dt), but this description of
yours is better than the one provided in docstring. If
datetime.localtime([dt]) gets local time in timezone aware object or
converts existing datetime (how about other types?), then the name
datetime.localtime() seems fine. But I would consider alternative
datetime.globaltime() name with the meaning that we get datetime object
that is globally synchronized, in other words it can be used not only
locally. .localtime() is ambiguous in this respect.
On 2011/01/14 04:30:11, sasha wrote:
dt is a consistent name for datetime parameters in Python manual.
On 2011/01/14 04:30:11, sasha wrote:
It must be recorded for history if we want to get rid of date/time curse
in Python4 without missing any details. Removing naive objects in favor
of normalized objects with UTC timestamps seems like a way to go.
On 2011/01/14 04:30:11, sasha wrote:
But these seconds are in UTC. There is no TZ correction.
[-]Add aware local time support to datetime module[/-][+]Add aware local time support to datetime module (issue9527)[/+][-]Add aware local time support to datetime module (issue9527)[/-][+]Add aware local time support to datetime module[/+]rhettinger commentedon Jan 30, 2011
I think Tim and Guido had deliberately avoided local timezone awareness when they designed the module.
Also, I question whether the proposed API is correct. ISTM that any code that sets the *dst* parameter is guaranteed to be wrong (hardwiring-in a value that will change every few months).
Have you studied the solutions used in other libraries and other languages? This path has been well-traveled, so original work may be less helpful than just researching existing solutions.
abalkin commentedon Jan 30, 2011
On Sat, Jan 29, 2011 at 11:56 PM, Raymond Hettinger
<report@bugs.python.org> wrote:
..
Can you elaborate on this? ISTM that your argument would equally
apply to C/POSIX mktime() API. It won't be the first time C/POSIX got
time handling wrong, but I doubt it is the case in this instance.
bitdancer commentedon May 5, 2011
LocalTimezone support would be *really* helpful for the email module. It would allow us to have unambiguous semantics for datetime objects representing timestamps exacted from or inserted into email messages (see bpo-665194 for recent discussion). The email module is already trying to handle timestamp translation, and I'd be willing to bet it is buggier than the proposal here.
At one point I even started to copy the LocalTimezone class from the docs into the email module. I implemented a naive extension of the current formatdate function instead, but after Alexander's feedback on bpo-665194 I think the naive implementation is not a good idea.
ncoghlan commentedon Feb 22, 2012
One important thing to remember is that once a time is in the past, whether or not DST was in effect for that time *is never going to change*. That means converting a DST aware timezone to a fixed offset timezone will work just fine for historical times.
It's mainly applications that need to handle times in the *future* (where the DST dates are still subject to change) that have to be aware of the problems when trying to handle DST with fixed offset timezone objects.
I think it's sufficient if the documentation pushes developers of such applications towards modules like pytz and dateutil to get access to variable offset tzinfo implementations.
python-dev commentedon May 26, 2012
New changeset df12ce0c96eb by R David Murray in branch 'default':
bpo-665194: Add a localtime function to email.utils.
http://hg.python.org/cpython/rev/df12ce0c96eb
abalkin commentedon Jun 11, 2012
This was originally posted on python-dev, but I hope reposting it here will make this issue easier to navigate.
"""
With addition of fixed offset timezone class and the timezone.utc
instance [0], it is easy to get UTC time as an aware datetime
instance:
However, if you want to keep time in your local timezone, getting an
aware datetime is almost a catch 22. If you know your timezone UTC
offset, you can do
but the problem is that there is no obvious or even correct way to
find local timezone UTC offset. [1]
In a comment on issue bpo-5094 ("datetime lacks concrete tzinfo
implementation for UTC"), I proposed to address this problem in a
localtime([t]) function that would return current time (or time
corresponding to the optional datetime argument) as an aware datetime
object carrying local timezone information in a tzinfo set to an
appropriate timezone instance. This solution is attractive by its
simplicity, but there are several problems:
An aware datetime cannot carry all information that system
localtime() supplies in a time tuple. Specifically, the is_dst flag
is lost. This is not a problem for most applications as long as
timezone UTC offset and timezone name are available, but may be an
issue when interoperability with the time module is required.
Datetime's tzinfo interface was designed with the idea that
<2010-11-06 12:00 EDT> + <1 day> = <2010-11-07 12:00 EST>, not
<2010-11-07 12:00 EDT>. It other words, if I have lunch with someone
at noon (12:00 EDT) on Saturday the day before first Sunday in
November, and want to meet again "at the same time tomorrow", I mean
12:00 EST, not 24 hours later. With localtime() returning datetime
with tzinfo set to fixed offset timezone, however, localtime() +
timedelta(1) will mean exactly 24 hours later and the result will be
expressed in an unusual for the given location timezone.
An alternative approach is the one recommended in the python manual.
[3] One could implement a LocalTimezone class with utcoffset(),
tzname() and dst() extracting information from system mktime and
localtime calls. This approach has its own shortcomings:
While adding integral number of days to datetimes in business
setting, it is natural to expect automatic timezone adjustments, it is
not as clearcut when adding hours or minutes.
The tzinfo.utcoffset() interface that expects *standard* local time
as an argument is confusing to many users. Even the "official"
example in the python manual gets it wrong. [4]
datetime(..., tzinfo=LocalTimezone()) is ambiguous during the
"repeated hour" when local clock is set back in DST to standard time
transition.
As far as I can tell, the only way to resolve the last problem is to
add is_dst flag to the datetime object, which would also be the
only way to achieve full interoperability between datetime objects and
time tuples. [5]
The traditional answer to a call for improvement of timezone support in
datetime module has been: "this is up to 3rd parties to implement."
Unfortunately, stdlib is asking 3rd parties to implement an impossible
interface without giving access to the necessary data. The
impossibility comes from the requirement that dst() method should find
out whether local time represents DST or standard time while there is
an hour each year when the same local time can be either. The missing
data is the system UTC offset when it changes historically. The time
module only gives access to the current UTC offset.
My preference is to implement the first alternative - localtime([t])
returning aware datetime with fixed offset timezone. This will solve
the problem of python's lack of access to the universally available
system facilities that are necessary to implement any kind of aware
local time support.
[0] http://docs.python.org/dev/library/datetime.html#timezone-objects
[1] http://bugs.python.org/issue1647654
[2] http://bugs.python.org/issue5094#msg106997
[3] http://docs.python.org/library/datetime.html#tzinfo-objects
[4] http://bugs.python.org/issue9063
[5] http://bugs.python.org/issue9004
"""
-- http://mail.python.org/pipermail/python-dev/2010-August/102842.html
abalkin commentedon Jun 12, 2012
Two objections have been raised to the proposed datetime.localtime() function:
It offers the third subtly different way to obtain current time in datetime module. The first two being provided by datetime.now() and datetime.today().
C library localtime function takes POSIX timestamp as an argument, so datetime.localtime() should follow suit.
I attach a prototype patch for a different approach: make datetime.astimezone() method supply local timezone information if no argument is given.
This patch also demonstrates that extracting all TZ information that platform may have knowledge of is not trivial.
jamesh commentedon Jun 12, 2012
One problem I can see with using a fixed offset tzinfo for localtime is that it might confuse people when doing date arithmetic. For example:
While it will give a correct answer as a point in time it will have the wrong time zone offset if run just before a daylight saving transition, which could be just as confusing.
I'm not sure how you'd solve this without e.g. importing pytz into the standard library though.
abalkin commentedon Jun 19, 2012
On Mon, Jun 11, 2012 at 11:18 PM, James Henstridge <report@bugs.python.org> wrote:
Yes, this is the issue that I discussed in my first python-ideas/python-dev post. (See msg162631 above.)
I think my latest proposal will fare slightly better in this scenario. I am now propose implementing local timezone info discovery in datetime.astimezone() method. Obtaining local time will now be a little more verbose:
but (local_time + timedelta(7)).astimezone() will give you correctly adjusted aware datetime seven days later.
If we also implement astimezone() for naive instances, we can even support naive local time arithmetics: (datetime.now() + timedelta(7)).astimezone() will produce the same time of day as "now" unless it falls into a non-existing hour.
Importing pytz will not help. Pytz fudges the issue by extending tzinfo methods to accept isdst flag, but there is no place in datetime to store this flag. Doing time calculations in local time is inherently ambiguous in presence of DST. On the other hand, producing tz aware local time from any unambiguous absolute time specification (UTC, time_t, local time + tz offset, etc.) is a well-defined problem which does not have an adequate solution.
abalkin commentedon Jun 19, 2012
I meant to say "does not have an adequate solution *in the current datetime module*". I think the enhanced datetime.astimezone() method will solve this problem.
abalkin commentedon Jun 20, 2012
Attached patch implements astimezone() default in both Python and C.
python-dev commentedon Jun 22, 2012
New changeset 88a5f2730579 by Alexander Belopolsky in branch 'default':
Issue bpo-9527: datetime.astimezone() method will now supply a class
http://hg.python.org/cpython/rev/88a5f2730579
New changeset 336c53c1f547 by Alexander Belopolsky in branch 'default':
Issue bpo-9527: datetime.astimezone() method will now supply a class
http://hg.python.org/cpython/rev/336c53c1f547
python-dev commentedon Jun 22, 2012
New changeset a7237f157625 by Alexander Belopolsky in branch 'default':
Issue bpo-9527: Fixes for platforms without tm_zone
http://hg.python.org/cpython/rev/a7237f157625
jcea commentedon Jun 22, 2012
This patch breaks OpenIndiana buildbots. For instance
http://www.python.org/dev/buildbot/all/builders/AMD64%20OpenIndiana%203.x/builds/3810/steps/test/logs/stdio
"""
FAIL: test_astimezone_default_eastern (test.datetimetester.TestDateTimeTZ_Pure)
----------------------------------------------------------------------
abalkin commentedon Jun 22, 2012
Working on this. It turns out tm_gmtoff uses the opposite sign to
that of timezone in time.h. For more confusion, consider this:
I am rechecking all UTC offset signs.
On Fri, Jun 22, 2012 at 3:36 PM, Jesús Cea Avión <report@bugs.python.org> wrote:
> Traceback (most recent call last): > File "/export/home/buildbot/64bits/3.x.cea-indiana-amd64/build/Lib/test/support.py", line 1139, in inner > return func(*args, **kwds) > File "/export/home/buildbot/64bits/3.x.cea-indiana-amd64/build/Lib/test/datetimetester.py", line 3286, in test_astimezone_default_eastern > self.assertEqual(local.strftime("%z %Z"), "+0500 EST") > AssertionError: '-0500 EST' != '+0500 EST' > - -0500 EST > ? ^ > + +0500 EST > ? ^ > """ > >python-dev commentedon Jun 22, 2012
New changeset 0f0e3ec22fce by Alexander Belopolsky in branch 'default':
Issue bpo-9527: tm_gmtoff has 'correct' sign.
http://hg.python.org/cpython/rev/0f0e3ec22fce