Skip to content

Commit b62338a

Browse files
authored
RUST-1129 Make the dependency on chrono optional and provide equivalent time interop (#352)
1 parent c7e9072 commit b62338a

File tree

16 files changed

+407
-122
lines changed

16 files changed

+407
-122
lines changed

.evergreen/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ axes:
181181
- id: "extra-rust-versions"
182182
values:
183183
- id: "min"
184-
display_name: "1.48 (minimum supported version)"
184+
display_name: "1.53 (minimum supported version)"
185185
variables:
186-
RUST_VERSION: "1.48.0"
186+
RUST_VERSION: "1.53.0"
187187
- id: "nightly"
188188
display_name: "nightly"
189189
variables:

.evergreen/run-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -o errexit
44

55
. ~/.cargo/env
66
RUST_BACKTRACE=1 cargo test
7-
RUST_BACKTRACE=1 cargo test --features chrono-0_4,uuid-0_8
7+
RUST_BACKTRACE=1 cargo test --all-features
88

99
cd serde-tests
1010
RUST_BACKTRACE=1 cargo test

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ exclude = [
3333
[features]
3434
default = []
3535
# if enabled, include API for interfacing with chrono 0.4
36-
chrono-0_4 = []
36+
chrono-0_4 = ["chrono"]
3737
# if enabled, include API for interfacing with uuid 0.8
3838
uuid-0_8 = []
39+
# if enabled, include API for interfacing with time 0.3
40+
time-0_3 = []
3941
# if enabled, include serde_with interop.
4042
# should be used in conjunction with chrono-0_4 or uuid-0_8.
4143
# it's commented out here because Cargo implicitly adds a feature flag for
@@ -47,7 +49,7 @@ name = "bson"
4749

4850
[dependencies]
4951
ahash = "0.7.2"
50-
chrono = { version = "0.4.15", features = ["std"], default-features = false }
52+
chrono = { version = "0.4.15", features = ["std"], default-features = false, optional = true }
5153
rand = "0.8"
5254
serde = { version = "1.0", features = ["derive"] }
5355
serde_json = { version = "1.0", features = ["preserve_order"] }
@@ -58,6 +60,7 @@ lazy_static = "1.4.0"
5860
uuid = { version = "0.8.1", features = ["serde", "v4"] }
5961
serde_bytes = "0.11.5"
6062
serde_with = { version = "1", optional = true }
63+
time = { version = "0.3.9", features = ["formatting", "parsing", "macros", "large-dates"] }
6164

6265
[dev-dependencies]
6366
assert_matches = "1.2"

src/bson.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use std::{
2626
fmt::{self, Debug, Display, Formatter},
2727
};
2828

29-
use chrono::Datelike;
3029
use serde_json::{json, Value};
3130

3231
pub use crate::document::Document;
@@ -311,6 +310,14 @@ impl From<oid::ObjectId> for Bson {
311310
}
312311
}
313312

313+
#[cfg(feature = "time-0_3")]
314+
#[cfg_attr(docsrs, doc(cfg(feature = "time-0_3")))]
315+
impl From<time::OffsetDateTime> for Bson {
316+
fn from(a: time::OffsetDateTime) -> Bson {
317+
Bson::DateTime(crate::DateTime::from(a))
318+
}
319+
}
320+
314321
#[cfg(feature = "chrono-0_4")]
315322
#[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))]
316323
impl<T: chrono::TimeZone> From<chrono::DateTime<T>> for Bson {
@@ -430,9 +437,10 @@ impl Bson {
430437
})
431438
}
432439
Bson::ObjectId(v) => json!({"$oid": v.to_hex()}),
433-
Bson::DateTime(v) if v.timestamp_millis() >= 0 && v.to_chrono().year() <= 99999 => {
440+
Bson::DateTime(v) if v.timestamp_millis() >= 0 && v.to_time_0_3().year() <= 9999 => {
434441
json!({
435-
"$date": v.to_rfc3339_string(),
442+
// Unwrap safety: timestamps in the guarded range can always be formatted.
443+
"$date": v.try_to_rfc3339_string().unwrap(),
436444
})
437445
}
438446
Bson::DateTime(v) => json!({
@@ -592,9 +600,10 @@ impl Bson {
592600
Bson::DateTime(v) if rawbson => doc! {
593601
"$date": v.timestamp_millis(),
594602
},
595-
Bson::DateTime(v) if v.timestamp_millis() >= 0 && v.to_chrono().year() <= 9999 => {
603+
Bson::DateTime(v) if v.timestamp_millis() >= 0 && v.to_time_0_3().year() <= 9999 => {
596604
doc! {
597-
"$date": v.to_rfc3339_string(),
605+
// Unwrap safety: timestamps in the guarded range can always be formatted.
606+
"$date": v.try_to_rfc3339_string().unwrap(),
598607
}
599608
}
600609
Bson::DateTime(v) => doc! {
@@ -776,8 +785,8 @@ impl Bson {
776785
}
777786

778787
if let Ok(date) = doc.get_str("$date") {
779-
if let Ok(date) = chrono::DateTime::parse_from_rfc3339(date) {
780-
return Bson::DateTime(crate::DateTime::from_chrono(date));
788+
if let Ok(dt) = crate::DateTime::parse_rfc3339_str(date) {
789+
return Bson::DateTime(dt);
781790
}
782791
}
783792
}

0 commit comments

Comments
 (0)