Parse time strings with `PrimitiveDateTime::parse` instead of `OffsetDateTime::parse`

This commit is contained in:
kevinpoitra 2020-01-06 02:23:23 -06:00
parent 729004aa6b
commit 2158a8e097
3 changed files with 15 additions and 15 deletions

View File

@ -992,7 +992,7 @@ impl<'a, 'b> PartialEq<Cookie<'b>> for Cookie<'a> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{Cookie, SameSite}; use super::{Cookie, SameSite};
use time::OffsetDateTime; use time::{OffsetDateTime, PrimitiveDateTime, UtcOffset};
#[test] #[test]
fn format() { fn format() {
@ -1017,7 +1017,7 @@ mod tests {
assert_eq!(&cookie.to_string(), "foo=bar; Domain=www.rust-lang.org"); assert_eq!(&cookie.to_string(), "foo=bar; Domain=www.rust-lang.org");
let time_str = "Wed, 21 Oct 2015 07:28:00 GMT"; let time_str = "Wed, 21 Oct 2015 07:28:00 GMT";
let expires = OffsetDateTime::parse(time_str, "%a, %d %b %Y %H:%M:%S").unwrap(); let expires = PrimitiveDateTime::parse(time_str, "%a, %d %b %Y %H:%M:%S").unwrap().using_offset(UtcOffset::UTC);
let cookie = Cookie::build("foo", "bar").expires(expires).finish(); let cookie = Cookie::build("foo", "bar").expires(expires).finish();
assert_eq!( assert_eq!(
&cookie.to_string(), &cookie.to_string(),

View File

@ -6,7 +6,7 @@ use std::fmt;
use std::str::Utf8Error; use std::str::Utf8Error;
use percent_encoding::percent_decode; use percent_encoding::percent_decode;
use time::{Duration, OffsetDateTime}; use time::{Duration, PrimitiveDateTime, UtcOffset};
use super::{Cookie, CookieStr, SameSite}; use super::{Cookie, CookieStr, SameSite};
@ -182,13 +182,13 @@ fn parse_inner<'c>(s: &str, decode: bool) -> Result<Cookie<'c>, ParseError> {
// Try parsing with three date formats according to // Try parsing with three date formats according to
// http://tools.ietf.org/html/rfc2616#section-3.3.1. Try // http://tools.ietf.org/html/rfc2616#section-3.3.1. Try
// additional ones as encountered in the real world. // additional ones as encountered in the real world.
let tm = OffsetDateTime::parse(v, "%a, %d %b %Y %H:%M:%S") let tm = PrimitiveDateTime::parse(v, "%a, %d %b %Y %H:%M:%S")
.or_else(|_| OffsetDateTime::parse(v, "%A, %d-%b-%y %H:%M:%S")) .or_else(|_| PrimitiveDateTime::parse(v, "%A, %d-%b-%y %H:%M:%S"))
.or_else(|_| OffsetDateTime::parse(v, "%a, %d-%b-%Y %H:%M:%S")) .or_else(|_| PrimitiveDateTime::parse(v, "%a, %d-%b-%Y %H:%M:%S"))
.or_else(|_| OffsetDateTime::parse(v, "%a %b %d %H:%M:%S %Y")); .or_else(|_| PrimitiveDateTime::parse(v, "%a %b %d %H:%M:%S %Y"));
if let Ok(time) = tm { if let Ok(time) = tm {
cookie.expires = Some(time) cookie.expires = Some(time.using_offset(UtcOffset::UTC))
} }
} }
_ => { _ => {
@ -216,7 +216,7 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{Cookie, SameSite}; use super::{Cookie, SameSite};
use time::{Duration, OffsetDateTime}; use time::{Duration, OffsetDateTime, PrimitiveDateTime, UtcOffset};
macro_rules! assert_eq_parse { macro_rules! assert_eq_parse {
($string:expr, $expected:expr) => { ($string:expr, $expected:expr) => {
@ -376,7 +376,7 @@ mod tests {
); );
let time_str = "Wed, 21 Oct 2015 07:28:00 GMT"; let time_str = "Wed, 21 Oct 2015 07:28:00 GMT";
let expires = OffsetDateTime::parse(time_str, "%a, %d %b %Y %H:%M:%S").unwrap(); let expires = PrimitiveDateTime::parse(time_str, "%a, %d %b %Y %H:%M:%S").unwrap().using_offset(UtcOffset::UTC);
expected.set_expires(expires); expected.set_expires(expires);
assert_eq_parse!( assert_eq_parse!(
" foo=bar ;HttpOnly; Secure; Max-Age=4; Path=/foo; \ " foo=bar ;HttpOnly; Secure; Max-Age=4; Path=/foo; \
@ -385,7 +385,7 @@ mod tests {
); );
unexpected.set_domain("foo.com"); unexpected.set_domain("foo.com");
let bad_expires = OffsetDateTime::parse(time_str, "%a, %d %b %Y %H:%S:%M").unwrap(); let bad_expires = PrimitiveDateTime::parse(time_str, "%a, %d %b %Y %H:%S:%M").unwrap().using_offset(UtcOffset::UTC);
expected.set_expires(bad_expires); expected.set_expires(bad_expires);
assert_ne_parse!( assert_ne_parse!(
" foo=bar ;HttpOnly; Secure; Max-Age=4; Path=/foo; \ " foo=bar ;HttpOnly; Secure; Max-Age=4; Path=/foo; \

View File

@ -18,11 +18,11 @@ impl FromStr for HttpDate {
type Err = ParseError; type Err = ParseError;
fn from_str(s: &str) -> Result<HttpDate, ParseError> { fn from_str(s: &str) -> Result<HttpDate, ParseError> {
match OffsetDateTime::parse(s, "%a, %d %b %Y %H:%M:%S") match PrimitiveDateTime::parse(s, "%a, %d %b %Y %H:%M:%S")
.or_else(|_| OffsetDateTime::parse(s, "%A, %d-%b-%y %H:%M:%S")) .or_else(|_| PrimitiveDateTime::parse(s, "%A, %d-%b-%y %H:%M:%S"))
.or_else(|_| OffsetDateTime::parse(s, "%c")) .or_else(|_| PrimitiveDateTime::parse(s, "%c"))
{ {
Ok(t) => Ok(HttpDate(t)), Ok(t) => Ok(HttpDate(t.using_offset(UtcOffset::UTC))),
Err(_) => { Err(_) => {
Err(ParseError::Header) Err(ParseError::Header)
}, },