From 83ada6e4513cb17f7135dfe343aa539a1b00bac8 Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Sat, 28 Oct 2023 10:42:33 -0600 Subject: [PATCH] Split clock feature into clock and now --- .github/workflows/test.yml | 1 + Cargo.toml | 3 ++- README.md | 3 ++- src/lib.rs | 17 +++++++++++++---- src/offset/utc.rs | 6 +++--- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a096504d97..48c8832c1b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -87,6 +87,7 @@ jobs: - run: cargo test --no-default-features --features=alloc - run: cargo test --no-default-features --features=unstable-locales - run: cargo test --no-default-features --features=alloc,unstable-locales + - run: cargo test --no-default-features --features=now no_std: strategy: diff --git a/Cargo.toml b/Cargo.toml index c6edb2a311..57ca5e2f55 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,8 @@ alloc = [] libc = [] winapi = ["windows-targets"] std = ["alloc"] -clock = ["std", "winapi", "iana-time-zone", "android-tzdata"] +clock = ["winapi", "iana-time-zone", "android-tzdata", "now"] +now = ["std"] oldtime = [] wasmbind = ["wasm-bindgen", "js-sys"] unstable-locales = ["pure-rust-locales"] diff --git a/README.md b/README.md index c3222ddaa3..a27371555c 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,8 @@ Default features: * `alloc`: Enable features that depend on allocation (primarily string formatting) * `std`: Enables functionality that depends on the standard library. This is a superset of `alloc` and adds interoperation with standard library types and traits. -* `clock`: Enables reading the system time (`now`) and local timezone (`Local`). +* `clock`: Enables reading the local timezone (`Local`). This is a superset of `now`. +* `now`: Enables reading the system time (`now`) * `wasmbind`: Interface with the JS Date API for the `wasm32` target. Optional features: diff --git a/src/lib.rs b/src/lib.rs index da8e00d17a..8d7c64e403 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -108,21 +108,28 @@ //! or in the local time zone //! ([`Local::now()`](./offset/struct.Local.html#method.now)). //! +#![cfg_attr(not(feature = "now"), doc = "```ignore")] +#![cfg_attr(feature = "now", doc = "```rust")] +//! use chrono::prelude::*; +//! +//! let utc: DateTime = Utc::now(); // e.g. `2014-11-28T12:45:59.324310806Z` +//! # let _ = utc; +//! ``` +//! #![cfg_attr(not(feature = "clock"), doc = "```ignore")] #![cfg_attr(feature = "clock", doc = "```rust")] //! use chrono::prelude::*; //! -//! let utc: DateTime = Utc::now(); // e.g. `2014-11-28T12:45:59.324310806Z` //! let local: DateTime = Local::now(); // e.g. `2014-11-28T21:45:59.324310806+09:00` -//! # let _ = utc; let _ = local; +//! # let _ = local; //! ``` //! //! Alternatively, you can create your own date and time. //! This is a bit verbose due to Rust's lack of function and method overloading, //! but in turn we get a rich combination of initialization methods. //! -#![cfg_attr(not(feature = "std"), doc = "```ignore")] -#![cfg_attr(feature = "std", doc = "```rust")] +#![cfg_attr(not(feature = "now"), doc = "```ignore")] +#![cfg_attr(feature = "now", doc = "```rust")] //! use chrono::prelude::*; //! use chrono::offset::LocalResult; //! @@ -146,12 +153,14 @@ //! assert_eq!(Utc.with_ymd_and_hms(2014, 7, 8, 80, 15, 33), LocalResult::None); //! assert_eq!(Utc.with_ymd_and_hms(2014, 7, 38, 21, 15, 33), LocalResult::None); //! +//! # #[cfg(feature = "clock")] { //! // other time zone objects can be used to construct a local datetime. //! // obviously, `local_dt` is normally different from `dt`, but `fixed_dt` should be identical. //! let local_dt = Local.from_local_datetime(&NaiveDate::from_ymd_opt(2014, 7, 8).unwrap().and_hms_milli_opt(9, 10, 11, 12).unwrap()).unwrap(); //! let fixed_dt = FixedOffset::east_opt(9 * 3600).unwrap().from_local_datetime(&NaiveDate::from_ymd_opt(2014, 7, 8).unwrap().and_hms_milli_opt(18, 10, 11, 12).unwrap()).unwrap(); //! assert_eq!(dt, fixed_dt); //! # let _ = local_dt; +//! # } //! # Some(()) //! # } //! # doctest().unwrap(); diff --git a/src/offset/utc.rs b/src/offset/utc.rs index 11e9dad931..7c23a282bb 100644 --- a/src/offset/utc.rs +++ b/src/offset/utc.rs @@ -5,7 +5,7 @@ use core::fmt; #[cfg(all( - feature = "clock", + feature = "now", not(all( target_arch = "wasm32", feature = "wasmbind", @@ -19,7 +19,7 @@ use rkyv::{Archive, Deserialize, Serialize}; use super::{FixedOffset, LocalResult, Offset, TimeZone}; use crate::naive::{NaiveDate, NaiveDateTime}; -#[cfg(feature = "clock")] +#[cfg(feature = "now")] #[allow(deprecated)] use crate::{Date, DateTime}; @@ -51,7 +51,7 @@ use crate::{Date, DateTime}; #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] pub struct Utc; -#[cfg(feature = "clock")] +#[cfg(feature = "now")] impl Utc { /// Returns a `Date` which corresponds to the current date. #[deprecated(