Skip to content

Commit

Permalink
Fixed #27327 -- Simplified time zone handling by requiring pytz.
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Oct 27, 2016
1 parent d84ffcc commit 414ad25
Show file tree
Hide file tree
Showing 30 changed files with 109 additions and 426 deletions.
12 changes: 2 additions & 10 deletions django/db/backends/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from collections import deque
from contextlib import contextmanager

import pytz

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db import DEFAULT_DB_ALIAS
Expand All @@ -16,11 +18,6 @@
from django.utils.functional import cached_property
from django.utils.six.moves import _thread as thread

try:
import pytz
except ImportError:
pytz = None

NO_DB_ALIAS = '__no_db__'


Expand Down Expand Up @@ -128,7 +125,6 @@ def timezone(self):
elif self.settings_dict['TIME_ZONE'] is None:
return timezone.utc
else:
# Only this branch requires pytz.
return pytz.timezone(self.settings_dict['TIME_ZONE'])

@cached_property
Expand Down Expand Up @@ -207,10 +203,6 @@ def check_settings(self):
raise ImproperlyConfigured(
"Connection '%s' cannot set TIME_ZONE because its engine "
"handles time zones conversions natively." % self.alias)
elif pytz is None:
raise ImproperlyConfigured(
"Connection '%s' cannot set TIME_ZONE because pytz isn't "
"installed." % self.alias)

def ensure_connection(self):
"""
Expand Down
13 changes: 0 additions & 13 deletions django/db/backends/mysql/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@

from .base import Database

try:
import pytz
except ImportError:
pytz = None


class DatabaseFeatures(BaseDatabaseFeatures):
empty_fetchmany_value = ()
Expand Down Expand Up @@ -56,14 +51,6 @@ def supports_microsecond_precision(self):

@cached_property
def has_zoneinfo_database(self):
# MySQL accepts full time zones names (eg. Africa/Nairobi) but rejects
# abbreviations (eg. EAT). When pytz isn't installed and the current
# time zone is LocalTimezone (the only sensible value in this
# context), the current time zone name will be an abbreviation. As a
# consequence, MySQL cannot perform time zone conversions reliably.
if pytz is None:
return False

# Test if the time zone definitions are installed.
with self.connection.cursor() as cursor:
cursor.execute("SELECT 1 FROM mysql.time_zone LIMIT 1")
Expand Down
6 changes: 0 additions & 6 deletions django/db/backends/oracle/features.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
from django.db.backends.base.features import BaseDatabaseFeatures
from django.db.utils import InterfaceError

try:
import pytz
except ImportError:
pytz = None


class DatabaseFeatures(BaseDatabaseFeatures):
empty_fetchmany_value = ()
Expand All @@ -19,7 +14,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
supports_subqueries_in_group_by = False
supports_transactions = True
supports_timezones = False
has_zoneinfo_database = pytz is not None
supports_bitwise_or = False
has_native_duration_field = True
can_defer_constraint_checks = True
Expand Down
7 changes: 2 additions & 5 deletions django/db/backends/sqlite3/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import re
import warnings

import pytz

from django.conf import settings
from django.db import utils
from django.db.backends import utils as backend_utils
Expand All @@ -23,11 +25,6 @@
from django.utils.encoding import force_text
from django.utils.safestring import SafeBytes

try:
import pytz
except ImportError:
pytz = None

try:
try:
from pysqlite2 import dbapi2 as Database
Expand Down
9 changes: 0 additions & 9 deletions django/db/backends/sqlite3/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@

from .base import Database

try:
import pytz
except ImportError:
pytz = None


class DatabaseFeatures(BaseDatabaseFeatures):
# SQLite cannot handle us only partially reading from a cursor's result set
Expand Down Expand Up @@ -78,7 +73,3 @@ def supports_stddev(self):
has_support = False
cursor.execute('DROP TABLE STDDEV_TEST')
return has_support

@cached_property
def has_zoneinfo_database(self):
return pytz is not None
15 changes: 1 addition & 14 deletions django/db/backends/sqlite3/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import uuid

from django.conf import settings
from django.core.exceptions import FieldError, ImproperlyConfigured
from django.core.exceptions import FieldError
from django.db import utils
from django.db.backends import utils as backend_utils
from django.db.backends.base.operations import BaseDatabaseOperations
Expand All @@ -13,11 +13,6 @@
from django.utils.dateparse import parse_date, parse_datetime, parse_time
from django.utils.duration import duration_string

try:
import pytz
except ImportError:
pytz = None


class DatabaseOperations(BaseDatabaseOperations):
def bulk_batch_size(self, fields, objs):
Expand Down Expand Up @@ -77,27 +72,19 @@ def time_trunc_sql(self, lookup_type, field_name):
# cause a collision with a field name).
return "django_time_trunc('%s', %s)" % (lookup_type.lower(), field_name)

def _require_pytz(self):
if settings.USE_TZ and pytz is None:
raise ImproperlyConfigured("This query requires pytz, but it isn't installed.")

def datetime_cast_date_sql(self, field_name, tzname):
self._require_pytz()
return "django_datetime_cast_date(%s, %%s)" % field_name, [tzname]

def datetime_cast_time_sql(self, field_name, tzname):
self._require_pytz()
return "django_datetime_cast_time(%s, %%s)" % field_name, [tzname]

def datetime_extract_sql(self, lookup_type, field_name, tzname):
# Same comment as in date_extract_sql.
self._require_pytz()
return "django_datetime_extract('%s', %s, %%s)" % (
lookup_type.lower(), field_name), [tzname]

def datetime_trunc_sql(self, lookup_type, field_name, tzname):
# Same comment as in date_trunc_sql.
self._require_pytz()
return "django_datetime_trunc('%s', %s, %%s)" % (
lookup_type.lower(), field_name), [tzname]

Expand Down
2 changes: 1 addition & 1 deletion django/db/models/functions/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def convert_value(self, value, expression, connection, context):
if value is None:
raise ValueError(
"Database returned an invalid datetime value. "
"Are time zone definitions for your database and pytz installed?"
"Are time zone definitions for your database installed?"
)
value = value.replace(tzinfo=None)
value = timezone.make_aware(value, self.tzinfo)
Expand Down
15 changes: 5 additions & 10 deletions django/templatetags/tz.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
from datetime import datetime, tzinfo

import pytz

from django.template import Library, Node, TemplateSyntaxError
from django.utils import six, timezone

try:
import pytz
except ImportError:
pytz = None


register = Library()


Expand Down Expand Up @@ -44,7 +40,6 @@ def do_timezone(value, arg):
Converts a datetime to local time in a given time zone.
The argument must be an instance of a tzinfo subclass or a time zone name.
If it is a time zone name, pytz is required.
Naive datetimes are assumed to be in local time in the default time zone.
"""
Expand All @@ -64,7 +59,7 @@ def do_timezone(value, arg):
# Obtain a tzinfo instance
if isinstance(arg, tzinfo):
tz = arg
elif isinstance(arg, six.string_types) and pytz is not None:
elif isinstance(arg, six.string_types):
try:
tz = pytz.timezone(arg)
except pytz.UnknownTimeZoneError:
Expand Down Expand Up @@ -156,8 +151,8 @@ def timezone_tag(parser, token):
Enables a given time zone just for this block.
The ``timezone`` argument must be an instance of a ``tzinfo`` subclass, a
time zone name, or ``None``. If is it a time zone name, pytz is required.
If it is ``None``, the default time zone is used within the block.
time zone name, or ``None``. If it is ``None``, the default time zone is
used within the block.
Sample usage::
Expand Down
Loading

0 comments on commit 414ad25

Please sign in to comment.