Skip to content

Commit dfadf99

Browse files
committed
add TruncTime support
1 parent d9a981d commit dfadf99

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
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-
- `TruncTime`
207206

208207
- The `tzinfo` parameter of the `Trunc` database functions doesn't work
209208
properly because MongoDB converts the result back to UTC.

django_mongodb/features.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -512,11 +512,6 @@ def django_test_expected_failures(self):
512512
"model_fields.test_datetimefield.DateTimeFieldTests.test_lookup_date_with_use_tz",
513513
"timezones.tests.NewDatabaseTests.test_query_convert_timezones",
514514
},
515-
"TruncTime database function not supported.": {
516-
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_time_comparison",
517-
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_time_func",
518-
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_time_none",
519-
},
520515
"MongoDB can't annotate ($project) a function like PI().": {
521516
"aggregation.tests.AggregateTestCase.test_aggregation_default_using_decimal_from_database",
522517
"db_functions.math.test_pi.PiTests.test",

django_mongodb/functions.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
Now,
1717
TruncBase,
1818
TruncDate,
19+
TruncTime,
1920
)
2021
from django.db.models.functions.math import Ceil, Cot, Degrees, Log, Power, Radians, Random, Round
2122
from django.db.models.functions.text import (
@@ -201,6 +202,23 @@ def trunc_date(self, compiler, connection):
201202
return {"$toDate": lhs_mql}
202203

203204

205+
def trunc_time(self, compiler, connection):
206+
lhs_mql = process_lhs(self, compiler, connection)
207+
return {
208+
"$dateFromString": {
209+
"dateString": {
210+
"$concat": [
211+
# Times are stored with datetime.min.date(), so by
212+
# replacing any existing date component with that, the
213+
# result of TruncTime can be compared to TimeField.
214+
"0001-01-01T",
215+
{"$dateToString": {"format": "%H:%M:%S.%L", "date": lhs_mql}},
216+
]
217+
}
218+
}
219+
}
220+
221+
204222
def register_functions():
205223
Cast.as_mql = cast
206224
Concat.as_mql = concat
@@ -223,4 +241,5 @@ def register_functions():
223241
Trim.as_mql = trim("trim")
224242
TruncBase.as_mql = trunc
225243
TruncDate.as_mql = trunc_date
244+
TruncTime.as_mql = trunc_time
226245
Upper.as_mql = preserve_null("toUpper")

0 commit comments

Comments
 (0)