diff options
author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2013-09-07 18:56:49 -0500 |
---|---|---|
committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2013-09-08 09:17:03 +0200 |
commit | 7c31e195db963ad1ebb28265e721097000fa84d1 (patch) | |
tree | 8a9cf042ec778031a7f79835abd687d24ce2b5b9 /django | |
parent | c03848b540d2787adda27f73a69a7980b38c7ac6 (diff) | |
download | django-7c31e195db963ad1ebb28265e721097000fa84d1.tar.gz |
[1.6.x] Fixed #18766 -- Pointed to pytz when LocalTimezone fails.
Thanks void for the report.
Backport of ded11aa6 from master.
Diffstat (limited to 'django')
-rw-r--r-- | django/utils/timezone.py | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/django/utils/timezone.py b/django/utils/timezone.py index 3493e056fb..93ef4dfd57 100644 --- a/django/utils/timezone.py +++ b/django/utils/timezone.py @@ -1,10 +1,12 @@ -"""Timezone helper functions. +""" +Timezone-related classes and functions. This module uses pytz when it's available and fallbacks when it isn't. """ from datetime import datetime, timedelta, tzinfo from threading import local +import sys import time as _time try: @@ -45,12 +47,14 @@ class UTC(tzinfo): def dst(self, dt): return ZERO -class LocalTimezone(tzinfo): +class ReferenceLocalTimezone(tzinfo): """ Local time implementation taken from Python's docs. Used only when pytz isn't available, and most likely inaccurate. If you're having trouble with this class, don't waste your time, just install pytz. + + Kept identical to the reference version. Subclasses contain improvements. """ def __init__(self): @@ -91,6 +95,23 @@ class LocalTimezone(tzinfo): tt = _time.localtime(stamp) return tt.tm_isdst > 0 +class LocalTimezone(ReferenceLocalTimezone): + """ + Slightly improved local time implementation focusing on correctness. + + It still crashes on dates before 1970 or after 2038, but at least the + error message is helpful. + """ + + def _isdst(self, dt): + try: + return super(LocalTimezone, self)._isdst(dt) + except (OverflowError, ValueError) as exc: + exc_type = type(exc) + exc_value = exc_type( + "Unsupported value: %r. You should install pytz." % dt) + exc_value.__cause__ = exc + six.reraise(exc_type, exc_value, sys.exc_info()[2]) utc = pytz.utc if pytz else UTC() """UTC time zone as a tzinfo instance.""" |