Skip to content

Commit

Permalink
Deprecate old formatting methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Jul 22, 2023
1 parent 4d655ca commit d4b1ef9
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 211 deletions.
2 changes: 2 additions & 0 deletions benches/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,15 @@ fn bench_format_with(c: &mut Criterion) {
});
}

#[allow(deprecated)]
fn bench_format(c: &mut Criterion) {
let dt = Local::now();
c.bench_function("bench_format", |b| {
b.iter(|| format!("{}", black_box(dt).format("%Y-%m-%dT%H:%M:%S%.f%:z")))
});
}

#[allow(deprecated)]
fn bench_format_with_items(c: &mut Criterion) {
let dt = Local::now();
let items: Vec<_> = StrftimeItems::new("%Y-%m-%dT%H:%M:%S%.f%:z").collect();
Expand Down
66 changes: 18 additions & 48 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ where
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[inline]
#[must_use]
#[deprecated(since = "0.4.27", note = "Use DateTime::format_with() instead")]
pub fn format_with_items<'a, I, B>(&self, items: I) -> DelayedFormat<I>
where
I: Iterator<Item = B> + Clone,
Expand All @@ -878,64 +879,33 @@ where
DelayedFormat::new_with_offset(Some(local.date()), Some(local.time()), &self.offset, items)
}

/// Formats the combined date and time per the specified format string.
/// Formats the date and time with the specified format string.
///
/// See the [`crate::format::strftime`] module for the supported escape sequences.
/// # Deprecated
///
/// # Example
/// ```rust
/// use chrono::prelude::*;
/// Use [`format_to_string`](#method.format_to_string) or [`format_with`](#method.format_with)
/// instead.
///
/// let date_time: DateTime<Utc> = Utc.with_ymd_and_hms(2017, 04, 02, 12, 50, 32).unwrap();
/// let formatted = format!("{}", date_time.format("%d/%m/%Y %H:%M"));
/// assert_eq!(formatted, "02/04/2017 12:50");
/// ```
/// # Errors/panics
///
/// The `Display` implementation of the returned `DelayedFormat` can return an error if the
/// format string is invalid. This goes against the [contract for `Display`][1], and causes a
/// panic when used in combination with [`to_string`], [`println!`] and [`format!`].
///
/// [1]: https://doc.rust-lang.org/stable/std/fmt/index.html#formatting-traits
/// [`to_string`]: ToString::to_string
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[inline]
#[must_use]
#[deprecated(
since = "0.4.27",
note = "Use DateTime::format_to_string() or DateTime::format_with() instead"
)]
pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>> {
#[allow(deprecated)]
self.format_with_items(StrftimeItems::new(fmt))
}

/// Formats the combined date and time with the specified formatting items and locale.
#[cfg(all(feature = "unstable-locales", any(feature = "alloc", feature = "std")))]
#[inline]
#[must_use]
pub fn format_localized_with_items<'a, I, B>(
&self,
items: I,
locale: Locale,
) -> DelayedFormat<I>
where
I: Iterator<Item = B> + Clone,
B: Borrow<Item<'a>>,
{
let local = self.naive_local();
DelayedFormat::new_with_offset_and_locale(
Some(local.date()),
Some(local.time()),
&self.offset,
items,
locale,
)
}

/// Formats the combined date and time per the specified format string and
/// locale.
///
/// See the [`crate::format::strftime`] module on the supported escape
/// sequences.
#[cfg(all(feature = "unstable-locales", any(feature = "alloc", feature = "std")))]
#[inline]
#[must_use]
pub fn format_localized<'a>(
&self,
fmt: &'a str,
locale: Locale,
) -> DelayedFormat<StrftimeItems<'a>> {
self.format_localized_with_items(StrftimeItems::new_with_locale(fmt, locale), locale)
}
}

