Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue9527] Add aware local time support to datetime module

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

May 25, 2012, 8:23 PM

Post #1 of 17 (252 views)
Permalink
[issue9527] Add aware local time support to datetime module

Roundup Robot <devnull [at] psf> added the comment:

New changeset df12ce0c96eb by R David Murray in branch 'default':
#665194: Add a localtime function to email.utils.
http://hg.python.org/cpython/rev/df12ce0c96eb

----------
nosy: +python-dev

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue9527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jun 5, 2012, 12:39 PM

Post #2 of 17 (237 views)
Permalink
[issue9527] Add aware local time support to datetime module [In reply to]

Changes by Barry A. Warsaw <barry [at] python>:


----------
nosy: +barry

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue9527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jun 5, 2012, 4:43 PM

Post #3 of 17 (236 views)
Permalink
[issue9527] Add aware local time support to datetime module [In reply to]

Changes by Cameron Simpson <cs [at] zip>:


----------
nosy: +cameron

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue9527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jun 11, 2012, 10:44 AM

Post #4 of 17 (230 views)
Permalink
[issue9527] Add aware local time support to datetime module [In reply to]

Alexander Belopolsky <alexander.belopolsky [at] gmail> added the comment:

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:

>>> datetime.now(timezone.utc)
datetime.datetime(2010, 8, 3, 14, 16, 10, 670308, tzinfo=datetime.timezone.utc)

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

>>> EDT = timezone(timedelta(hours=-4))
>>> datetime.now(EDT)
datetime.datetime(2010, 8, 3, 10, 20, 23, 769537,
tzinfo=datetime.timezone(datetime.timedelta(-1, 72000)))

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 #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:

1. 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.

2. 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:

1. 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.

2. 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]

3. 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

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue9527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jun 11, 2012, 7:27 PM

Post #5 of 17 (232 views)
Permalink
[issue9527] Add aware local time support to datetime module [In reply to]

Alexander Belopolsky <alexander.belopolsky [at] gmail> added the comment:

Two objections have been raised to the proposed datetime.localtime() function:

1. 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().

2. 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.

----------
Added file: http://bugs.python.org/file25939/datetime-astimezone-proto.diff

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue9527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jun 11, 2012, 7:28 PM

Post #6 of 17 (236 views)
Permalink
[issue9527] Add aware local time support to datetime module [In reply to]

Changes by Alexander Belopolsky <alexander.belopolsky [at] gmail>:


Added file: http://bugs.python.org/file25940/testtz.py

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue9527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jun 11, 2012, 8:18 PM

Post #7 of 17 (233 views)
Permalink
[issue9527] Add aware local time support to datetime module [In reply to]

James Henstridge <james [at] jamesh> added the comment:

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:

>>> d = datetime.localtime() + timedelta(days=7)

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.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue9527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jun 18, 2012, 8:18 PM

Post #8 of 17 (232 views)
Permalink
[issue9527] Add aware local time support to datetime module [In reply to]

Alexander Belopolsky <alexander.belopolsky [at] gmail> added the comment:

On Mon, Jun 11, 2012 at 11:18 PM, James Henstridge <report [at] bugs> wrote:
> One problem I can see with using a fixed offset
> tzinfo for localtime is that it might confuse people
> when doing date arithmetic.

Yes, this is the issue that I discussed in my first python-ideas/python-dev post. (See msg162631 above.)

>    >>> d = datetime.localtime() + timedelta(days=7)
>
> 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 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:

local_time = datetime.now(timezone.utc).astimezone()

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.

> I'm not sure how you'd solve this without e.g. importing
> pytz into the standard library though.

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.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue9527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jun 18, 2012, 8:22 PM

Post #9 of 17 (230 views)
Permalink
[issue9527] Add aware local time support to datetime module [In reply to]

Alexander Belopolsky <alexander.belopolsky [at] gmail> added the comment:

> ... is a well-defined problem which does not have an adequate solution.

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.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue9527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jun 20, 2012, 2:41 PM

Post #10 of 17 (229 views)
Permalink
[issue9527] Add aware local time support to datetime module [In reply to]

Alexander Belopolsky <alexander.belopolsky [at] gmail> added the comment:

Attached patch implements astimezone() default in both Python and C.

----------
stage: -> patch review
Added file: http://bugs.python.org/file26062/issue9527.diff

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue9527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jun 22, 2012, 8:58 AM

Post #11 of 17 (229 views)
Permalink
[issue9527] Add aware local time support to datetime module [In reply to]

Changes by Alexander Belopolsky <alexander.belopolsky [at] gmail>:


----------
stage: patch review -> commit review

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue9527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jun 22, 2012, 9:26 AM

Post #12 of 17 (229 views)
Permalink
[issue9527] Add aware local time support to datetime module [In reply to]

Roundup Robot <devnull [at] psf> added the comment:

New changeset 88a5f2730579 by Alexander Belopolsky in branch 'default':
Issue #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 #9527: datetime.astimezone() method will now supply a class
http://hg.python.org/cpython/rev/336c53c1f547

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue9527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jun 22, 2012, 10:27 AM

Post #13 of 17 (229 views)
Permalink
[issue9527] Add aware local time support to datetime module [In reply to]

Roundup Robot <devnull [at] psf> added the comment:

New changeset a7237f157625 by Alexander Belopolsky in branch 'default':
Issue #9527: Fixes for platforms without tm_zone
http://hg.python.org/cpython/rev/a7237f157625

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue9527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jun 22, 2012, 10:37 AM

Post #14 of 17 (229 views)
Permalink
[issue9527] Add aware local time support to datetime module [In reply to]

Changes by Alexander Belopolsky <alexander.belopolsky [at] gmail>:


----------
resolution: -> fixed
stage: commit review -> committed/rejected
status: open -> closed

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue9527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jun 22, 2012, 12:36 PM

Post #15 of 17 (229 views)
Permalink
[issue9527] Add aware local time support to datetime module [In reply to]

Jesús Cea Avión <jcea [at] jcea> added the comment:

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)
----------------------------------------------------------------------
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
? ^
"""

----------
nosy: +jcea

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue9527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jun 22, 2012, 12:39 PM

Post #16 of 17 (230 views)
Permalink
[issue9527] Add aware local time support to datetime module [In reply to]

Alexander Belopolsky <alexander.belopolsky [at] gmail> added the comment:

Working on this. It turns out tm_gmtoff uses the opposite sign to
that of timezone in time.h. For more confusion, consider this:

$ TZ=EST+5 date +%z
-0500

I am rechecking all UTC offset signs.

On Fri, Jun 22, 2012 at 3:36 PM, Jesús Cea Avión <report [at] bugs> wrote:
>
> Jesús Cea Avión <jcea [at] jcea> added the comment:
>
> 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)
> ----------------------------------------------------------------------
> 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
> ? ^
> """
>
> ----------
> nosy: +jcea
>
> _______________________________________
> Python tracker <report [at] bugs>
> <http://bugs.python.org/issue9527>
> _______________________________________

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue9527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jun 22, 2012, 1:09 PM

Post #17 of 17 (229 views)
Permalink
[issue9527] Add aware local time support to datetime module [In reply to]

Roundup Robot <devnull [at] psf> added the comment:

New changeset 0f0e3ec22fce by Alexander Belopolsky in branch 'default':
Issue #9527: tm_gmtoff has 'correct' sign.
http://hg.python.org/cpython/rev/0f0e3ec22fce

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue9527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.