Skip to content

Commit

Permalink
add TruncDate support
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Dec 20, 2024
1 parent 546554a commit 7c62294
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ Congratulations, your project is ready to go!
- `Right`
- `SHA1`, `SHA224`, `SHA256`, `SHA384`, `SHA512`
- `Sign`
- `TruncDate`
- `TruncTime`

- The `tzinfo` parameter of the `Trunc` database functions doesn't work
Expand Down
8 changes: 1 addition & 7 deletions django_mongodb/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,14 +469,8 @@ def django_test_expected_failures(self):
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_extract_quarter_func",
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_extract_quarter_func_boundaries",
},
"TruncDate database function not supported.": {
"aggregation.tests.AggregateTestCase.test_aggregation_default_using_date_from_database",
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_date_func",
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_date_none",
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_lookup_name_sql_injection",
"expressions.tests.FieldTransformTests.test_multiple_transforms_in_values",
"TruncDate database function's tzinfo not supported.": {
"model_fields.test_datetimefield.DateTimeFieldTests.test_lookup_date_with_use_tz",
"model_fields.test_datetimefield.DateTimeFieldTests.test_lookup_date_without_use_tz",
"timezones.tests.NewDatabaseTests.test_query_convert_timezones",
},
"TruncTime database function not supported.": {
Expand Down
23 changes: 23 additions & 0 deletions django_mongodb/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ExtractYear,
Now,
TruncBase,
TruncDate,
)
from django.db.models.functions.math import Ceil, Cot, Degrees, Log, Power, Radians, Random, Round
from django.db.models.functions.text import (
Expand Down Expand Up @@ -191,6 +192,27 @@ def trunc(self, compiler, connection):
return {"$dateTrunc": lhs_mql}


def trunc_date(self, compiler, connection):
# Cast to date rather than truncate to date.
lhs_mql = process_lhs(self, compiler, connection)
tzname = self.get_tzname()
if tzname and tzname != "UTC":
raise NotSupportedError(f"TruncDate with tzinfo ({tzname}) isn't supported on MongoDB.")
return {
"$dateFromString": {
"dateString": {
"$concat": [
{"$dateToString": {"format": "%Y-%m-%d", "date": lhs_mql}},
# Dates are stored with time(0, 0), so by replacing any
# existing time component with that, the result of
# TruncDate can be compared to DateField.
"T00:00:00.000",
]
},
}
}


def register_functions():
Cast.as_mql = cast
Concat.as_mql = concat
Expand All @@ -212,4 +234,5 @@ def register_functions():
Substr.as_mql = substr
Trim.as_mql = trim("trim")
TruncBase.as_mql = trunc
TruncDate.as_mql = trunc_date
Upper.as_mql = preserve_null("toUpper")

0 comments on commit 7c62294

Please sign in to comment.