Merge lp:~cjwatson/pytz/smarter-utcoffset into lp:pytz

Proposed by Colin Watson
Status: Needs review
Proposed branch: lp:~cjwatson/pytz/smarter-utcoffset
Merge into: lp:pytz
Diff against target: 38 lines (+20/-3)
1 file modified
src/pytz/tzinfo.py (+20/-3)
To merge this branch: bzr merge lp:~cjwatson/pytz/smarter-utcoffset
Reviewer Review Type Date Requested Status
Stuart Bishop Pending
Review via email: mp+245472@code.launchpad.net

Commit message

Use the most recent appropriate transition for DstTzInfo._utcoffset and friends, rather than the first transition which may be very old.

Description of the change

This fixes nonsensical _utcoffset values for various timezones that were constructed based on extremely old timezone transitions. The replacement algorithm here is much closer to what localtime() does. I've confirmed that this fixes the test failures from http://lpbuildbot.canonical.com/builders/lp-devel-precise/builds/277/steps/shell_9/logs/summary that aren't just due to renamed zones.

To post a comment you must log in.

Unmerged revisions

357. By Colin Watson

Use the most recent appropriate transition for DstTzInfo._utcoffset and friends, rather than the first transition which may be very old.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/pytz/tzinfo.py'
2--- src/pytz/tzinfo.py 2014-11-03 08:24:30 +0000
3+++ src/pytz/tzinfo.py 2014-12-31 13:19:05 +0000
4@@ -164,15 +164,32 @@
5 _dst = None # DST offset
6
7 def __init__(self, _inf=None, _tzinfos=None):
8+ '''Construct a new DstTzInfo object.
9+
10+ >>> from pytz import timezone
11+ >>> london = timezone('Europe/London')
12+ >>> dt = datetime(2014, 1, 1, 0, 0, 0, tzinfo=london)
13+ >>> london.utcoffset(dt)
14+ datetime.timedelta(0)
15+ '''
16 if _inf:
17 self._tzinfos = _tzinfos
18 self._utcoffset, self._dst, self._tzname = _inf
19 else:
20 _tzinfos = {}
21 self._tzinfos = _tzinfos
22- self._utcoffset, self._dst, self._tzname = self._transition_info[0]
23- _tzinfos[self._transition_info[0]] = self
24- for inf in self._transition_info[1:]:
25+ if len(self._transition_info) == 1:
26+ # Use the first rule (which should also be the only one).
27+ self._utcoffset, self._dst, self._tzname = (
28+ self._transition_info[0])
29+ _tzinfos[self._transition_info[0]] = self
30+ else:
31+ for inf in reversed(self._transition_info):
32+ if not inf[1]:
33+ self._utcoffset, self._dst, self._tzname = inf
34+ _tzinfos[inf] = self
35+ break
36+ for inf in self._transition_info:
37 if inf not in _tzinfos:
38 _tzinfos[inf] = self.__class__(inf, _tzinfos)
39

Subscribers

People subscribed via source and target branches