impl DateTime<Utc> {
Expand Down
64 changes: 12 additions & 52 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1329,36 +1329,11 @@ impl NaiveDate {
}

/// Formats the date with the specified formatting items.
/// Otherwise it is the same as the ordinary `format` method.
///
/// The `Iterator` of items should be `Clone`able,
/// since the resulting `DelayedFormat` value may be formatted multiple times.
///
/// # Example
///
/// ```
/// use chrono::NaiveDate;
/// use chrono::format::strftime::StrftimeItems;
///
/// let fmt = StrftimeItems::new("%Y-%m-%d");
/// let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap();
/// assert_eq!(d.format_with_items(fmt.clone()).to_string(), "2015-09-05");
/// assert_eq!(d.format("%Y-%m-%d").to_string(), "2015-09-05");
/// ```
///
/// The resulting `DelayedFormat` can be formatted directly via the `Display` trait.
///
/// ```
/// # use chrono::NaiveDate;
/// # use chrono::format::strftime::StrftimeItems;
/// # let fmt = StrftimeItems::new("%Y-%m-%d").clone();
/// # let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap();
/// assert_eq!(format!("{}", d.format_with_items(fmt)), "2015-09-05");
/// ```
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[inline]
#[must_use]
#[deprecated(since = "0.4.27", note = "Use NaiveDate::format_with() instead")]
pub fn format_with_items<'a, I, B>(&self, items: I) -> DelayedFormat<I>
where
I: Iterator<Item = B> + Clone,
Expand All @@ -1368,42 +1343,27 @@ impl NaiveDate {
}

/// Formats the date with the specified format string.
/// See the [`format::strftime` module](../format/strftime/index.html)
/// on the supported escape sequences.
///
/// This returns a `DelayedFormat`,
/// which gets converted to a string only when actual formatting happens.
/// You may use the `to_string` method to get a `String`,
/// or just feed it into `print!` and other formatting macros.
/// (In this way it avoids the redundant memory allocation.)
/// # Deprecated
///
/// A wrong format string does *not* issue an error immediately.
/// Rather, converting or formatting the `DelayedFormat` fails.
/// You are recommended to immediately use `DelayedFormat` for this reason.
///
/// # Example
///
/// ```
/// use chrono::NaiveDate;
/// Use [`format_to_string`](#method.format_to_string) or [`format_with`](#method.format_with)
/// instead.
///
/// let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap();
/// assert_eq!(d.format("%Y-%m-%d").to_string(), "2015-09-05");
/// assert_eq!(d.format("%A, %-d %B, %C%y").to_string(), "Saturday, 5 September, 2015");
/// ```
/// # Errors/panics
///
/// The resulting `DelayedFormat` can be formatted directly via the `Display` trait.
/// The `Display` implementation of the returned `DelayedFormat` can return an error if the
/// format string is invalid. This goes against the [contract for `Display`][1], and causes a
/// panic when used in combination with [`to_string`], [`println!`] and [`format!`].
///
/// ```
/// # use chrono::NaiveDate;
/// # let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap();
/// assert_eq!(format!("{}", d.format("%Y-%m-%d")), "2015-09-05");
/// assert_eq!(format!("{}", d.format("%A, %-d %B, %C%y")), "Saturday, 5 September, 2015");
/// ```
/// [1]: https://doc.rust-lang.org/stable/std/fmt/index.html#formatting-traits
/// [`to_string`]: ToString::to_string
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[inline]
#[must_use]
#[deprecated(since = "0.4.27", note = "Use DateTime::format_to_string() instead")]
pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>> {
#[allow(deprecated)]
self.format_with_items(StrftimeItems::new(fmt))
}

Expand Down
66 changes: 13 additions & 53 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,36 +912,11 @@ impl NaiveDateTime {
}

/// Formats the combined date and time with the specified formatting items.
/// Otherwise it is the same as the ordinary [`format`](#method.format) method.
///
/// The `Iterator` of items should be `Clone`able,
/// since the resulting `DelayedFormat` value may be formatted multiple times.
///
/// # Example
///
/// ```
/// use chrono::NaiveDate;
/// use chrono::format::strftime::StrftimeItems;
///
/// let fmt = StrftimeItems::new("%Y-%m-%d %H:%M:%S");
/// let dt = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap().and_hms_opt(23, 56, 4).unwrap();
/// assert_eq!(dt.format_with_items(fmt.clone()).to_string(), "2015-09-05 23:56:04");
/// assert_eq!(dt.format("%Y-%m-%d %H:%M:%S").to_string(), "2015-09-05 23:56:04");
/// ```
///
/// The resulting `DelayedFormat` can be formatted directly via the `Display` trait.
///
/// ```
/// # use chrono::NaiveDate;
/// # use chrono::format::strftime::StrftimeItems;
/// # let fmt = StrftimeItems::new("%Y-%m-%d %H:%M:%S").clone();
/// # let dt = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap().and_hms_opt(23, 56, 4).unwrap();
/// assert_eq!(format!("{}", dt.format_with_items(fmt)), "2015-09-05 23:56:04");
/// ```
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[inline]
#[must_use]
#[deprecated(since = "0.4.27", note = "Use NaiveDateTime::format_with() instead")]
pub fn format_with_items<'a, I, B>(&self, items: I) -> DelayedFormat<I>
where
I: Iterator<Item = B> + Clone,
Expand All @@ -950,43 +925,28 @@ impl NaiveDateTime {
DelayedFormat::new(Some(self.date), Some(self.time), items)
}

/// Formats the combined date and time with the specified format string.
/// See the [`format::strftime` module](../format/strftime/index.html)
/// on the supported escape sequences.
/// Formats the date and time with the specified format string.
///
/// This returns a `DelayedFormat`,
/// which gets converted to a string only when actual formatting happens.
/// You may use the `to_string` method to get a `String`,
/// or just feed it into `print!` and other formatting macros.
/// (In this way it avoids the redundant memory allocation.)
/// # Deprecated
///
/// A wrong format string does *not* issue an error immediately.
/// Rather, converting or formatting the `DelayedFormat` fails.
/// You are recommended to immediately use `DelayedFormat` for this reason.
/// Use [`format_to_string`](#method.format_to_string) or [`format_with`](#method.format_with)
/// instead.
///
/// # Example
/// # Errors/panics
///
/// ```
/// use chrono::NaiveDate;
/// The `Display` implementation of the returned `DelayedFormat` can return an error if the
/// format string is invalid. This goes against the [contract for `Display`][1], and causes a
/// panic when used in combination with [`to_string`], [`println!`] and [`format!`].
///
/// let dt = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap().and_hms_opt(23, 56, 4).unwrap();
/// assert_eq!(dt.format("%Y-%m-%d %H:%M:%S").to_string(), "2015-09-05 23:56:04");
/// assert_eq!(dt.format("around %l %p on %b %-d").to_string(), "around 11 PM on Sep 5");
/// ```
///
/// The resulting `DelayedFormat` can be formatted directly via the `Display` trait.
///
/// ```
/// # use chrono::NaiveDate;
/// # let dt = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap().and_hms_opt(23, 56, 4).unwrap();
/// assert_eq!(format!("{}", dt.format("%Y-%m-%d %H:%M:%S")), "2015-09-05 23:56:04");
/// assert_eq!(format!("{}", dt.format("around %l %p on %b %-d")), "around 11 PM on Sep 5");
/// ```
/// [1]: https://doc.rust-lang.org/stable/std/fmt/index.html#formatting-traits
/// [`to_string`]: ToString::to_string
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[inline]
#[must_use]
#[deprecated(since = "0.4.27", note = "Use NaiveDateTime::format_to_string() instead")]
pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>> {
#[allow(deprecated)]
self.format_with_items(StrftimeItems::new(fmt))
}

Expand Down
70 changes: 12 additions & 58 deletions src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,40 +830,11 @@ impl NaiveTime {
}

/// Formats the time with the specified formatting items.
/// Otherwise it is the same as the ordinary [`format`](#method.format) method.
///
/// The `Iterator` of items should be `Clone`able,
/// since the resulting `DelayedFormat` value may be formatted multiple times.
///
/// # Example
///
/// ```
/// use chrono::NaiveTime;
/// use chrono::format::strftime::StrftimeItems;
///
/// let items = StrftimeItems::new("%H:%M:%S").parse()?;
/// let fmt = NaiveTime::formatter(&items)?;
/// let t = NaiveTime::from_hms_opt(23, 56, 4).unwrap();
/// assert_eq!(t.format_with(&fmt).to_string(), "23:56:04");
/// assert_eq!(t.format_to_string("%H:%M:%S")?, "23:56:04");
/// # Ok::<(), chrono::ParseError>(())
/// ```
///
/// The resulting `DelayedFormat` can be formatted directly via the `Display` trait.
///
/// ```
/// # use chrono::NaiveTime;
/// # use chrono::format::strftime::StrftimeItems;
/// # let items = StrftimeItems::new("%H:%M:%S").parse()?;
/// # let fmt = NaiveTime::formatter(&items)?;
/// # let t = NaiveTime::from_hms_opt(23, 56, 4).unwrap();
/// assert_eq!(format!("{}", t.format_with(&fmt)), "23:56:04");
/// # Ok::<(), chrono::ParseError>(())
/// ```
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[inline]
#[must_use]
#[deprecated(since = "0.4.27", note = "Use NaiveTime::format_with() instead")]
pub fn format_with_items<'a, I, B>(&self, items: I) -> DelayedFormat<I>
where
I: Iterator<Item = B> + Clone,
Expand All @@ -873,44 +844,27 @@ impl NaiveTime {
}

/// Formats the time with the specified format string.
/// See the [`format::strftime` module](../format/strftime/index.html)
/// on the supported escape sequences.
///
/// This returns a `DelayedFormat`,
/// which gets converted to a string only when actual formatting happens.
/// You may use the `to_string` method to get a `String`,
/// or just feed it into `print!` and other formatting macros.
/// (In this way it avoids the redundant memory allocation.)
/// # Deprecated
///
/// A wrong format string does *not* issue an error immediately.
/// Rather, converting or formatting the `DelayedFormat` fails.
/// You are recommended to immediately use `DelayedFormat` for this reason.
/// Use [`format_to_string`](#method.format_to_string) or [`format_with`](#method.format_with)
/// instead.
///
/// # Example
/// # Errors/panics
///
/// ```
/// use chrono::NaiveTime;
///
/// let t = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
/// assert_eq!(t.format("%H:%M:%S").to_string(), "23:56:04");
/// assert_eq!(t.format("%H:%M:%S%.6f").to_string(), "23:56:04.012345");
/// assert_eq!(t.format("%-I:%M %p").to_string(), "11:56 PM");
/// ```
/// The `Display` implementation of the returned `DelayedFormat` can return an error if the
/// format string is invalid. This goes against the [contract for `Display`][1], and causes a
/// panic when used in combination with [`to_string`], [`println!`] and [`format!`].
///
/// The resulting `DelayedFormat` can be formatted directly via the `Display` trait.
///
/// ```
/// # use chrono::NaiveTime;
/// # let t = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
/// assert_eq!(format!("{}", t.format("%H:%M:%S")), "23:56:04");
/// assert_eq!(format!("{}", t.format("%H:%M:%S%.6f")), "23:56:04.012345");
/// assert_eq!(format!("{}", t.format("%-I:%M %p")), "11:56 PM");
/// ```
/// [1]: https://doc.rust-lang.org/stable/std/fmt/index.html#formatting-traits
/// [`to_string`]: ToString::to_string
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[inline]
#[must_use]
#[deprecated(since = "0.4.27", note = "Use DateTime::format_to_string() instead")]
pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>> {
#[allow(deprecated)]
self.format_with_items(StrftimeItems::new(fmt))
}

Expand Down

0 comments on commit d4b1ef9

Please sign in to comment.