Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ def _dict_to_bson(doc, check_keys, opts, top_level=True):
def _millis_to_datetime(millis, opts):
"""Convert milliseconds since epoch UTC to datetime."""
diff = ((millis % 1000) + 1000) % 1000
seconds = (millis - diff) / 1000
seconds = (millis - diff) // 1000
micros = diff * 1000
if opts.tz_aware:
dt = EPOCH_AWARE + datetime.timedelta(seconds=seconds,
Expand All @@ -837,7 +837,7 @@ def _datetime_to_millis(dtm):
if dtm.utcoffset() is not None:
dtm = dtm - dtm.utcoffset()
return int(calendar.timegm(dtm.timetuple()) * 1000 +
dtm.microsecond / 1000)
dtm.microsecond // 1000)
Copy link
Member Author

@ShaneHarvey ShaneHarvey Jul 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should line 822 also use // in seconds = (millis - diff) / 1000 ?



_CODEC_OPTIONS_TYPE_ERROR = TypeError(
Expand Down
7 changes: 7 additions & 0 deletions test/test_bson.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,13 @@ def test_datetime_encode_decode(self):
dt2 = BSON.encode({"date": dt1}).decode()["date"]
self.assertEqual(dt1, dt2)

def test_large_datetime_truncation(self):
# Ensure that a large datetime is truncated correctly.
dt1 = datetime.datetime(9999, 1, 1, 1, 1, 1, 999999)
dt2 = BSON.encode({"date": dt1}).decode()["date"]
self.assertEqual(dt2.microsecond, 999000)
self.assertEqual(dt2.second, dt1.second)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can reproduce this on Windows with 32-bit and 64-bit Python 3.X. So this fix does actually work.


def test_aware_datetime(self):
aware = datetime.datetime(1993, 4, 4, 2,
tzinfo=FixedOffset(555, "SomeZone"))
Expand Down