Skip to content

Commit c90a00c

Browse files
committed
add TruncTime support
1 parent 7c62294 commit c90a00c

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
@@ -473,11 +473,6 @@ def django_test_expected_failures(self):
473473
"model_fields.test_datetimefield.DateTimeFieldTests.test_lookup_date_with_use_tz",
474474
"timezones.tests.NewDatabaseTests.test_query_convert_timezones",
475475
},
476-
"TruncTime database function not supported.": {
477-
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_time_comparison",
478-
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_time_func",
479-
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_time_none",
480-
},
481476
"MongoDB can't annotate ($project) a function like PI().": {
482477
"aggregation.tests.AggregateTestCase.test_aggregation_default_using_decimal_from_database",
483478
"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 (
@@ -213,6 +214,23 @@ def trunc_date(self, compiler, connection):
213214
}
214215

215216

217+
def trunc_time(self, compiler, connection):
218+
lhs_mql = process_lhs(self, compiler, connection)
219+
return {
220+
"$dateFromString": {
221+
"dateString": {
222+
"$concat": [
223+
# Times are stored with date(1, 1, 1)), so by
224+
# replacing any existing date component with that, the
225+
# result of TruncTime can be compared to TimeField.
226+
"0001-01-01T",
227+
{"$dateToString": {"format": "%H:%M:%S.%L", "date": lhs_mql}},
228+
]
229+
}
230+
}
231+
}
232+
233+
216234
def register_functions():
217235
Cast.as_mql = cast
218236
Concat.as_mql = concat
@@ -235,4 +253,5 @@ def register_functions():
235253
Trim.as_mql = trim("trim")
236254
TruncBase.as_mql = trunc
237255
TruncDate.as_mql = trunc_date
256+
TruncTime.as_mql = trunc_time
238257
Upper.as_mql = preserve_null("toUpper")

0 commit comments

Comments
 (0)