Skip to content

Commit

Permalink
Fixed #33037 -- Fixed Trunc() with offset timezones on MySQL, SQLite,…
Browse files Browse the repository at this point in the history
… Oracle.
  • Loading branch information
adzshaf authored and felixxm committed Feb 15, 2024
1 parent 2aa8388 commit 22285d3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -919,6 +919,7 @@ answer newbie questions, and generally made Django that much better:
Sergey Fedoseev <fedoseev.sergey@gmail.com>
Sergey Kolosov <m17.admin@gmail.com>
Seth Hill <sethrh@gmail.com>
Shafiya Adzhani <adz.arsym@gmail.com>
Shai Berger <shai@platonix.com>
Shannon -jj Behrens <https://www.jjinux.com/>
Shawn Milochik <shawn@milochik.com>
Expand Down
5 changes: 4 additions & 1 deletion django/db/backends/sqlite3/_functions.py
Expand Up @@ -118,7 +118,10 @@ def _sqlite_datetime_parse(dt, tzname=None, conn_tzname=None):
hours, minutes = offset.split(":")
offset_delta = timedelta(hours=int(hours), minutes=int(minutes))
dt += offset_delta if sign == "+" else -offset_delta
dt = timezone.localtime(dt, zoneinfo.ZoneInfo(tzname))
# The tzname may originally be just the offset e.g. "+3:00",
# which becomes an empty string after splitting the sign and offset.
# In this case, use the conn_tzname as fallback.
dt = timezone.localtime(dt, zoneinfo.ZoneInfo(tzname or conn_tzname))
return dt


Expand Down
2 changes: 2 additions & 0 deletions django/db/backends/utils.py
Expand Up @@ -200,6 +200,8 @@ def split_tzname_delta(tzname):
if sign in tzname:
name, offset = tzname.rsplit(sign, 1)
if offset and parse_time(offset):
if ":" not in offset:
offset = f"{offset}:00"
return name, sign, offset
return tzname, None, None

Expand Down
44 changes: 29 additions & 15 deletions tests/db_functions/datetime/test_extract_trunc.py
Expand Up @@ -1832,17 +1832,18 @@ def test_trunc_func_with_timezone(self):
end_datetime = timezone.make_aware(end_datetime)
self.create_model(start_datetime, end_datetime)
self.create_model(end_datetime, start_datetime)
melb = zoneinfo.ZoneInfo("Australia/Melbourne")

def assertDatetimeKind(kind):
truncated_start = truncate_to(start_datetime.astimezone(melb), kind, melb)
truncated_end = truncate_to(end_datetime.astimezone(melb), kind, melb)
def assertDatetimeKind(kind, tzinfo):
truncated_start = truncate_to(
start_datetime.astimezone(tzinfo), kind, tzinfo
)
truncated_end = truncate_to(end_datetime.astimezone(tzinfo), kind, tzinfo)
queryset = DTModel.objects.annotate(
truncated=Trunc(
"start_datetime",
kind,
output_field=DateTimeField(),
tzinfo=melb,
tzinfo=tzinfo,
)
).order_by("start_datetime")
self.assertSequenceEqual(
Expand All @@ -1853,15 +1854,17 @@ def assertDatetimeKind(kind):
],
)

def assertDatetimeToDateKind(kind):
truncated_start = truncate_to(start_datetime.astimezone(melb).date(), kind)
truncated_end = truncate_to(end_datetime.astimezone(melb).date(), kind)
def assertDatetimeToDateKind(kind, tzinfo):
truncated_start = truncate_to(
start_datetime.astimezone(tzinfo).date(), kind
)
truncated_end = truncate_to(end_datetime.astimezone(tzinfo).date(), kind)
queryset = DTModel.objects.annotate(
truncated=Trunc(
"start_datetime",
kind,
output_field=DateField(),
tzinfo=melb,
tzinfo=tzinfo,
),
).order_by("start_datetime")
self.assertSequenceEqual(
Expand All @@ -1872,15 +1875,17 @@ def assertDatetimeToDateKind(kind):
],
)

def assertDatetimeToTimeKind(kind):
truncated_start = truncate_to(start_datetime.astimezone(melb).time(), kind)
truncated_end = truncate_to(end_datetime.astimezone(melb).time(), kind)
def assertDatetimeToTimeKind(kind, tzinfo):
truncated_start = truncate_to(
start_datetime.astimezone(tzinfo).time(), kind
)
truncated_end = truncate_to(end_datetime.astimezone(tzinfo).time(), kind)
queryset = DTModel.objects.annotate(
truncated=Trunc(
"start_datetime",
kind,
output_field=TimeField(),
tzinfo=melb,
tzinfo=tzinfo,
)
).order_by("start_datetime")
self.assertSequenceEqual(
Expand All @@ -1891,6 +1896,10 @@ def assertDatetimeToTimeKind(kind):
],
)

timezones = [
zoneinfo.ZoneInfo("Australia/Melbourne"),
zoneinfo.ZoneInfo("Etc/GMT+10"),
]
date_truncations = ["year", "quarter", "month", "week", "day"]
time_truncations = ["hour", "minute", "second"]
tests = [
Expand All @@ -1900,8 +1909,13 @@ def assertDatetimeToTimeKind(kind):
]
for assertion, truncations in tests:
for truncation in truncations:
with self.subTest(assertion=assertion.__name__, truncation=truncation):
assertion(truncation)
for tzinfo in timezones:
with self.subTest(
assertion=assertion.__name__,
truncation=truncation,
tzinfo=tzinfo.key,
):
assertion(truncation, tzinfo)

qs = DTModel.objects.filter(
start_datetime__date=Trunc(
Expand Down

0 comments on commit 22285d3

Please sign in to comment.