Skip to content

Commit

Permalink
Optimize transition lookup for timezone handling in Python bindings.
Browse files Browse the repository at this point in the history
This modifies the transition lookup for timezone handling classes to
start the search from the last used transition, rather than starting
from the beginning of the list each time.
  • Loading branch information
wagnerrp committed Sep 8, 2012
1 parent f357257 commit 6ce3e15
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions mythtv/bindings/python/MythTV/utility/dt.py
Expand Up @@ -24,15 +24,32 @@ class basetzinfo( _pytzinfo ):
'time utc local offset abbrev isdst')

def _get_transition(self, dt=None):
try:
index = self.__last
except AttributeError:
index = 0

if dt is None:
dt = _pydatetime.now()
dt = (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second)

for i, transition in enumerate(self._transitions):
if dt < transition.local[0:5]:
transition = self._transitions[i-1]
break
return transition
direction = 0
while True:
if dt < self._transitions[index].local[0:5]:
if direction == 1:
index -= 1
break
else:
direction = -1
else:
if direction == -1:
break
else:
direction = 1
index += direction

self.__last = index
return self._transitions[index]

def utcoffset(self, dt=None):
return timedelta(0, self._get_transition(dt).offset)
Expand Down

0 comments on commit 6ce3e15

Please sign in to comment.