use less intermediate formatting (-5% improvement)

This commit is contained in:
Conrad Ludgate 2022-10-17 10:33:35 +01:00 committed by Dirkjan Ochtman
parent b9241c0666
commit 64e082a5f8
4 changed files with 18 additions and 7 deletions

View File

@ -532,7 +532,8 @@ impl<Tz: TimeZone> Sub<Date<Tz>> for Date<Tz> {
impl<Tz: TimeZone> fmt::Debug for Date<Tz> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}{:?}", self.naive_local(), self.offset)
self.naive_local().fmt(f)?;
self.offset.fmt(f)
}
}
@ -541,7 +542,8 @@ where
Tz::Offset: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}{}", self.naive_local(), self.offset)
self.naive_local().fmt(f)?;
self.offset.fmt(f)
}
}

View File

@ -11,6 +11,7 @@ use alloc::string::{String, ToString};
#[cfg(any(feature = "alloc", feature = "std", test))]
use core::borrow::Borrow;
use core::cmp::Ordering;
use core::fmt::Write;
use core::ops::{Add, AddAssign, Sub, SubAssign};
use core::{fmt, hash, str};
#[cfg(feature = "std")]
@ -990,7 +991,8 @@ impl<Tz: TimeZone> Sub<Days> for DateTime<Tz> {
impl<Tz: TimeZone> fmt::Debug for DateTime<Tz> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}{:?}", self.naive_local(), self.offset)
self.naive_local().fmt(f)?;
self.offset.fmt(f)
}
}
@ -999,7 +1001,9 @@ where
Tz::Offset: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {}", self.naive_local(), self.offset)
self.naive_local().fmt(f)?;
f.write_char(' ')?;
self.offset.fmt(f)
}
}

View File

@ -737,7 +737,7 @@ fn format_inner<'a>(
if let (Some(d), Some(t), Some(&(_, off))) = (date, time, off) {
// reuse `Debug` impls which already print ISO 8601 format.
// this is faster in this way.
write!(result, "{:?}T{:?}", d, t)?;
write!(result, "{:?}", crate::NaiveDateTime::new(*d, *t))?;
Some(write_local_minus_utc(result, off, false, Colons::Single))
} else {
None

View File

@ -6,6 +6,7 @@
#[cfg(any(feature = "alloc", feature = "std", test))]
use core::borrow::Borrow;
use core::convert::TryFrom;
use core::fmt::Write;
use core::ops::{Add, AddAssign, Sub, SubAssign};
use core::{fmt, str};
@ -1629,7 +1630,9 @@ impl Sub<Days> for NaiveDateTime {
/// ```
impl fmt::Debug for NaiveDateTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}T{:?}", self.date, self.time)
self.date.fmt(f)?;
f.write_char('T')?;
self.time.fmt(f)
}
}
@ -1660,7 +1663,9 @@ impl fmt::Debug for NaiveDateTime {
/// ```
impl fmt::Display for NaiveDateTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {}", self.date, self.time)
self.date.fmt(f)?;
f.write_char(' ')?;
self.time.fmt(f)
}
}