mongodb / bson-rust

Encoding and decoding support for BSON in Rust
MIT License
400 stars 132 forks source link

Add chrono::TimeDelta to mongodb::bson::DateTime #489

Closed ChanManChan closed 1 month ago

ChanManChan commented 2 months ago

Is there a better way to add chrono::Duration to bson::DateTime without converting to chrono::DateTime?

currently I'm doing this expires_at: DateTime::from_millis(now.timestamp_millis() + duration.num_milliseconds()),

isabelatkinson commented 1 month ago

Hi @ChanManChan, there is currently no way to add a chrono::Duration/chrono::TimeDelta directly to a bson::DateTime. Your current approach looks reasonable. Enabling the chrono-0_4 feature flag if you have not done so already will also allow you to convert more easily between bson::DateTime and chrono::DateTime via the to_chrono and from_chrono methods.

ChanManChan commented 1 month ago

Hi @ChanManChan, there is currently no way to add a chrono::Duration/chrono::TimeDelta directly to a bson::DateTime. Your current approach looks reasonable. Enabling the chrono-0_4 feature flag if you have not done so already will also allow you to convert more easily between bson::DateTime and chrono::DateTime via the to_chrono and from_chrono methods.

Thanks for the suggestions but I think I'm sticking with my solution if you find it reasonable.