Skip to content

Commit

Permalink
Don't require time crate for clock feature (chronotope#478)
Browse files Browse the repository at this point in the history
Absorb just enough of the time crate that it is no longer required for
the clock feature. v0.1 of the time crate is long deprecated, and v0.2 of
the crate is a complete rewrite.

Vendoring v0.1 allows chrono to control its own destiny. It also means
that downstream users that have upgraded to the time v0.2 ecosystem do
not wind up with both time v0.1 and v0.2 in the dependency tree.

Even with this patch, the dependency on the old time crate remains by
default for backwards compatibility. Specifically, the
`chrono::Duration` type is a re-export of the `time::Duration` type when
the `oldtime` feature is enabled, as it is by default. The intent is
that the `oldtime` feature will be removed when chrono v0.5 is released.

Supersedes chronotope#286.
Fixes chronotope#400.
  • Loading branch information
benesch authored and pickfire committed Jul 5, 2022
1 parent 41ee924 commit b605a21
Show file tree
Hide file tree
Showing 13 changed files with 564 additions and 119 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ Versions with only mechanical changes will be omitted from the following list.
* Add %Z specifier to the `FromStr`, similar to the glibc strptime
(does not set the offset from the timezone name)

* Drop the dependency on time v0.1, which is deprecated, unless the `oldtime`
feature is active. This feature is active by default in v0.4.16 for backwards
compatibility, but will likely be removed in v0.5. Code that imports
`time::Duration` should be switched to import `chrono::Duration` instead to
avoid breakage.

## 0.4.15

### Fixes
Expand Down
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ appveyor = { repository = "chronotope/chrono" }
name = "chrono"

[features]
default = ["clock", "std"]
default = ["clock", "std", "oldtime"]
alloc = []
std = []
clock = ["time", "std"]
clock = ["libc", "std", "winapi"]
oldtime = ["time"]
wasmbind = ["wasm-bindgen", "js-sys"]
unstable-locales = ["pure-rust-locales", "alloc"]
__internal_bench = []
__doctest = []

[dependencies]
libc = { version = "0.2.69", optional = true }
time = { version = "0.1.43", optional = true }
num-integer = { version = "0.1.36", default-features = false }
num-traits = { version = "0.2", default-features = false }
Expand All @@ -45,6 +47,9 @@ pure-rust-locales = { version = "0.5.2", optional = true }
wasm-bindgen = { version = "0.2", optional = true }
js-sys = { version = "0.3", optional = true } # contains FFI bindings for the JS Date API

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.0", features = ["std", "minwinbase", "minwindef", "timezoneapi"], optional = true }

[dev-dependencies]
serde_json = { version = "1" }
serde_derive = { version = "1", default-features = false }
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,9 @@ Addition and subtraction is also supported.
The following illustrates most supported operations to the date and time:

```rust
extern crate time;

use chrono::prelude::*;
use time::Duration;
use chrono::Duration;

// assume this returned `2014-11-28T21:45:59.324310806+09:00`:
let dt = FixedOffset::east(9*3600).ymd(2014, 11, 28).and_hms_nano(21, 45, 59, 324310806);
Expand Down
18 changes: 14 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,10 @@
//!
//! ```rust
//! # extern crate chrono;
//! extern crate time;
//!
//! # fn main() {
//! use chrono::prelude::*;
//! use time::Duration;
//! use chrono::Duration;
//!
//! // assume this returned `2014-11-28T21:45:59.324310806+09:00`:
//! let dt = FixedOffset::east(9*3600).ymd(2014, 11, 28).and_hms_nano(21, 45, 59, 324310806);
Expand Down Expand Up @@ -438,10 +437,21 @@ extern crate std as alloc;
#[cfg(any(feature = "std", test))]
extern crate std as core;

#[cfg(feature = "clock")]
#[cfg(feature = "oldtime")]
extern crate time as oldtime;
#[cfg(not(feature = "clock"))]
#[cfg(not(feature = "oldtime"))]
mod oldtime;

#[cfg(feature = "clock")]
extern crate libc;
#[cfg(all(feature = "clock", windows))]
extern crate winapi;
#[cfg(all(
feature = "clock",
not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))
))]
mod sys;

