Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimise rfc3339 (and rfc2822) #844

Merged
merged 6 commits into from Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/date.rs
Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down
20 changes: 14 additions & 6 deletions src/datetime/mod.rs
Expand Up @@ -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")]
Expand Down Expand Up @@ -599,16 +600,20 @@ where
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
pub fn to_rfc2822(&self) -> String {
const ITEMS: &[Item<'static>] = &[Item::Fixed(Fixed::RFC2822)];
self.format_with_items(ITEMS.iter()).to_string()
let mut result = String::with_capacity(32);
crate::format::write_rfc2822(&mut result, self.naive_local(), self.offset.fix())
.expect("writing rfc2822 datetime to string should never fail");
result
}

/// Returns an RFC 3339 and ISO 8601 date and time string such as `1996-12-19T16:39:57-08:00`.
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
pub fn to_rfc3339(&self) -> String {
const ITEMS: &[Item<'static>] = &[Item::Fixed(Fixed::RFC3339)];
self.format_with_items(ITEMS.iter()).to_string()
let mut result = String::with_capacity(32);
crate::format::write_rfc3339(&mut result, self.naive_local(), self.offset.fix())
.expect("writing rfc3339 datetime to string should never fail");
result
}

/// Return an RFC 3339 and ISO 8601 date and time string with subseconds
Expand Down Expand Up @@ -990,7 +995,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)
}
}

Expand All @@ -999,7 +1005,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)
}
}

Expand Down