Skip to content

RUST-1960 Make large-dates time feature optional #546

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

Merged
merged 4 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ default = ["compat-3-0-0"]
compat-3-0-0 = []
# if enabled, include API for interfacing with chrono 0.4
chrono-0_4 = ["dep:chrono"]
# enable the large-dates feature for the time crate
large_dates = ["time/large-dates"]
# if enabled, include API for interfacing with uuid 1.x
uuid-1 = []
# if enabled, include API for interfacing with time 0.3
Expand All @@ -62,7 +64,7 @@ once_cell = "1.5.1"
uuid = { version = "1.1.2", features = ["serde", "v4"] }
serde_bytes = "0.11.5"
serde_with = { version = "3.1.0", optional = true }
time = { version = "0.3.9", features = ["formatting", "parsing", "macros", "large-dates"] }
time = { version = "0.3.9", features = ["formatting", "parsing", "macros"] }
bitvec = "1.0.1"
serde_path_to_error = { version = "0.1.16", optional = true }

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ Note that if you are using `bson` through the `mongodb` crate, you do not need t
| `time-0_3` | Enable support for v0.3 of the [`time`](https://docs.rs/time/0.3) crate in the public API. | n/a | no |
| `serde_with-3` | Enable [`serde_with`](https://docs.rs/serde_with/3.x) 3.x integrations for `bson::DateTime` and `bson::Uuid`.| serde_with | no |
| `serde_path_to_error` | Enable support for error paths via integration with [`serde_path_to_error`](https://docs.rs/serde_path_to_err/latest). This is an unstable feature and any breaking changes to `serde_path_to_error` may affect usage of it via this feature. | serde_path_to_error | no |
| `compat-3-0-0` | Required for future compatibility if default features are disabled. | no |
| `compat-3-0-0` | Required for future compatibility if default features are disabled. | n/a | no |
| `large_dates` | Increase the supported year range for some `bson::DateTime` utilities from +/-9,999 (inclusive) to +/-999,999 (inclusive). Note that enabling this feature can impact performance and introduce parsing ambiguities. | n/a | no |

## Overview of the BSON Format

Expand Down
8 changes: 8 additions & 0 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ use serde::{Deserialize, Deserializer, Serialize};
/// # }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// ## Large Dates
/// The range of dates supported by `DateTime` is defined by [`DateTime::MIN`] and
/// [`DateTime::MAX`]. However, some utilities for constructing and converting `DateTimes`, such as
/// interop with the [`time::OffsetDateTime`] type and with RFC 3339 strings, are bounded by the
/// [`time`] crate's supported date range. The `large_dates` feature can be enabled to expand this
/// range, which enables the
/// [`large-dates` feature for `time`](https://docs.rs/time/latest/time/#feature-flags).
#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Copy, Clone)]
pub struct DateTime(i64);

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
//! | `serde_with-3` | Enable [`serde_with`](https://docs.rs/serde_with/3.x) 3.x integrations for [`DateTime`] and [`Uuid`]. | no |
//! | `serde_path_to_error` | Enable support for error paths via integration with [`serde_path_to_error`](https://docs.rs/serde_path_to_err/latest). This is an unstable feature and any breaking changes to `serde_path_to_error` may affect usage of it via this feature. | no |
//! | `compat-3-0-0` | Required for future compatibility if default features are disabled. | no |
//! | `large_dates` | Increase the supported year range for some `bson::DateTime` utilities from +/-9,999 (inclusive) to +/-999,999 (inclusive). Note that enabling this feature can impact performance and introduce parsing ambiguities. | no |
//!
//! ## BSON values
//!
Expand Down
1 change: 1 addition & 0 deletions src/tests/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ fn datetime_to_rfc3339() {
}

#[test]
#[cfg(feature = "large_dates")]
fn invalid_datetime_to_rfc3339() {
assert!(crate::DateTime::MAX.try_to_rfc3339_string().is_err());
}
Expand Down
5 changes: 5 additions & 0 deletions src/tests/spec/corpus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ where
fn run_test(test: TestFile) {
let _guard = LOCK.run_concurrently();
for valid in test.valid {
#[cfg(not(feature = "large_dates"))]
if valid.description == "Y10K" {
continue;
}

let description = format!("{}: {}", test.description, valid.description);

let canonical_bson = hex::decode(&valid.canonical_bson).expect(&description);
Expand Down