extern crate num_integer;
extern crate num_traits;
#[cfg(feature = "rustc-serialize")]
Expand Down
30 changes: 12 additions & 18 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,10 +874,9 @@ impl NaiveDate {
/// # Example
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// use chrono::NaiveDate;
/// # extern crate chrono; fn main() {
/// use chrono::{Duration, NaiveDate};
/// use chrono::naive::MAX_DATE;
/// use time::Duration;
///
/// let d = NaiveDate::from_ymd(2015, 9, 5);
/// assert_eq!(d.checked_add_signed(Duration::days(40)),
Expand Down Expand Up @@ -909,10 +908,9 @@ impl NaiveDate {
/// # Example
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// use chrono::NaiveDate;
/// # extern crate chrono; fn main() {
/// use chrono::{Duration, NaiveDate};
/// use chrono::naive::MIN_DATE;
/// use time::Duration;
///
/// let d = NaiveDate::from_ymd(2015, 9, 5);
/// assert_eq!(d.checked_sub_signed(Duration::days(40)),
Expand Down Expand Up @@ -946,9 +944,8 @@ impl NaiveDate {
/// # Example
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// use chrono::NaiveDate;
/// use time::Duration;
/// # extern crate chrono; fn main() {
/// use chrono::{Duration, NaiveDate};
///
/// let from_ymd = NaiveDate::from_ymd;
/// let since = NaiveDate::signed_duration_since;
Expand Down Expand Up @@ -1453,9 +1450,8 @@ impl Datelike for NaiveDate {
/// # Example
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// use chrono::NaiveDate;
/// use time::Duration;
/// # extern crate chrono; fn main() {
/// use chrono::{Duration, NaiveDate};
///
/// let from_ymd = NaiveDate::from_ymd;
///
Expand Down Expand Up @@ -1495,9 +1491,8 @@ impl AddAssign<OldDuration> for NaiveDate {
/// # Example
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// use chrono::NaiveDate;
/// use time::Duration;
/// # extern crate chrono; fn main() {
/// use chrono::{Duration, NaiveDate};
///
/// let from_ymd = NaiveDate::from_ymd;
///
Expand Down Expand Up @@ -1539,9 +1534,8 @@ impl SubAssign<OldDuration> for NaiveDate {
/// # Example
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// use chrono::NaiveDate;
/// use time::Duration;
/// # extern crate chrono; fn main() {
/// use chrono::{Duration, NaiveDate};
///
/// let from_ymd = NaiveDate::from_ymd;
///
Expand Down
70 changes: 28 additions & 42 deletions src/naive/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,8 @@ impl NaiveDateTime {
/// # Example
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// use chrono::NaiveDate;
/// use time::Duration;
/// # extern crate chrono; fn main() {
/// use chrono::{Duration, NaiveDate};
///
/// let from_ymd = NaiveDate::from_ymd;
///
Expand All @@ -455,9 +454,8 @@ impl NaiveDateTime {
/// Overflow returns `None`.
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// # use chrono::NaiveDate;
/// # use time::Duration;
/// # extern crate chrono; fn main() {
/// # use chrono::{Duration, NaiveDate};
/// # let hms = |h, m, s| NaiveDate::from_ymd(2016, 7, 8).and_hms(h, m, s);
/// assert_eq!(hms(3, 5, 7).checked_add_signed(Duration::days(1_000_000_000)), None);
/// # }
Expand All @@ -467,9 +465,8 @@ impl NaiveDateTime {
/// but the addition assumes that it is the only leap second happened.
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// # use chrono::NaiveDate;
/// # use time::Duration;
/// # extern crate chrono; fn main() {
/// # use chrono::{Duration, NaiveDate};
/// # let from_ymd = NaiveDate::from_ymd;
/// # let hmsm = |h, m, s, milli| from_ymd(2016, 7, 8).and_hms_milli(h, m, s, milli);
/// let leap = hmsm(3, 5, 59, 1_300);
Expand Down Expand Up @@ -513,9 +510,8 @@ impl NaiveDateTime {
/// # Example
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// use chrono::NaiveDate;
/// use time::Duration;
/// # extern crate chrono; fn main() {
/// use chrono::{Duration, NaiveDate};
///
/// let from_ymd = NaiveDate::from_ymd;
///
Expand All @@ -541,9 +537,8 @@ impl NaiveDateTime {
/// Overflow returns `None`.
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// # use chrono::NaiveDate;
/// # use time::Duration;
/// # extern crate chrono; fn main() {
/// # use chrono::{Duration, NaiveDate};
/// # let hms = |h, m, s| NaiveDate::from_ymd(2016, 7, 8).and_hms(h, m, s);
/// assert_eq!(hms(3, 5, 7).checked_sub_signed(Duration::days(1_000_000_000)), None);
/// # }
Expand All @@ -553,9 +548,8 @@ impl NaiveDateTime {
/// but the subtraction assumes that it is the only leap second happened.
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// # use chrono::NaiveDate;
/// # use time::Duration;
/// # extern crate chrono; fn main() {
/// # use chrono::{Duration, NaiveDate};
/// # let from_ymd = NaiveDate::from_ymd;
/// # let hmsm = |h, m, s, milli| from_ymd(2016, 7, 8).and_hms_milli(h, m, s, milli);
/// let leap = hmsm(3, 5, 59, 1_300);
Expand Down Expand Up @@ -595,9 +589,8 @@ impl NaiveDateTime {
/// # Example
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// use chrono::NaiveDate;
/// use time::Duration;
/// # extern crate chrono; fn main() {
/// use chrono::{Duration, NaiveDate};
///
/// let from_ymd = NaiveDate::from_ymd;
///
Expand All @@ -616,9 +609,8 @@ impl NaiveDateTime {
/// there were no other leap seconds happened.
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// # use chrono::NaiveDate;
/// # use time::Duration;
/// # extern crate chrono; fn main() {
/// # use chrono::{Duration, NaiveDate};
/// # let from_ymd = NaiveDate::from_ymd;
/// let leap = from_ymd(2015, 6, 30).and_hms_milli(23, 59, 59, 1_500);
/// assert_eq!(leap.signed_duration_since(from_ymd(2015, 6, 30).and_hms(23, 0, 0)),
Expand Down Expand Up @@ -1217,9 +1209,8 @@ impl hash::Hash for NaiveDateTime {
/// # Example
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// use chrono::NaiveDate;
/// use time::Duration;
/// # extern crate chrono; fn main() {
/// use chrono::{Duration, NaiveDate};
///
/// let from_ymd = NaiveDate::from_ymd;
///
Expand All @@ -1243,9 +1234,8 @@ impl hash::Hash for NaiveDateTime {
/// but the addition assumes that it is the only leap second happened.
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// # use chrono::NaiveDate;
/// # use time::Duration;
/// # extern crate chrono; fn main() {
/// # use chrono::{Duration, NaiveDate};
/// # let from_ymd = NaiveDate::from_ymd;
/// # let hmsm = |h, m, s, milli| from_ymd(2016, 7, 8).and_hms_milli(h, m, s, milli);
/// let leap = hmsm(3, 5, 59, 1_300);
Expand Down Expand Up @@ -1289,9 +1279,8 @@ impl AddAssign<OldDuration> for NaiveDateTime {
/// # Example
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// use chrono::NaiveDate;
/// use time::Duration;
/// # extern crate chrono; fn main() {
/// use chrono::{Duration, NaiveDate};
///
/// let from_ymd = NaiveDate::from_ymd;
///
Expand All @@ -1315,9 +1304,8 @@ impl AddAssign<OldDuration> for NaiveDateTime {
/// but the subtraction assumes that it is the only leap second happened.
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// # use chrono::NaiveDate;
/// # use time::Duration;
/// # extern crate chrono; fn main() {
/// # use chrono::{Duration, NaiveDate};
/// # let from_ymd = NaiveDate::from_ymd;
/// # let hmsm = |h, m, s, milli| from_ymd(2016, 7, 8).and_hms_milli(h, m, s, milli);
/// let leap = hmsm(3, 5, 59, 1_300);
Expand Down Expand Up @@ -1360,9 +1348,8 @@ impl SubAssign<OldDuration> for NaiveDateTime {
/// # Example
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// use chrono::NaiveDate;
/// use time::Duration;
/// # extern crate chrono; fn main() {
/// use chrono::{Duration, NaiveDate};
///
/// let from_ymd = NaiveDate::from_ymd;
///
Expand All @@ -1380,9 +1367,8 @@ impl SubAssign<OldDuration> for NaiveDateTime {
/// there were no other leap seconds happened.
///
/// ~~~~
/// # extern crate chrono; extern crate time; fn main() {
/// # use chrono::NaiveDate;
/// # use time::Duration;
/// # extern crate chrono; fn main() {
/// # use chrono::{Duration, NaiveDate};
/// # let from_ymd = NaiveDate::from_ymd;
/// let leap = from_ymd(2015, 6, 30).and_hms_milli(23, 59, 59, 1_500);
/// assert_eq!(leap - from_ymd(2015, 6, 30).and_hms(23, 0, 0),
Expand Down
Loading

0 comments on commit b605a21

Please sign in to comment.