Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTime.MarshalBSONValue loses precision (rounds to seconds) #85

Closed
dimovnike opened this issue Oct 7, 2021 · 1 comment
Closed

DateTime.MarshalBSONValue loses precision (rounds to seconds) #85

dimovnike opened this issue Oct 7, 2021 · 1 comment

Comments

@dimovnike
Copy link
Contributor

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
}
@dimovnike
Copy link
Contributor Author

whould you please accept a pull request #86 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant