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

-2
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

-2
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

+2-16
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

+6-5
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

+3-11
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

+2-5
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

+1-2
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

+1-1
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

+3-10
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

+17-16
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.

plain-models/plain/models/functions/datetime.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
YearLt,
1818
YearLte,
1919
)
20-
from plain.runtime import settings
2120
from plain.utils import timezone
2221

2322

@@ -29,13 +28,10 @@ def get_tzname(self):
2928
# applying a function. 2015-12-31 23:00:00 -02:00 is stored in the
3029
# database as 2016-01-01 01:00:00 +00:00. Any results should be
3130
# based on the input datetime not the stored datetime.
32-
tzname = None
33-
if settings.USE_TZ:
34-
if self.tzinfo is None:
35-
tzname = timezone.get_current_timezone_name()
36-
else:
37-
tzname = timezone._get_timezone_name(self.tzinfo)
38-
return tzname
31+
if self.tzinfo is None:
32+
return timezone.get_current_timezone_name()
33+
else:
34+
return timezone._get_timezone_name(self.tzinfo)
3935

4036

4137
class Extract(TimezoneMixin, Transform):
@@ -335,9 +331,7 @@ def resolve_expression(
335331

336332
def convert_value(self, value, expression, connection):
337333
if isinstance(self.output_field, DateTimeField):
338-
if not settings.USE_TZ:
339-
pass
340-
elif value is not None:
334+
if value is not None:
341335
value = value.replace(tzinfo=None)
342336
value = timezone.make_aware(value, self.tzinfo)
343337
elif not connection.features.has_zoneinfo_database:

plain-models/plain/models/query.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
create_namedtuple_class,
3838
resolve_callables,
3939
)
40-
from plain.runtime import settings
4140
from plain.utils import timezone
4241
from plain.utils.functional import cached_property, partition
4342

@@ -1259,11 +1258,10 @@ def datetimes(self, field_name, kind, order="ASC", tzinfo=None):
12591258
)
12601259
if order not in ("ASC", "DESC"):
12611260
raise ValueError("'order' must be either 'ASC' or 'DESC'.")
1262-
if settings.USE_TZ:
1263-
if tzinfo is None:
1264-
tzinfo = timezone.get_current_timezone()
1265-
else:
1266-
tzinfo = None
1261+
1262+
if tzinfo is None:
1263+
tzinfo = timezone.get_current_timezone()
1264+
12671265
return (
12681266
self.annotate(
12691267
datetimefield=Trunc(

plain/plain/forms/fields.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from plain import validators
1616
from plain.exceptions import ValidationError
17-
from plain.runtime import settings
1817
from plain.utils import timezone
1918
from plain.utils.dateparse import parse_datetime, parse_duration
2019
from plain.utils.duration import duration_string
@@ -1001,7 +1000,7 @@ def from_current_timezone(value):
10011000
When time zone support is enabled, convert naive datetimes
10021001
entered in the current time zone to aware datetimes.
10031002
"""
1004-
if settings.USE_TZ and value is not None and timezone.is_naive(value):
1003+
if value is not None and timezone.is_naive(value):
10051004
current_timezone = timezone.get_current_timezone()
10061005
try:
10071006
if timezone._datetime_ambiguous_or_imaginary(value, current_timezone):
@@ -1025,6 +1024,6 @@ def to_current_timezone(value):
10251024
When time zone support is enabled, convert aware datetimes
10261025
to naive datetimes in the current time zone for display.
10271026
"""
1028-
if settings.USE_TZ and value is not None and timezone.is_aware(value):
1027+
if value is not None and timezone.is_aware(value):
10291028
return timezone.make_naive(value)
10301029
return value

plain/plain/runtime/global_settings.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@
2020

2121
# Local time zone for this installation. All choices can be found here:
2222
# https://en.wikipedia.org/wiki/List_of_tz_zones_by_name (although not all
23-
# systems may support all possibilities). When USE_TZ is True, this is
24-
# interpreted as the default user time zone.
23+
# systems may support all possibilities). This is interpreted as the default
24+
# user time zone.
2525
TIME_ZONE: str = "UTC"
2626

27-
# If you set this to True, Plain will use timezone-aware datetimes.
28-
USE_TZ = True
29-
3027
# Default charset to use for all Response objects, if a MIME type isn't
3128
# manually specified. It's used to construct the Content-Type header.
3229
DEFAULT_CHARSET = "utf-8"

0 commit comments

Comments
 (0)