time
time copied to clipboard
add well known format RFC 1123
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()
}