Permalink
Browse files

Optimize transition lookup for timezone handling in Python bindings.

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...
1 parent f357257 commit 6ce3e150c614a54de5b81a624fb442f0c12498a3 @wagnerrp wagnerrp committed Sep 8, 2012
Showing with 22 additions and 5 deletions.
  1. +22 −5 mythtv/bindings/python/MythTV/utility/dt.py
@@ -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)

0 comments on commit 6ce3e15

Please sign in to comment.