Skip to content

Commit 12ea1a2

Browse files
committed
Adds FixedTimezone cache
1 parent 3bae48d commit 12ea1a2

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

pendulum/pendulum.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ def _safe_create_datetime_zone(cls, obj):
6666
if isinstance(obj, (int, float)):
6767
timezone_offset = obj * 60 * 60
6868

69-
return FixedTimezone(timezone_offset)
69+
return FixedTimezone.load(timezone_offset)
7070
elif isinstance(obj, datetime.tzinfo) and not isinstance(obj, Timezone):
7171
# pytz
7272
if hasattr(obj, 'localize'):
7373
return cls._timezone(obj.zone)
7474

75-
return FixedTimezone(obj.utcoffset(None).total_seconds())
75+
return FixedTimezone.load(obj.utcoffset(None).total_seconds())
7676

7777
return cls._timezone(obj)
7878

pendulum/tz/timezone.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def load(cls, name):
7474
returns it from the cache.
7575
7676
:param name: The name of the timezone
77-
:type name: str
77+
:type name: str or int
7878
7979
:rtype: Timezone
8080
"""
@@ -450,6 +450,8 @@ class FixedTimezone(Timezone):
450450
A timezone that has a fixed offset to UTC.
451451
"""
452452

453+
_cache = {}
454+
453455
def __init__(self, offset, name=None, transition_type=None):
454456
"""
455457
:param offset: offset to UTC in seconds.
@@ -479,6 +481,13 @@ def __init__(self, offset, name=None, transition_type=None):
479481
)
480482
self._tzinfo = self._tzinfos[0]
481483

484+
@classmethod
485+
def load(cls, name):
486+
if name not in cls._cache:
487+
cls._cache[name] = cls(name)
488+
489+
return cls._cache[name]
490+
482491
def _normalize(self, dt, dst_rule=Timezone.POST_TRANSITION):
483492
return dt.replace(tzinfo=self._tzinfo)
484493

0 commit comments

Comments
 (0)