Skip to content

Commit c282160

Browse files
committed
Never return an error when formatting the RFC2822 item.
1 parent 243994f commit c282160

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

src/datetime/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,12 @@ impl<Tz: TimeZone> DateTime<Tz> {
554554
/// ```
555555
#[cfg(feature = "alloc")]
556556
pub fn try_to_rfc2822(&self) -> Option<String> {
557+
let naive_local = self.overflowing_naive_local();
558+
if !(0..=9999).contains(&naive_local.year()) {
559+
return None;
560+
}
557561
let mut result = String::with_capacity(32);
558-
write_rfc2822(&mut result, self.overflowing_naive_local(), self.offset.fix()).ok()?;
562+
write_rfc2822(&mut result, naive_local, self.offset.fix()).ok()?;
559563
Some(result)
560564
}
561565

src/format/formatting.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -599,24 +599,13 @@ pub(crate) fn write_rfc3339(
599599
.format(w, off)
600600
}
601601

602-
/// Write datetimes like `Tue, 1 Jul 2003 10:52:37 +0200`, same as `%a, %d %b %Y %H:%M:%S %z`
603-
///
604-
/// # Errors
605-
///
606-
/// RFC 2822 is only defined on years 0 through 9999, and this function returns an error on dates
607-
/// outside that range.
602+
/// Write datetimes like `Tue, 1 Jul 2003 10:52:37 +0200`, similar to `%a, %d %b %Y %H:%M:%S %z`.
608603
#[cfg(feature = "alloc")]
609604
pub(crate) fn write_rfc2822(
610605
w: &mut impl Write,
611606
dt: NaiveDateTime,
612607
off: FixedOffset,
613608
) -> fmt::Result {
614-
let year = dt.year();
615-
// RFC2822 is only defined on years 0 through 9999
616-
if !(0..=9999).contains(&year) {
617-
return Err(fmt::Error);
618-
}
619-
620609
let english = default_locale();
621610

622611
w.write_str(short_weekdays(english)[dt.weekday().num_days_from_sunday() as usize])?;
@@ -630,8 +619,13 @@ pub(crate) fn write_rfc2822(
630619
w.write_char(' ')?;
631620
w.write_str(short_months(english)[dt.month0() as usize])?;
632621
w.write_char(' ')?;
633-
write_hundreds(w, (year / 100) as u8)?;
634-
write_hundreds(w, (year % 100) as u8)?;
622+
let year = dt.year();
623+
if (0..=9999).contains(&year) {
624+
write_hundreds(w, (year / 100) as u8)?;
625+
write_hundreds(w, (year % 100) as u8)?;
626+
} else {
627+
write!(w, "{:04}", year)?;
628+
}
635629
w.write_char(' ')?;
636630

637631
let (hour, min, sec) = dt.time().hms();

src/format/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ pub enum Fixed {
240240
/// Parsing allows an optional colon.
241241
TimezoneOffsetZ,
242242
/// RFC 2822 date and time syntax. Commonly used for email and MIME date and time.
243+
///
244+
/// This does not always output a strictly valid RFC 2822 string. RFC 2822 is only defined on
245+
/// years 0 through 9999. We format a date outside these ranges anyway to prevent a panic when
246+
/// formatting.
243247
RFC2822,
244248
/// RFC 3339 & ISO 8601 date and time syntax.
245249
///

0 commit comments

Comments
 (0)