Skip to content

Commit 9f98e23

Browse files
committed
Adds a small speedup when changing timezones.
1 parent 1d7cc25 commit 9f98e23

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

pendulum/tz/timezone.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def __init__(self, name, transitions,
1919
self._transitions = transitions
2020
self._transition_types = transition_types
2121
self._default_transition_type = default_transition_type
22+
self._local_hint = {}
2223

2324
@property
2425
def name(self):
@@ -202,13 +203,7 @@ def _get_timestamp(self, dt):
202203
if hasattr(dt, 'float_timestamp'):
203204
return dt.float_timestamp
204205

205-
if dt.tzinfo is None:
206-
t = _time.mktime((dt.year, dt.month, dt.day,
207-
dt.hour, dt.minute, dt.second,
208-
-1, -1, -1)) + dt.microsecond / 1e6
209-
210-
else:
211-
t = (dt - datetime(1970, 1, 1, tzinfo=UTC)).total_seconds()
206+
t = (dt - datetime(1970, 1, 1, tzinfo=UTC)).total_seconds()
212207

213208
if dt.microsecond > 0 and t < 0:
214209
t -= 1
@@ -217,6 +212,14 @@ def _get_timestamp(self, dt):
217212

218213
def _find_transition_index(self, dt, prop='_time'):
219214
lo, hi = 0, len(self._transitions)
215+
hint = self._local_hint.get(prop)
216+
if hint:
217+
if dt == hint[0]:
218+
return hint[1]
219+
elif dt < hint[0]:
220+
hi = hint[1]
221+
else:
222+
lo = hint[1]
220223

221224
while lo < hi:
222225
mid = (lo + hi) // 2
@@ -225,6 +228,8 @@ def _find_transition_index(self, dt, prop='_time'):
225228
else:
226229
lo = mid + 1
227230

231+
self._local_hint[prop] = (dt, lo)
232+
228233
return lo
229234

230235
def __repr__(self):

0 commit comments

Comments
 (0)