Skip to content

Commit f633798

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 43cbc05 commit f633798

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

django_mongodb_backend/functions.py

+3
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ def trunc_date(self, compiler, connection):
249249

250250

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

docs/source/topics/known-issues.rst

+5
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ Database functions
7575
:class:`~django.db.models.functions.SHA512`
7676
- :class:`~django.db.models.functions.Sign`
7777

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

tests/db_functions_/models.py

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

+26
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)