diff --git a/actix-http/src/config.rs b/actix-http/src/config.rs index 6089741d..0312d611 100644 --- a/actix-http/src/config.rs +++ b/actix-http/src/config.rs @@ -8,7 +8,7 @@ use actix_rt::time::{delay_for, delay_until, Delay, Instant}; use bytes::BytesMut; use futures_util::{future, FutureExt}; use time; -use time::PrimitiveDateTime; +use time::OffsetDateTime; // "Sun, 06 Nov 1994 08:49:37 GMT".len() const DATE_VALUE_LENGTH: usize = 29; @@ -212,7 +212,7 @@ impl Date { } fn update(&mut self) { self.pos = 0; - write!(self, "{}", PrimitiveDateTime::now().format("%a, %d %b %Y %H:%M:%S GMT")).unwrap(); + write!(self, "{}", OffsetDateTime::now().format("%a, %d %b %Y %H:%M:%S GMT")).unwrap(); } } diff --git a/actix-http/src/cookie/builder.rs b/actix-http/src/cookie/builder.rs index 73b121e2..383c31b1 100644 --- a/actix-http/src/cookie/builder.rs +++ b/actix-http/src/cookie/builder.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; -use time::{Duration, PrimitiveDateTime}; +use time::{Duration, OffsetDateTime}; use super::{Cookie, SameSite}; @@ -63,13 +63,13 @@ impl CookieBuilder { /// use actix_http::cookie::Cookie; /// /// let c = Cookie::build("foo", "bar") - /// .expires(time::now()) + /// .expires(time::OffsetDateTime::now()) /// .finish(); /// /// assert!(c.expires().is_some()); /// ``` #[inline] - pub fn expires(mut self, when: PrimitiveDateTime) -> CookieBuilder { + pub fn expires(mut self, when: OffsetDateTime) -> CookieBuilder { self.cookie.set_expires(when); self } diff --git a/actix-http/src/cookie/jar.rs b/actix-http/src/cookie/jar.rs index b28102c5..9ba5aa19 100644 --- a/actix-http/src/cookie/jar.rs +++ b/actix-http/src/cookie/jar.rs @@ -1,7 +1,7 @@ use std::collections::HashSet; use std::mem::replace; -use time::{Duration, PrimitiveDateTime}; +use time::{Duration, OffsetDateTime}; use super::delta::DeltaCookie; use super::Cookie; @@ -221,7 +221,7 @@ impl CookieJar { if self.original_cookies.contains(cookie.name()) { cookie.set_value(""); cookie.set_max_age(Duration::seconds(0)); - cookie.set_expires(PrimitiveDateTime::now() - Duration::days(365)); + cookie.set_expires(OffsetDateTime::now() - Duration::days(365)); self.delta_cookies.replace(DeltaCookie::removed(cookie)); } else { self.delta_cookies.remove(cookie.name()); diff --git a/actix-http/src/cookie/mod.rs b/actix-http/src/cookie/mod.rs index 1d56284c..66ed1d48 100644 --- a/actix-http/src/cookie/mod.rs +++ b/actix-http/src/cookie/mod.rs @@ -66,7 +66,7 @@ use std::fmt; use std::str::FromStr; use percent_encoding::{percent_encode, AsciiSet, CONTROLS}; -use time::{Duration, PrimitiveDateTime}; +use time::{Duration, OffsetDateTime}; pub use self::builder::CookieBuilder; pub use self::draft::*; @@ -171,7 +171,7 @@ pub struct Cookie<'c> { /// The cookie's value. value: CookieStr, /// The cookie's expiration, if any. - expires: Option, + expires: Option, /// The cookie's maximum age, if any. max_age: Option, /// The cookie's domain, if any. @@ -546,7 +546,7 @@ impl<'c> Cookie<'c> { /// assert_eq!(c.expires().map(|t| t.tm_year), Some(117)); /// ``` #[inline] - pub fn expires(&self) -> Option { + pub fn expires(&self) -> Option { self.expires } @@ -697,19 +697,19 @@ impl<'c> Cookie<'c> { /// /// ```rust /// use actix_http::cookie::Cookie; - /// use time::Duration; + /// use time::{Duration, OffsetDateTime}; /// /// let mut c = Cookie::new("name", "value"); /// assert_eq!(c.expires(), None); /// - /// let mut now = time::PrimitiveDateTime::now(); + /// let mut now = OffsetDateTime::now(); /// now += Duration::week(); /// /// c.set_expires(now); /// assert!(c.expires().is_some()) /// ``` #[inline] - pub fn set_expires(&mut self, time: PrimitiveDateTime) { + pub fn set_expires(&mut self, time: OffsetDateTime) { self.expires = Some(time); } @@ -733,7 +733,7 @@ impl<'c> Cookie<'c> { pub fn make_permanent(&mut self) { let twenty_years = Duration::days(365 * 20); self.set_max_age(twenty_years); - self.set_expires(PrimitiveDateTime::now() + twenty_years); + self.set_expires(OffsetDateTime::now() + twenty_years); } fn fmt_parameters(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -992,7 +992,7 @@ impl<'a, 'b> PartialEq> for Cookie<'a> { #[cfg(test)] mod tests { use super::{Cookie, SameSite}; - use time::PrimitiveDateTime; + use time::OffsetDateTime; #[test] fn format() { @@ -1017,7 +1017,7 @@ mod tests { assert_eq!(&cookie.to_string(), "foo=bar; Domain=www.rust-lang.org"); let time_str = "Wed, 21 Oct 2015 07:28:00 GMT"; - let expires = PrimitiveDateTime::parse(time_str, "%a, %d %b %Y %H:%M:%S").unwrap(); + let expires = OffsetDateTime::parse(time_str, "%a, %d %b %Y %H:%M:%S").unwrap(); let cookie = Cookie::build("foo", "bar").expires(expires).finish(); assert_eq!( &cookie.to_string(), diff --git a/actix-http/src/cookie/parse.rs b/actix-http/src/cookie/parse.rs index 157126db..2d026749 100644 --- a/actix-http/src/cookie/parse.rs +++ b/actix-http/src/cookie/parse.rs @@ -6,7 +6,7 @@ use std::fmt; use std::str::Utf8Error; use percent_encoding::percent_decode; -use time::{Duration, PrimitiveDateTime}; +use time::{Duration, OffsetDateTime}; use super::{Cookie, CookieStr, SameSite}; @@ -182,10 +182,10 @@ fn parse_inner<'c>(s: &str, decode: bool) -> Result, ParseError> { // Try parsing with three date formats according to // http://tools.ietf.org/html/rfc2616#section-3.3.1. Try // additional ones as encountered in the real world. - let tm = PrimitiveDateTime::parse(v, "%a, %d %b %Y %H:%M:%S") - .or_else(|_| PrimitiveDateTime::parse(v, "%A, %d-%b-%y %H:%M:%S")) - .or_else(|_| PrimitiveDateTime::parse(v, "%a, %d-%b-%Y %H:%M:%S")) - .or_else(|_| PrimitiveDateTime::parse(v, "%a %b %d %H:%M:%S %Y")); + let tm = OffsetDateTime::parse(v, "%a, %d %b %Y %H:%M:%S") + .or_else(|_| OffsetDateTime::parse(v, "%A, %d-%b-%y %H:%M:%S")) + .or_else(|_| OffsetDateTime::parse(v, "%a, %d-%b-%Y %H:%M:%S")) + .or_else(|_| OffsetDateTime::parse(v, "%a %b %d %H:%M:%S %Y")); if let Ok(time) = tm { cookie.expires = Some(time) @@ -216,7 +216,7 @@ where #[cfg(test)] mod tests { use super::{Cookie, SameSite}; - use time::{Duration, PrimitiveDateTime}; + use time::{Duration, OffsetDateTime}; macro_rules! assert_eq_parse { ($string:expr, $expected:expr) => { @@ -376,7 +376,7 @@ mod tests { ); let time_str = "Wed, 21 Oct 2015 07:28:00 GMT"; - let expires = PrimitiveDateTime::parse(time_str, "%a, %d %b %Y %H:%M:%S").unwrap(); + let expires = OffsetDateTime::parse(time_str, "%a, %d %b %Y %H:%M:%S").unwrap(); expected.set_expires(expires); assert_eq_parse!( " foo=bar ;HttpOnly; Secure; Max-Age=4; Path=/foo; \ @@ -385,7 +385,7 @@ mod tests { ); unexpected.set_domain("foo.com"); - let bad_expires = PrimitiveDateTime::parse(time_str, "%a, %d %b %Y %H:%S:%M").unwrap(); + let bad_expires = OffsetDateTime::parse(time_str, "%a, %d %b %Y %H:%S:%M").unwrap(); expected.set_expires(bad_expires); assert_ne_parse!( " foo=bar ;HttpOnly; Secure; Max-Age=4; Path=/foo; \ diff --git a/actix-http/src/header/shared/httpdate.rs b/actix-http/src/header/shared/httpdate.rs index 5177a751..b0ef64a0 100644 --- a/actix-http/src/header/shared/httpdate.rs +++ b/actix-http/src/header/shared/httpdate.rs @@ -5,22 +5,22 @@ use std::time::{SystemTime, UNIX_EPOCH}; use bytes::{buf::BufMutExt, BytesMut}; use http::header::{HeaderValue, InvalidHeaderValue}; -use time::PrimitiveDateTime; +use time::{PrimitiveDateTime, OffsetDateTime, UtcOffset}; use crate::error::ParseError; use crate::header::IntoHeaderValue; /// A timestamp with HTTP formatting and parsing #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] -pub struct HttpDate(time::PrimitiveDateTime); +pub struct HttpDate(OffsetDateTime); impl FromStr for HttpDate { type Err = ParseError; fn from_str(s: &str) -> Result { - match PrimitiveDateTime::parse(s, "%a, %d %b %Y %H:%M:%S") - .or_else(|_| PrimitiveDateTime::parse(s, "%A, %d-%b-%y %H:%M:%S")) - .or_else(|_| PrimitiveDateTime::parse(s, "%c")) + match OffsetDateTime::parse(s, "%a, %d %b %Y %H:%M:%S") + .or_else(|_| OffsetDateTime::parse(s, "%A, %d-%b-%y %H:%M:%S")) + .or_else(|_| OffsetDateTime::parse(s, "%c")) { Ok(t) => Ok(HttpDate(t)), Err(_) => { @@ -36,15 +36,15 @@ impl Display for HttpDate { } } -impl From for HttpDate { - fn from(dt: time::PrimitiveDateTime) -> HttpDate { +impl From for HttpDate { + fn from(dt: time::OffsetDateTime) -> HttpDate { HttpDate(dt) } } impl From for HttpDate { fn from(sys: SystemTime) -> HttpDate { - HttpDate(PrimitiveDateTime::from(sys)) + HttpDate(PrimitiveDateTime::from(sys).using_offset(UtcOffset::UTC)) } } @@ -61,7 +61,7 @@ impl IntoHeaderValue for HttpDate { impl From for SystemTime { fn from(date: HttpDate) -> SystemTime { let dt = date.0; - let epoch = PrimitiveDateTime::unix_epoch(); + let epoch = OffsetDateTime::unix_epoch(); if dt >= epoch { UNIX_EPOCH + (dt - epoch) @@ -74,9 +74,9 @@ impl From for SystemTime { #[cfg(test)] mod tests { use super::HttpDate; - use time::{PrimitiveDateTime, Date, Time}; + use time::{OffsetDateTime, Date, Time}; - const NOV_07: HttpDate = HttpDate(PrimitiveDateTime::new( + const NOV_07: HttpDate = HttpDate(OffsetDateTime::new( Date::try_from_ymd(1994, 11, 7).unwrap(), Time::try_from_hms(8, 48, 37).unwrap() )); diff --git a/actix-session/src/cookie.rs b/actix-session/src/cookie.rs index 355f49d7..918b24da 100644 --- a/actix-session/src/cookie.rs +++ b/actix-session/src/cookie.rs @@ -27,7 +27,7 @@ use actix_web::{Error, HttpMessage, ResponseError}; use derive_more::{Display, From}; use futures::future::{ok, FutureExt, LocalBoxFuture, Ready}; use serde_json::error::Error as JsonError; -use time::{Duration, PrimitiveDateTime}; +use time::{Duration, OffsetDateTime}; use crate::{Session, SessionStatus}; @@ -125,7 +125,7 @@ impl CookieSessionInner { let mut cookie = Cookie::named(self.name.clone()); cookie.set_value(""); cookie.set_max_age(Duration::seconds(0)); - cookie.set_expires(PrimitiveDateTime::now() - Duration::days(365)); + cookie.set_expires(OffsetDateTime::now() - Duration::days(365)); let val = HeaderValue::from_str(&cookie.to_string())?; res.headers_mut().append(SET_COOKIE, val); diff --git a/src/middleware/logger.rs b/src/middleware/logger.rs index cd15e3e0..4cf5e445 100644 --- a/src/middleware/logger.rs +++ b/src/middleware/logger.rs @@ -14,7 +14,7 @@ use bytes::Bytes; use futures::future::{ok, Ready}; use log::debug; use regex::Regex; -use time::PrimitiveDateTime; +use time::OffsetDateTime; use crate::dev::{BodySize, MessageBody, ResponseBody}; use crate::error::{Error, Result}; @@ -163,11 +163,11 @@ where LoggerResponse { fut: self.service.call(req), format: None, - time: PrimitiveDateTime::now(), + time: OffsetDateTime::now(), _t: PhantomData, } } else { - let now = PrimitiveDateTime::now(); + let now = OffsetDateTime::now(); let mut format = self.inner.format.clone(); for unit in &mut format.0 { @@ -192,7 +192,7 @@ where { #[pin] fut: S::Future, - time: time::PrimitiveDateTime, + time: OffsetDateTime, format: Option, _t: PhantomData<(B,)>, } @@ -242,7 +242,7 @@ pub struct StreamLog { body: ResponseBody, format: Option, size: usize, - time: time::PrimitiveDateTime, + time: OffsetDateTime, } impl Drop for StreamLog { @@ -366,19 +366,19 @@ impl FormatText { &self, fmt: &mut Formatter<'_>, size: usize, - entry_time: time::PrimitiveDateTime, + entry_time: OffsetDateTime, ) -> Result<(), fmt::Error> { match *self { FormatText::Str(ref string) => fmt.write_str(string), FormatText::Percent => "%".fmt(fmt), FormatText::ResponseSize => size.fmt(fmt), FormatText::Time => { - let rt = PrimitiveDateTime::now() - entry_time; + let rt = OffsetDateTime::now() - entry_time; let rt = (rt.whole_nanoseconds() as f64) / 1_000_000_000.0; fmt.write_fmt(format_args!("{:.6}", rt)) } FormatText::TimeMillis => { - let rt = PrimitiveDateTime::now() - entry_time; + let rt = OffsetDateTime::now() - entry_time; let rt = (rt.whole_nanoseconds() as f64) / 1_000_000.0; fmt.write_fmt(format_args!("{:.6}", rt)) } @@ -414,7 +414,7 @@ impl FormatText { } } - fn render_request(&mut self, now: PrimitiveDateTime, req: &ServiceRequest) { + fn render_request(&mut self, now: OffsetDateTime, req: &ServiceRequest) { match *self { FormatText::RequestLine => { *self = if req.query_string().is_empty() { @@ -513,7 +513,7 @@ mod tests { .uri("/test/route/yeah") .to_srv_request(); - let now = PrimitiveDateTime::now(); + let now = OffsetDateTime::now(); for unit in &mut format.0 { unit.render_request(now, &req); } @@ -544,7 +544,7 @@ mod tests { ) .to_srv_request(); - let now = PrimitiveDateTime::now(); + let now = OffsetDateTime::now(); for unit in &mut format.0 { unit.render_request(now, &req); } @@ -554,7 +554,7 @@ mod tests { unit.render_response(&resp); } - let entry_time = PrimitiveDateTime::now(); + let entry_time = OffsetDateTime::now(); let render = |fmt: &mut Formatter<'_>| { for unit in &format.0 { unit.render(fmt, 1024, entry_time)?; @@ -572,7 +572,7 @@ mod tests { let mut format = Format::new("%t"); let req = TestRequest::default().to_srv_request(); - let now = PrimitiveDateTime::now(); + let now = OffsetDateTime::now(); for unit in &mut format.0 { unit.render_request(now, &req); }