@@ -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" ) ]
609604pub ( 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 ( ) ;
0 commit comments