Skip to content

Commit e143a57

Browse files
committed
issue-85: DateTime.MarshalBSONValue() was losing milliseconds
Also adjusted the tests to catch lost milliseconds. Signed-off-by: Nick Dimov <[email protected]>
1 parent 583b813 commit e143a57

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

time.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,12 @@ func (t *DateTime) UnmarshalBSON(data []byte) error {
210210
// Marshals a DateTime as a bsontype.DateTime, an int64 representing
211211
// milliseconds since epoch.
212212
func (t DateTime) MarshalBSONValue() (bsontype.Type, []byte, error) {
213-
// UnixNano cannot be used, the result of calling UnixNano on the zero
214-
// Time is undefined.
215-
i64 := NormalizeTimeForMarshal(time.Time(t)).Unix() * 1000
213+
// UnixNano cannot be used directly, the result of calling UnixNano on the zero
214+
// Time is undefined. Thats why we use time.Nanosecond() instead.
215+
216+
tNorm := NormalizeTimeForMarshal(time.Time(t))
217+
i64 := tNorm.Unix()*1000 + int64(tNorm.Nanosecond())/1e6
218+
216219
buf := make([]byte, 8)
217220
binary.LittleEndian.PutUint64(buf, uint64(i64))
218221

time_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ var (
3939
{[]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"},
4040
{[]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"},
4141
{[]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"},
42-
{[]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"},
43-
{[]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"},
44-
{[]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"},
42+
{[]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"},
43+
{[]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"},
44+
{[]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"},
4545
{[]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"},
4646
{[]byte("0001-01-01T00:00:00Z"), time.Time{}.UTC(), "0001-01-01T00:00:00.000Z", "0001-01-01T00:00:00.000Z"},
4747
{[]byte(""), time.Unix(0, 0).UTC(), "1970-01-01T00:00:00.000Z", "1970-01-01T00:00:00.000Z"},

0 commit comments

Comments
 (0)