Skip to content

Commit

Permalink
Merge pull request #86 from dimovnike/85-bson-precision-loss
Browse files Browse the repository at this point in the history
issue-85: DateTime.MarshalBSONValue() was losing milliseconds
  • Loading branch information
casualjim committed Oct 9, 2021
2 parents 583b813 + e143a57 commit cdff709
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
9 changes: 6 additions & 3 deletions time.go
Expand Up @@ -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))

Expand Down
6 changes: 3 additions & 3 deletions time_test.go
Expand Up @@ -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"},
Expand Down

0 comments on commit cdff709

Please sign in to comment.