Skip to content

Commit 86e6dfa

Browse files
committed
Remove USE_TZ setting
1 parent d592380 commit 86e6dfa

File tree

15 files changed

+50
-118
lines changed

15 files changed

+50
-118
lines changed

plain-htmx/tests/settings.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
INSTALLED_PACKAGES = [
22
"plain.htmx",
33
]
4-
5-
USE_TZ = True

plain-importmap/test_project/settings.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,3 @@
5757
USE_I18N = True
5858

5959
USE_L10N = True
60-
61-
USE_TZ = True

plain-models/plain/models/backends/base/base.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from collections import deque
1010
from contextlib import contextmanager
1111

12-
from plain.exceptions import ImproperlyConfigured
1312
from plain.models.backends import utils
1413
from plain.models.backends.base.validation import BaseDatabaseValidation
1514
from plain.models.backends.signals import connection_created
@@ -150,9 +149,7 @@ def timezone(self):
150149
Plain uses may be constrained by the requirements of other users of
151150
the database.
152151
"""
153-
if not settings.USE_TZ:
154-
return None
155-
elif self.settings_dict["TIME_ZONE"] is None:
152+
if self.settings_dict["TIME_ZONE"] is None:
156153
return datetime.timezone.utc
157154
else:
158155
return zoneinfo.ZoneInfo(self.settings_dict["TIME_ZONE"])
@@ -162,9 +159,7 @@ def timezone_name(self):
162159
"""
163160
Name of the time zone of the database connection.
164161
"""
165-
if not settings.USE_TZ:
166-
return settings.TIME_ZONE
167-
elif self.settings_dict["TIME_ZONE"] is None:
162+
if self.settings_dict["TIME_ZONE"] is None:
168163
return "UTC"
169164
else:
170165
return self.settings_dict["TIME_ZONE"]
@@ -238,8 +233,6 @@ def create_cursor(self, name=None):
238233

239234
def connect(self):
240235
"""Connect to the database. Assume that the connection is closed."""
241-
# Check for invalid configurations.
242-
self.check_settings()
243236
# In case the previous connection was closed while in an atomic block
244237
self.in_atomic_block = False
245238
self.savepoint_ids = []
@@ -262,13 +255,6 @@ def connect(self):
262255

263256
self.run_on_commit = []
264257

265-
def check_settings(self):
266-
if self.settings_dict["TIME_ZONE"] is not None and not settings.USE_TZ:
267-
raise ImproperlyConfigured(
268-
"Connection '%s' cannot set TIME_ZONE because USE_TZ is False."
269-
% self.alias
270-
)
271-
272258
def ensure_connection(self):
273259
"""Guarantee that a connection to the database is established."""
274260
if self.connection is None:

plain-models/plain/models/backends/base/operations.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
from plain.models.backends import utils
99
from plain.models.db import NotSupportedError
10-
from plain.runtime import settings
1110
from plain.utils import timezone
1211
from plain.utils.encoding import force_str
1312

@@ -591,10 +590,12 @@ def year_lookup_bounds_for_datetime_field(self, value, iso_year=False):
591590
else:
592591
first = datetime.datetime(value, 1, 1)
593592
second = datetime.datetime(value, 12, 31, 23, 59, 59, 999999)
594-
if settings.USE_TZ:
595-
tz = timezone.get_current_timezone()
596-
first = timezone.make_aware(first, tz)
597-
second = timezone.make_aware(second, tz)
593+
594+
# Make sure that datetimes are aware in the current timezone
595+
tz = timezone.get_current_timezone()
596+
first = timezone.make_aware(first, tz)
597+
second = timezone.make_aware(second, tz)
598+
598599
first = self.adapt_datetimefield_value(first)
599600
second = self.adapt_datetimefield_value(second)
600601
return [first, second]

plain-models/plain/models/backends/mysql/operations.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from plain.models.constants import OnConflict
66
from plain.models.expressions import Exists, ExpressionWrapper
77
from plain.models.lookups import Lookup
8-
from plain.runtime import settings
98
from plain.utils import timezone
109
from plain.utils.encoding import force_str
1110
from plain.utils.regex_helper import _lazy_re_compile
@@ -91,7 +90,7 @@ def _prepare_tzname_delta(self, tzname):
9190
return f"{sign}{offset}" if offset else tzname
9291

9392
def _convert_sql_to_tz(self, sql, params, tzname):
94-
if tzname and settings.USE_TZ and self.connection.timezone_name != tzname:
93+
if tzname and self.connection.timezone_name != tzname:
9594
return f"CONVERT_TZ({sql}, %s, %s)", (
9695
*params,
9796
self.connection.timezone_name,
@@ -230,13 +229,7 @@ def adapt_datetimefield_value(self, value):
230229

231230
# MySQL doesn't support tz-aware datetimes
232231
if timezone.is_aware(value):
233-
if settings.USE_TZ:
234-
value = timezone.make_naive(value, self.connection.timezone)
235-
else:
236-
raise ValueError(
237-
"MySQL backend does not support timezone-aware datetimes when "
238-
"USE_TZ is False."
239-
)
232+
value = timezone.make_naive(value, self.connection.timezone)
240233
return str(value)
241234

242235
def adapt_timefield_value(self, value):
@@ -283,8 +276,7 @@ def get_db_converters(self, expression):
283276
if internal_type == "BooleanField":
284277
converters.append(self.convert_booleanfield_value)
285278
elif internal_type == "DateTimeField":
286-
if settings.USE_TZ:
287-
converters.append(self.convert_datetimefield_value)
279+
converters.append(self.convert_datetimefield_value)
288280
elif internal_type == "UUIDField":
289281
converters.append(self.convert_uuidfield_value)
290282
return converters

plain-models/plain/models/backends/postgresql/base.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from plain.models.backends.utils import CursorDebugWrapper as BaseCursorDebugWrapper
1414
from plain.models.db import DatabaseError as WrappedDatabaseError
1515
from plain.models.db import connections
16-
from plain.runtime import settings
1716
from plain.utils.functional import cached_property
1817
from plain.utils.safestring import SafeString
1918

@@ -213,9 +212,7 @@ def get_connection_params(self):
213212
if settings_dict["PORT"]:
214213
conn_params["port"] = settings_dict["PORT"]
215214
if is_psycopg3:
216-
conn_params["context"] = get_adapters_template(
217-
settings.USE_TZ, self.timezone
218-
)
215+
conn_params["context"] = get_adapters_template(self.timezone)
219216
# Disable prepared statements by default to keep connection poolers
220217
# working. Can be reenabled via OPTIONS in the settings dict.
221218
conn_params["prepare_threshold"] = conn_params.pop(
@@ -315,7 +312,7 @@ def create_cursor(self, name=None):
315312
if self.timezone != tzloader.timezone:
316313
register_tzloader(self.timezone, cursor)
317314
else:
318-
cursor.tzinfo_factory = self.tzinfo_factory if settings.USE_TZ else None
315+
cursor.tzinfo_factory = self.tzinfo_factory
319316
return cursor
320317

321318
def tzinfo_factory(self, offset):

plain-models/plain/models/backends/postgresql/operations.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
)
1212
from plain.models.backends.utils import split_tzname_delta
1313
from plain.models.constants import OnConflict
14-
from plain.runtime import settings
1514
from plain.utils.regex_helper import _lazy_re_compile
1615

1716

@@ -106,7 +105,7 @@ def _prepare_tzname_delta(self, tzname):
106105
return tzname
107106

108107
def _convert_sql_to_tz(self, sql, params, tzname):
109-
if tzname and settings.USE_TZ:
108+
if tzname:
110109
tzname_param = self._prepare_tzname_delta(tzname)
111110
return f"{sql} AT TIME ZONE %s", (*params, tzname_param)
112111
return sql, params

plain-models/plain/models/backends/postgresql/psycopg_any.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def upgrade(self, obj, format):
5151
return dumper
5252

5353
@lru_cache
54-
def get_adapters_template(use_tz, timezone):
54+
def get_adapters_template(timezone):
5555
# Create at adapters map extending the base one.
5656
ctx = adapt.AdaptersMap(adapters)
5757
# Register a no-op dumper to avoid a round trip from psycopg version 3

plain-models/plain/models/backends/sqlite3/operations.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from plain.models.constants import OnConflict
1010
from plain.models.db import DatabaseError, NotSupportedError
1111
from plain.models.expressions import Col
12-
from plain.runtime import settings
1312
from plain.utils import timezone
1413
from plain.utils.dateparse import parse_date, parse_datetime, parse_time
1514
from plain.utils.functional import cached_property
@@ -103,7 +102,7 @@ def time_trunc_sql(self, lookup_type, sql, params, tzname=None):
103102
)
104103

105104
def _convert_tznames_to_sql(self, tzname):
106-
if tzname and settings.USE_TZ:
105+
if tzname:
107106
return tzname, self.connection.timezone_name
108107
return None, None
109108

@@ -245,13 +244,7 @@ def adapt_datetimefield_value(self, value):
245244

246245
# SQLite doesn't support tz-aware datetimes
247246
if timezone.is_aware(value):
248-
if settings.USE_TZ:
249-
value = timezone.make_naive(value, self.connection.timezone)
250-
else:
251-
raise ValueError(
252-
"SQLite backend does not support timezone-aware datetimes when "
253-
"USE_TZ is False."
254-
)
247+
value = timezone.make_naive(value, self.connection.timezone)
255248

256249
return str(value)
257250

@@ -290,7 +283,7 @@ def convert_datetimefield_value(self, value, expression, connection):
290283
if value is not None:
291284
if not isinstance(value, datetime.datetime):
292285
value = parse_datetime(value)
293-
if settings.USE_TZ and not timezone.is_aware(value):
286+
if not timezone.is_aware(value):
294287
value = timezone.make_aware(value, self.connection.timezone)
295288
return value
296289

plain-models/plain/models/fields/__init__.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ def to_python(self, value):
12891289
if value is None:
12901290
return value
12911291
if isinstance(value, datetime.datetime):
1292-
if settings.USE_TZ and timezone.is_aware(value):
1292+
if timezone.is_aware(value):
12931293
# Convert aware datetimes to the default time zone
12941294
# before casting them to dates (#17742).
12951295
default_timezone = timezone.get_default_timezone()
@@ -1391,20 +1391,21 @@ def to_python(self, value):
13911391
return value
13921392
if isinstance(value, datetime.date):
13931393
value = datetime.datetime(value.year, value.month, value.day)
1394-
if settings.USE_TZ:
1395-
# For backwards compatibility, interpret naive datetimes in
1396-
# local time. This won't work during DST change, but we can't
1397-
# do much about it, so we let the exceptions percolate up the
1398-
# call stack.
1399-
warnings.warn(
1400-
"DateTimeField {}.{} received a naive datetime "
1401-
"({}) while time zone support is active.".format(
1402-
self.model.__name__, self.name, value
1403-
),
1404-
RuntimeWarning,
1405-
)
1406-
default_timezone = timezone.get_default_timezone()
1407-
value = timezone.make_aware(value, default_timezone)
1394+
1395+
# For backwards compatibility, interpret naive datetimes in
1396+
# local time. This won't work during DST change, but we can't
1397+
# do much about it, so we let the exceptions percolate up the
1398+
# call stack.
1399+
warnings.warn(
1400+
"DateTimeField {}.{} received a naive datetime "
1401+
"({}) while time zone support is active.".format(
1402+
self.model.__name__, self.name, value
1403+
),
1404+
RuntimeWarning,
1405+
)
1406+
default_timezone = timezone.get_default_timezone()
1407+
value = timezone.make_aware(value, default_timezone)
1408+
14081409
return value
14091410

14101411
try:
@@ -1449,7 +1450,7 @@ def pre_save(self, model_instance, add):
14491450
def get_prep_value(self, value):
14501451
value = super().get_prep_value(value)
14511452
value = self.to_python(value)
1452-
if value is not None and settings.USE_TZ and timezone.is_naive(value):
1453+
if value is not None and timezone.is_naive(value):
14531454
# For backwards compatibility, interpret naive datetimes in local
14541455
# time. This won't work during DST change, but we can't do much
14551456
# about it, so we let the exceptions percolate up the call stack.

0 commit comments

Comments
 (0)