Skip to content

Commit

Permalink
Use different method to run feature-dependent doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 22, 2024
1 parent 61d54b5 commit 6f2c7cc
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 28 deletions.
10 changes: 6 additions & 4 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ impl<Tz: TimeZone> DateTime<Tz> {
///
/// # Example
///
#[cfg_attr(not(feature = "clock"), doc = "```ignore")]
#[cfg_attr(feature = "clock", doc = "```rust")]
/// ```
/// # #[cfg(feature = "clock")] {
/// use chrono::{DateTime, Local};
///
/// let dt = Local::now();
Expand All @@ -90,6 +90,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// // Serialize, pass through FFI... and recreate the `DateTime`:
/// let dt_new = DateTime::<Local>::from_naive_utc_and_offset(naive_utc, offset);
/// assert_eq!(dt, dt_new);
/// # }
/// ```
#[inline]
#[must_use]
Expand Down Expand Up @@ -691,8 +692,8 @@ impl<Tz: TimeZone> DateTime<Tz> {
///
/// # Example
///
#[cfg_attr(not(feature = "clock"), doc = "```ignore")]
#[cfg_attr(feature = "clock", doc = "```rust")]
/// ```
/// # #[cfg(feature = "clock")] {
/// use chrono::{Local, NaiveTime};
///
/// let noon = NaiveTime::from_hms_opt(12, 0, 0).unwrap();
Expand All @@ -701,6 +702,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
///
/// assert_eq!(today_noon.single().unwrap().time(), noon);
/// assert_eq!(today_midnight.single().unwrap().time(), NaiveTime::MIN);
/// # }
/// ```
#[must_use]
pub fn with_time(&self, time: NaiveTime) -> LocalResult<Self> {
Expand Down
5 changes: 3 additions & 2 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
//! C's `strftime` format. The available options can be found [here](./strftime/index.html).
//!
//! # Example
#![cfg_attr(not(feature = "std"), doc = "```ignore")]
#![cfg_attr(feature = "std", doc = "```rust")]
//! ```
//! # #[cfg(feature = "std")] {
//! use chrono::{NaiveDateTime, TimeZone, Utc};
//!
//! let date_time = Utc.with_ymd_and_hms(2020, 11, 10, 0, 1, 32).unwrap();
Expand All @@ -27,6 +27,7 @@
//!
//! let parsed = NaiveDateTime::parse_from_str(&formatted, "%Y-%m-%d %H:%M:%S")?.and_utc();
//! assert_eq!(parsed, date_time);
//! # }
//! # Ok::<(), chrono::ParseError>(())
//! ```
Expand Down
10 changes: 6 additions & 4 deletions src/format/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ use crate::{DateTime, Datelike, TimeDelta, Timelike, Weekday};
///
/// Let's see how `Parsed` correctly detects the second RFC 2822 string from before is inconsistent.
///
#[cfg_attr(not(feature = "alloc"), doc = "```ignore")]
#[cfg_attr(feature = "alloc", doc = "```rust")]
/// ```
/// # #[cfg(feature = "alloc")] {
/// use chrono::format::{ParseErrorKind, Parsed};
/// use chrono::Weekday;
///
Expand Down Expand Up @@ -89,6 +89,7 @@ use crate::{DateTime, Datelike, TimeDelta, Timelike, Weekday};
/// if let Err(error) = result {
/// assert_eq!(error.kind(), ParseErrorKind::Impossible);
/// }
/// # }
/// # Ok::<(), chrono::ParseError>(())
/// ```
///
Expand All @@ -98,8 +99,8 @@ use crate::{DateTime, Datelike, TimeDelta, Timelike, Weekday};
/// [RFC2822 formatting item]: crate::format::Fixed::RFC2822
/// [`format::parse()`]: crate::format::parse()
///
#[cfg_attr(not(feature = "alloc"), doc = "```ignore")]
#[cfg_attr(feature = "alloc", doc = "```rust")]
/// ```
/// # #[cfg(feature = "alloc")] {
/// use chrono::format::{parse, Fixed, Item, Parsed};
/// use chrono::Weekday;
///
Expand All @@ -120,6 +121,7 @@ use crate::{DateTime, Datelike, TimeDelta, Timelike, Weekday};
/// // What is the weekday?
/// assert_eq!(parsed.weekday(), Some(Weekday::Thu));
/// }
/// # }
/// # Ok::<(), chrono::ParseError>(())
/// ```
#[allow(clippy::manual_non_exhaustive)]
Expand Down
4 changes: 2 additions & 2 deletions src/format/strftime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ impl<'a> StrftimeItems<'a> {
///
/// # Example
///
#[cfg_attr(not(any(feature = "alloc", feature = "std")), doc = "```ignore")]
#[cfg_attr(any(feature = "alloc", feature = "std"), doc = "```rust")]
/// ```
/// # #[cfg(feature = "alloc")] {
/// use chrono::format::{Locale, StrftimeItems};
/// use chrono::{FixedOffset, TimeZone};
///
Expand Down
15 changes: 9 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,22 @@
//! 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")]
//! ```
//! # #[cfg(feature = "now")] {
//! use chrono::prelude::*;
//!
//! let utc: DateTime<Utc> = 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")]
//! ```
//! # #[cfg(feature = "clock")] {
//! use chrono::prelude::*;
//!
//! let local: DateTime<Local> = Local::now(); // e.g. `2014-11-28T21:45:59.324310806+09:00`
//! # let _ = local;
//! # }
//! ```
//!
//! Alternatively, you can create your own date and time.
Expand Down Expand Up @@ -382,8 +384,8 @@
//! [`DateTime.timestamp_subsec_nanos`](DateTime::timestamp_subsec_nanos)
//! to get the number of additional number of nanoseconds.
//!
#![cfg_attr(not(feature = "std"), doc = "```ignore")]
#![cfg_attr(feature = "std", doc = "```rust")]
//! ```
//! # #[cfg(feature = "std")] {
//! // We need the trait in scope to use Utc::timestamp().
//! use chrono::{DateTime, Utc};
//!
Expand All @@ -394,6 +396,7 @@
//! // Get epoch value from a datetime:
//! let dt = DateTime::parse_from_rfc2822("Fri, 14 Jul 2017 02:40:00 +0000").unwrap();
//! assert_eq!(dt.timestamp(), 1_500_000_000);
//! # }
//! ```
//!
//! ### Naive date and time
Expand Down
10 changes: 6 additions & 4 deletions src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,12 +948,13 @@ impl Timelike for NaiveTime {
/// ([Why?](#leap-second-handling))
/// Use the proper [formatting method](#method.format) to get a human-readable representation.
///
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
#[cfg_attr(feature = "std", doc = "```")]
/// ```
/// # #[cfg(feature = "std")] {
/// # use chrono::{NaiveTime, Timelike};
/// let leap = NaiveTime::from_hms_milli_opt(23, 59, 59, 1_000).unwrap();
/// assert_eq!(leap.second(), 59);
/// assert_eq!(leap.format("%H:%M:%S").to_string(), "23:59:60");
/// # }
/// ```
#[inline]
fn second(&self) -> u32 {
Expand All @@ -980,12 +981,13 @@ impl Timelike for NaiveTime {
/// You can reduce the range with `time.nanosecond() % 1_000_000_000`, or
/// use the proper [formatting method](#method.format) to get a human-readable representation.
///
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
#[cfg_attr(feature = "std", doc = "```")]
/// ```
/// # #[cfg(feature = "std")] {
/// # use chrono::{NaiveTime, Timelike};
/// let leap = NaiveTime::from_hms_milli_opt(23, 59, 59, 1_000).unwrap();
/// assert_eq!(leap.nanosecond(), 1_000_000_000);
/// assert_eq!(leap.format("%H:%M:%S%.9f").to_string(), "23:59:60.000000000");
/// # }
/// ```
#[inline]
fn nanosecond(&self) -> u32 {
Expand Down
10 changes: 6 additions & 4 deletions src/offset/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ impl FixedOffset {
///
/// # Example
///
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
#[cfg_attr(feature = "std", doc = "```")]
/// ```
/// # #[cfg(feature = "std")] {
/// use chrono::{FixedOffset, TimeZone};
/// let hour = 3600;
/// let datetime =
/// FixedOffset::east_opt(5 * hour).unwrap().with_ymd_and_hms(2016, 11, 08, 0, 0, 0).unwrap();
/// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00+05:00")
/// # }
/// ```
#[must_use]
pub const fn east_opt(secs: i32) -> Option<FixedOffset> {
Expand Down Expand Up @@ -83,13 +84,14 @@ impl FixedOffset {
///
/// # Example
///
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
#[cfg_attr(feature = "std", doc = "```")]
/// ```
/// # #[cfg(feature = "std")] {
/// use chrono::{FixedOffset, TimeZone};
/// let hour = 3600;
/// let datetime =
/// FixedOffset::west_opt(5 * hour).unwrap().with_ymd_and_hms(2016, 11, 08, 0, 0, 0).unwrap();
/// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00-05:00")
/// # }
/// ```
#[must_use]
pub const fn west_opt(secs: i32) -> Option<FixedOffset> {
Expand Down
5 changes: 3 additions & 2 deletions src/weekday.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,16 @@ impl Weekday {
///
/// # Example
///
#[cfg_attr(not(feature = "clock"), doc = "```ignore")]
#[cfg_attr(feature = "clock", doc = "```rust")]
/// ```
/// # #[cfg(feature = "clock")] {
/// # use chrono::{Local, Datelike};
/// // MTWRFSU is occasionally used as a single-letter abbreviation of the weekdays.
/// // Use `num_days_from_monday` to index into the array.
/// const MTWRFSU: [char; 7] = ['M', 'T', 'W', 'R', 'F', 'S', 'U'];
///
/// let today = Local::now().weekday();
/// println!("{}", MTWRFSU[today.num_days_from_monday() as usize]);
/// # }
/// ```
#[inline]
pub const fn num_days_from_monday(&self) -> u32 {
Expand Down

0 comments on commit 6f2c7cc

Please sign in to comment.