From e23240474b4c7d630a4224a558f2e47a4b957cd1 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Tue, 18 Nov 2014 21:50:03 +0100 Subject: [PATCH] Simplified caching of get_default_timezone(). --- django/test/signals.py | 2 +- django/utils/timezone.py | 21 +++++++++------------ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/django/test/signals.py b/django/test/signals.py index f065e155b8..c38dd80688 100644 --- a/django/test/signals.py +++ b/django/test/signals.py @@ -59,7 +59,7 @@ def update_connections_time_zone(**kwargs): time.tzset() # Reset local time zone cache - timezone._localtime = None + timezone.get_default_timezone.cache_clear() # Reset the database connections' time zone if kwargs['setting'] == 'USE_TZ' and settings.TIME_ZONE != 'UTC': diff --git a/django/utils/timezone.py b/django/utils/timezone.py index 5b83e8a3aa..a45a72f2f9 100644 --- a/django/utils/timezone.py +++ b/django/utils/timezone.py @@ -15,6 +15,7 @@ except ImportError: pytz = None from django.conf import settings +from django.utils import lru_cache from django.utils import six from django.utils.decorators import ContextDecorator @@ -162,25 +163,21 @@ def get_fixed_timezone(offset): name = sign + hhmm return FixedOffset(offset, name) -# In order to avoid accessing the settings at compile time, -# wrap the expression in a function and cache the result. -_localtime = None - +# In order to avoid accessing settings at compile time, +# wrap the logic in a function and cache the result. +@lru_cache.lru_cache() def get_default_timezone(): """ Returns the default time zone as a tzinfo instance. This is the time zone defined by settings.TIME_ZONE. """ - global _localtime - if _localtime is None: - if isinstance(settings.TIME_ZONE, six.string_types) and pytz is not None: - _localtime = pytz.timezone(settings.TIME_ZONE) - else: - # This relies on os.environ['TZ'] being set to settings.TIME_ZONE. - _localtime = LocalTimezone() - return _localtime + if isinstance(settings.TIME_ZONE, six.string_types) and pytz is not None: + return pytz.timezone(settings.TIME_ZONE) + else: + # This relies on os.environ['TZ'] being set to settings.TIME_ZONE. + return LocalTimezone() # This function exists for consistency with get_current_timezone_name