From e143a57f9cd400f93a808b4310c77ba122d27a47 Mon Sep 17 00:00:00 2001 From: Nick Dimov <3619341+dimovnike@users.noreply.github.com> Date: Thu, 7 Oct 2021 17:28:50 +0300 Subject: [PATCH] issue-85: DateTime.MarshalBSONValue() was losing milliseconds Also adjusted the tests to catch lost milliseconds. Signed-off-by: Nick Dimov <3619341+dimovnike@users.noreply.github.com> --- time.go | 9 ++++++--- time_test.go | 6 +++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/time.go b/time.go index 425413f..9c9359a 100644 --- a/time.go +++ b/time.go @@ -210,9 +210,12 @@ func (t *DateTime) UnmarshalBSON(data []byte) error { // Marshals a DateTime as a bsontype.DateTime, an int64 representing // milliseconds since epoch. func (t DateTime) MarshalBSONValue() (bsontype.Type, []byte, error) { - // UnixNano cannot be used, the result of calling UnixNano on the zero - // Time is undefined. - i64 := NormalizeTimeForMarshal(time.Time(t)).Unix() * 1000 + // UnixNano cannot be used directly, the result of calling UnixNano on the zero + // Time is undefined. Thats why we use time.Nanosecond() instead. + + tNorm := NormalizeTimeForMarshal(time.Time(t)) + i64 := tNorm.Unix()*1000 + int64(tNorm.Nanosecond())/1e6 + buf := make([]byte, 8) binary.LittleEndian.PutUint64(buf, uint64(i64)) diff --git a/time_test.go b/time_test.go index e97c3e1..206e7ca 100644 --- a/time_test.go +++ b/time_test.go @@ -39,9 +39,9 @@ var ( {[]byte("2014-12-15T08:00Z"), time.Date(2014, 12, 15, 8, 0, 0, 0, time.UTC), "2014-12-15T08:00:00.000Z", "2014-12-15T08:00:00.000Z"}, {[]byte("2018-01-28T23:54Z"), time.Date(2018, 01, 28, 23, 54, 0, 0, time.UTC), "2018-01-28T23:54:00.000Z", "2018-01-28T23:54:00.000Z"}, {[]byte("2014-12-15T08:00:00.000Z"), time.Date(2014, 12, 15, 8, 0, 0, 0, time.UTC), "2014-12-15T08:00:00.000Z", "2014-12-15T08:00:00.000Z"}, - {[]byte("2011-08-18T19:03:37.000000000+01:00"), time.Date(2011, 8, 18, 19, 3, 37, 0, p.Location()), "2011-08-18T19:03:37.000+01:00", "2011-08-18T18:03:37.000Z"}, - {[]byte("2011-08-18T19:03:37.000000+0100"), time.Date(2011, 8, 18, 19, 3, 37, 0, p.Location()), "2011-08-18T19:03:37.000+01:00", "2011-08-18T18:03:37.000Z"}, - {[]byte("2011-08-18T19:03:37.000+0100"), time.Date(2011, 8, 18, 19, 3, 37, 0, p.Location()), "2011-08-18T19:03:37.000+01:00", "2011-08-18T18:03:37.000Z"}, + {[]byte("2011-08-18T19:03:37.123000000+01:00"), time.Date(2011, 8, 18, 19, 3, 37, 123*1e6, p.Location()), "2011-08-18T19:03:37.123+01:00", "2011-08-18T18:03:37.123Z"}, + {[]byte("2011-08-18T19:03:37.123000+0100"), time.Date(2011, 8, 18, 19, 3, 37, 123*1e6, p.Location()), "2011-08-18T19:03:37.123+01:00", "2011-08-18T18:03:37.123Z"}, + {[]byte("2011-08-18T19:03:37.123+0100"), time.Date(2011, 8, 18, 19, 3, 37, 123*1e6, p.Location()), "2011-08-18T19:03:37.123+01:00", "2011-08-18T18:03:37.123Z"}, {[]byte("2014-12-15T19:30:20Z"), time.Date(2014, 12, 15, 19, 30, 20, 0, time.UTC), "2014-12-15T19:30:20.000Z", "2014-12-15T19:30:20.000Z"}, {[]byte("0001-01-01T00:00:00Z"), time.Time{}.UTC(), "0001-01-01T00:00:00.000Z", "0001-01-01T00:00:00.000Z"}, {[]byte(""), time.Unix(0, 0).UTC(), "1970-01-01T00:00:00.000Z", "1970-01-01T00:00:00.000Z"},