Skip to content

Commit ac28121

Browse files
committed
Document the tzinfo parameter of TruncDate/TruncTime as unsupported
Add the same exception raising from TruncDate to TruncTime and add tests for both functions.
1 parent 87fa60c commit ac28121

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

django_mongodb_backend/functions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ def trunc_date(self, compiler, connection):
248248

249249

250250
def trunc_time(self, compiler, connection):
251+
tzname = self.get_tzname()
252+
if tzname and tzname != "UTC":
253+
raise NotSupportedError(f"TruncTime with tzinfo ({tzname}) isn't supported on MongoDB.")
251254
lhs_mql = process_lhs(self, compiler, connection)
252255
return {
253256
"$dateFromString": {

docs/source/topics/known-issues.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ Database functions
7474
:class:`~django.db.models.functions.SHA512`
7575
- :class:`~django.db.models.functions.Sign`
7676

77+
- The ``tzinfo`` parameter of the
78+
:class:`~django.db.models.functions.TruncDate` and
79+
:class:`~django.db.models.functions.TruncTime` database functions isn't
80+
supported.
81+
7782
Transaction management
7883
======================
7984

tests/db_functions_/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.db import models
2+
3+
4+
class DTModel(models.Model):
5+
start_datetime = models.DateTimeField(null=True, blank=True)

tests/db_functions_/test_datetime.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from zoneinfo import ZoneInfo
2+
3+
from django.db import NotSupportedError
4+
from django.db.models.functions import TruncDate, TruncTime
5+
from django.test import TestCase, override_settings
6+
7+
from .models import DTModel
8+
9+
10+
@override_settings(USE_TZ=True)
11+
class TruncTests(TestCase):
12+
melb = ZoneInfo("Australia/Melbourne")
13+
14+
def test_truncdate_tzinfo(self):
15+
msg = "TruncDate with tzinfo (Australia/Melbourne) isn't supported on MongoDB."
16+
with self.assertRaisesMessage(NotSupportedError, msg):
17+
DTModel.objects.annotate(
18+
melb_date=TruncDate("start_datetime", tzinfo=self.melb),
19+
).get()
20+
21+
def test_trunctime_tzinfo(self):
22+
msg = "TruncTime with tzinfo (Australia/Melbourne) isn't supported on MongoDB."
23+
with self.assertRaisesMessage(NotSupportedError, msg):
24+
DTModel.objects.annotate(
25+
melb_date=TruncTime("start_datetime", tzinfo=self.melb),
26+
).get()

0 commit comments

Comments
 (0)