DateTime.MarshalBSONValue loses precision (rounds to seconds)
dimovnike opened this issue · comments
The following implementation loses milliseconds when creating a BSON value.
// MarshalBSONValue is an interface implemented by types that can marshal themselves
// into a BSON document represented as bytes. The bytes returned must be a valid
// BSON document if the error is nil.
// 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
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(i64))
return bsontype.DateTime, buf, nil
}