Skip to content

Commit

Permalink
fix loss of time component in Value(datetime)
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Jan 1, 2025
1 parent f91a5ea commit 3173355
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 2 additions & 0 deletions django_mongodb/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ def value(self, compiler, connection): # noqa: ARG001
return {"$literal": value}
if isinstance(value, Decimal):
return Decimal128(value)
if isinstance(value, datetime.datetime):
return value
if isinstance(value, datetime.date):
# Turn dates into datetimes since BSON doesn't support dates.
return datetime.datetime.combine(value, datetime.datetime.min.time())
Expand Down
2 changes: 1 addition & 1 deletion tests/expressions_/test_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_date(self):
def test_datetime(self):
self.assertEqual(
Value(datetime.datetime(2025, 1, 1, 9, 8, 7)).as_mql(None, None),
datetime.datetime(2025, 1, 1),
datetime.datetime(2025, 1, 1, 9, 8, 7),
)

def test_decimal(self):
Expand Down

0 comments on commit 3173355

Please sign in to comment.