Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.5.x] Fixed #19708 -- Exception in timezone.override(None).

Thanks rafales.

Backport of 9a4a1ce.
  • Loading branch information...
commit db49b6d2b298bc53b55df9ee7cf80e6492d3116b 1 parent 73bb966
@aaugustin aaugustin authored
View
7 django/utils/timezone.py
@@ -111,6 +111,7 @@ def get_default_timezone():
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
@@ -198,10 +199,10 @@ def __enter__(self):
activate(self.timezone)
def __exit__(self, exc_type, exc_value, traceback):
- if self.old_timezone is not None:
- _active.value = self.old_timezone
+ if self.old_timezone is None:
+ deactivate()
else:
- del _active.value
+ _active.value = self.old_timezone
# Templates
View
30 tests/regressiontests/utils/timezone.py
@@ -3,9 +3,14 @@
import pickle
from django.test.utils import override_settings
from django.utils import timezone
+from django.utils.tzinfo import FixedOffset
from django.utils import unittest
+EAT = FixedOffset(180) # Africa/Nairobi
+ICT = FixedOffset(420) # Asia/Bangkok
+
+
class TimezoneTests(unittest.TestCase):
def test_localtime(self):
@@ -20,6 +25,31 @@ def test_now(self):
with override_settings(USE_TZ=False):
self.assertTrue(timezone.is_naive(timezone.now()))
+ def test_override(self):
+ default = timezone.get_default_timezone()
+ try:
+ timezone.activate(ICT)
+
+ with timezone.override(EAT):
+ self.assertIs(EAT, timezone.get_current_timezone())
+ self.assertIs(ICT, timezone.get_current_timezone())
+
+ with timezone.override(None):
+ self.assertIs(default, timezone.get_current_timezone())
+ self.assertIs(ICT, timezone.get_current_timezone())
+
+ timezone.deactivate()
+
+ with timezone.override(EAT):
+ self.assertIs(EAT, timezone.get_current_timezone())
+ self.assertIs(default, timezone.get_current_timezone())
+
+ with timezone.override(None):
+ self.assertIs(default, timezone.get_current_timezone())
+ self.assertIs(default, timezone.get_current_timezone())
+ finally:
+ timezone.deactivate()
+
def test_copy(self):
self.assertIsInstance(copy.copy(timezone.UTC()), timezone.UTC)
self.assertIsInstance(copy.copy(timezone.LocalTimezone()), timezone.LocalTimezone)
Please sign in to comment.
Something went wrong with that request. Please try again.