Skip to content

Commit b71a93f

Browse files
mckelvinbmoscon
authored andcommitted
Cache for mktz (pandas-dev#303)
Performance in mktz can be quite slow, so this change implements a cache to improve the performance of the timezone lookups.
1 parent c9048bc commit b71a93f

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

arctic/date/_mktz.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import dateutil
44
import tzlocal
55
import six
6+
import weakref
7+
8+
9+
_TZ_CACHE = weakref.WeakValueDictionary()
610

711

812
class TimezoneError(Exception):
@@ -34,6 +38,11 @@ def mktz(zone=None):
3438
if zone is None:
3539
zone = tzlocal.get_localzone().zone
3640
zone = six.u(zone)
41+
42+
cached = _TZ_CACHE.get(zone)
43+
if cached is not None:
44+
return cached
45+
3746
tz = dateutil.tz.gettz(zone)
3847
if not tz:
3948
raise TimezoneError('Timezone "%s" can not be read' % (zone))
@@ -44,4 +53,5 @@ def mktz(zone=None):
4453
if zone.startswith(p):
4554
tz.zone = zone[len(p) + 1:]
4655
break
56+
_TZ_CACHE[zone] = tz
4757
return tz

0 commit comments

Comments
 (0)