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

add well known format RFC 1123 #498

Closed
ctaggart opened this issue Aug 5, 2022 · 2 comments
Closed

add well known format RFC 1123 #498

ctaggart opened this issue Aug 5, 2022 · 2 comments
Labels
A-format-description Area: format description A-well-known-format-description Area: well known format descriptions C-feature-request Category: a new feature (not already implemented)

Comments

@ctaggart
Copy link

ctaggart commented Aug 5, 2022

Thank you for maintaining the time crate! I recently migrated the Azure for SDK to it from the chrono crate. I like the design of time:OffsetDateTime much more than chrono::DateTime<T>. One thing that was missing that would be nice to see is RFC 1123 as a well known format. From #200, I see actix-web needed it and I'm sure there are many others.

from https://github.com/Azure/azure-sdk-for-rust/pull/965/files#diff-ee1e0429ff34efb8baedc932a7b765213e4ee67b4aaff384a18048144b6b535fR44-R86 :

/// RFC 1123: Requirements for Internet Hosts - Application and Support
///
/// https://www.rfc-editor.org/rfc/rfc1123
///
/// In Azure REST API specifications it is specified as `"format": "date-time-rfc1123"`.
///
/// In .NET it is the `rfc1123pattern`.
/// https://docs.microsoft.com/dotnet/api/system.globalization.datetimeformatinfo.rfc1123pattern
///
/// This format is also the preferred HTTP date format.
/// https://httpwg.org/specs/rfc9110.html#http.date
///
/// Sun, 06 Nov 1994 08:49:37 GMT
pub fn parse_rfc1123(s: &str) -> crate::Result<OffsetDateTime> {
    Ok(PrimitiveDateTime::parse(s, RFC1123_FORMAT)
        .with_context(ErrorKind::DataConversion, || {
            format!("unable to parse rfc1123 date '{s}")
        })?
        .assume_utc())
}

const RFC1123_FORMAT: &[FormatItem] = format_description!(
    "[weekday repr:short], [day] [month repr:short] [year] [hour]:[minute]:[second] GMT"
);

/// RFC 1123: Requirements for Internet Hosts - Application and Support
///
/// https://www.rfc-editor.org/rfc/rfc1123
///
/// In Azure REST API specifications it is specified as `"format": "date-time-rfc1123"`.
///
/// In .NET it is the `rfc1123pattern`.
/// https://docs.microsoft.com/dotnet/api/system.globalization.datetimeformatinfo.rfc1123pattern
///
/// This format is also the preferred HTTP date format.
/// https://httpwg.org/specs/rfc9110.html#http.date
///
/// Sun, 06 Nov 1994 08:49:37 GMT
pub fn to_rfc1123(date: &OffsetDateTime) -> String {
    date.to_offset(UtcOffset::UTC);
    // known format does not panic
    date.format(&RFC1123_FORMAT).unwrap()
}
@jhpratt jhpratt added C-feature-request Category: a new feature (not already implemented) A-format-description Area: format description A-well-known-format-description Area: well known format descriptions labels Aug 6, 2022
@jhpratt
Copy link
Member

jhpratt commented Feb 27, 2023

After reading the relevant part of the specifications, RFC 1123 appears to be nearly identical to RFC 2822. I haven't written anything formal down, but the primary difference I see is that RFC 1123 would accept a two digit year, and that's not particularly relevant as the conversion to the final Date/PrimitiveDateTime/OffsetDateTime only considers when the full four+ digit year has been parsed. RFC 2822 also accepts some things that are frankly bizarre (namely folding whitespace). In reality that shouldn't be a concern.

Am I overlooking something here?

@jhpratt
Copy link
Member

jhpratt commented Mar 19, 2023

Closing per above. If I am missing something, feel free to leave a comment.

@jhpratt jhpratt closed this as not planned Won't fix, can't repro, duplicate, stale Mar 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-format-description Area: format description A-well-known-format-description Area: well known format descriptions C-feature-request Category: a new feature (not already implemented)
Projects
None yet
Development

No branches or pull requests

2 participants