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
=== modified file 'src/pytz/tzinfo.py'
--- src/pytz/tzinfo.py 2014-11-03 08:24:30 +0000
+++ src/pytz/tzinfo.py 2014-12-31 13:19:05 +0000
@@ -164,15 +164,32 @@
164 _dst = None # DST offset164 _dst = None # DST offset
165165
166 def __init__(self, _inf=None, _tzinfos=None):166 def __init__(self, _inf=None, _tzinfos=None):
167 '''Construct a new DstTzInfo object.
168
169 >>> from pytz import timezone
170 >>> london = timezone('Europe/London')
171 >>> dt = datetime(2014, 1, 1, 0, 0, 0, tzinfo=london)
172 >>> london.utcoffset(dt)
173 datetime.timedelta(0)
174 '''
167 if _inf:175 if _inf:
168 self._tzinfos = _tzinfos176 self._tzinfos = _tzinfos
169 self._utcoffset, self._dst, self._tzname = _inf177 self._utcoffset, self._dst, self._tzname = _inf
170 else:178 else:
171 _tzinfos = {}179 _tzinfos = {}
172 self._tzinfos = _tzinfos180 self._tzinfos = _tzinfos
173 self._utcoffset, self._dst, self._tzname = self._transition_info[0]181 if len(self._transition_info) == 1:
174 _tzinfos[self._transition_info[0]] = self182 # Use the first rule (which should also be the only one).
175 for inf in self._transition_info[1:]:183 self._utcoffset, self._dst, self._tzname = (
184 self._transition_info[0])
185 _tzinfos[self._transition_info[0]] = self
186 else:
187 for inf in reversed(self._transition_info):
188 if not inf[1]:
189 self._utcoffset, self._dst, self._tzname = inf
190 _tzinfos[inf] = self
191 break
192 for inf in self._transition_info:
176 if inf not in _tzinfos:193 if inf not in _tzinfos:
177 _tzinfos[inf] = self.__class__(inf, _tzinfos)194 _tzinfos[inf] = self.__class__(inf, _tzinfos)
178195

Subscribers

People subscribed via source and target branches