Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.6.x] Fixed #18766 -- Pointed to pytz when LocalTimezone fails.

Thanks void for the report.

Backport of ded11aa from master.
  • Loading branch information...
commit 7c31e195db963ad1ebb28265e721097000fa84d1 1 parent c03848b
Aymeric Augustin authored
25  django/utils/timezone.py
... ...
@@ -1,10 +1,12 @@
1  
-"""Timezone helper functions.
  1
+"""
  2
+Timezone-related classes and functions.
2 3
 
3 4
 This module uses pytz when it's available and fallbacks when it isn't.
4 5
 """
5 6
 
6 7
 from datetime import datetime, timedelta, tzinfo
7 8
 from threading import local
  9
+import sys
8 10
 import time as _time
9 11
 
10 12
 try:
@@ -45,12 +47,14 @@ def tzname(self, dt):
45 47
     def dst(self, dt):
46 48
         return ZERO
47 49
 
48  
-class LocalTimezone(tzinfo):
  50
+class ReferenceLocalTimezone(tzinfo):
49 51
     """
50 52
     Local time implementation taken from Python's docs.
51 53
 
52 54
     Used only when pytz isn't available, and most likely inaccurate. If you're
53 55
     having trouble with this class, don't waste your time, just install pytz.
  56
+
  57
+    Kept identical to the reference version. Subclasses contain improvements.
54 58
     """
55 59
 
56 60
     def __init__(self):
@@ -91,6 +95,23 @@ def _isdst(self, dt):
91 95
         tt = _time.localtime(stamp)
92 96
         return tt.tm_isdst > 0
93 97
 
  98
+class LocalTimezone(ReferenceLocalTimezone):
  99
+    """
  100
+    Slightly improved local time implementation focusing on correctness.
  101
+
  102
+    It still crashes on dates before 1970 or after 2038, but at least the
  103
+    error message is helpful.
  104
+    """
  105
+
  106
+    def _isdst(self, dt):
  107
+        try:
  108
+            return super(LocalTimezone, self)._isdst(dt)
  109
+        except (OverflowError, ValueError) as exc:
  110
+            exc_type = type(exc)
  111
+            exc_value = exc_type(
  112
+                    "Unsupported value: %r. You should install pytz." % dt)
  113
+            exc_value.__cause__ = exc
  114
+            six.reraise(exc_type, exc_value, sys.exc_info()[2])
94 115
 
95 116
 utc = pytz.utc if pytz else UTC()
96 117
 """UTC time zone as a tzinfo instance."""
7  tests/utils_tests/test_timezone.py
@@ -2,6 +2,7 @@
2 2
 import datetime
3 3
 import pickle
4 4
 from django.test.utils import override_settings
  5
+from django.utils import six
5 6
 from django.utils import timezone
6 7
 from django.utils.tzinfo import FixedOffset
7 8
 from django.utils import unittest
@@ -19,6 +20,12 @@ def test_localtime(self):
19 20
         local_now = timezone.localtime(now, local_tz)
20 21
         self.assertEqual(local_now.tzinfo, local_tz)
21 22
 
  23
+    def test_localtime_out_of_range(self):
  24
+        local_tz = timezone.LocalTimezone()
  25
+        long_ago = datetime.datetime(1900, 1, 1, tzinfo=timezone.utc)
  26
+        with six.assertRaisesRegex(self, OverflowError, "install pytz"):
  27
+            timezone.localtime(long_ago, local_tz)
  28
+
22 29
     def test_now(self):
23 30
         with override_settings(USE_TZ=True):
24 31
             self.assertTrue(timezone.is_aware(timezone.now()))

0 notes on commit 7c31e19

Please sign in to comment.
Something went wrong with that request. Please try again.