Skip to content

Commit 7c62294

Browse files
committed
add TruncDate support
1 parent 546554a commit 7c62294

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ Congratulations, your project is ready to go!
203203
- `Right`
204204
- `SHA1`, `SHA224`, `SHA256`, `SHA384`, `SHA512`
205205
- `Sign`
206-
- `TruncDate`
207206
- `TruncTime`
208207

209208
- The `tzinfo` parameter of the `Trunc` database functions doesn't work

django_mongodb/features.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -469,14 +469,8 @@ def django_test_expected_failures(self):
469469
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_extract_quarter_func",
470470
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_extract_quarter_func_boundaries",
471471
},
472-
"TruncDate database function not supported.": {
473-
"aggregation.tests.AggregateTestCase.test_aggregation_default_using_date_from_database",
474-
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_date_func",
475-
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_date_none",
476-
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_lookup_name_sql_injection",
477-
"expressions.tests.FieldTransformTests.test_multiple_transforms_in_values",
472+
"TruncDate database function's tzinfo not supported.": {
478473
"model_fields.test_datetimefield.DateTimeFieldTests.test_lookup_date_with_use_tz",
479-
"model_fields.test_datetimefield.DateTimeFieldTests.test_lookup_date_without_use_tz",
480474
"timezones.tests.NewDatabaseTests.test_query_convert_timezones",
481475
},
482476
"TruncTime database function not supported.": {

django_mongodb/functions.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
ExtractYear,
1616
Now,
1717
TruncBase,
18+
TruncDate,
1819
)
1920
from django.db.models.functions.math import Ceil, Cot, Degrees, Log, Power, Radians, Random, Round
2021
from django.db.models.functions.text import (
@@ -191,6 +192,27 @@ def trunc(self, compiler, connection):
191192
return {"$dateTrunc": lhs_mql}
192193

193194

195+
def trunc_date(self, compiler, connection):
196+
# Cast to date rather than truncate to date.
197+
lhs_mql = process_lhs(self, compiler, connection)
198+
tzname = self.get_tzname()
199+
if tzname and tzname != "UTC":
200+
raise NotSupportedError(f"TruncDate with tzinfo ({tzname}) isn't supported on MongoDB.")
201+
return {
202+
"$dateFromString": {
203+
"dateString": {
204+
"$concat": [
205+
{"$dateToString": {"format": "%Y-%m-%d", "date": lhs_mql}},
206+
# Dates are stored with time(0, 0), so by replacing any
207+
# existing time component with that, the result of
208+
# TruncDate can be compared to DateField.
209+
"T00:00:00.000",
210+
]
211+
},
212+
}
213+
}
214+
215+
194216
def register_functions():
195217
Cast.as_mql = cast
196218
Concat.as_mql = concat
@@ -212,4 +234,5 @@ def register_functions():
212234
Substr.as_mql = substr
213235
Trim.as_mql = trim("trim")
214236
TruncBase.as_mql = trunc
237+
TruncDate.as_mql = trunc_date
215238
Upper.as_mql = preserve_null("toUpper")

0 commit comments

Comments
 (0)