Skip to content

Commit 4bf199f

Browse files
quodlibetorseanmonstar
authored andcommitted
Replace time with httpdate
1 parent 2d9a5c4 commit 4bf199f

File tree

3 files changed

+29
-51
lines changed

3 files changed

+29
-51
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ bitflags = "1.0"
2424
bytes = "1"
2525
mime = "0.3.14"
2626
sha-1 = "0.9"
27-
time = "0.1.34"
27+
httpdate = "1"
2828

2929
[features]
3030
nightly = []

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ extern crate bitflags;
7878
extern crate bytes;
7979
extern crate headers_core;
8080
extern crate http;
81+
extern crate httpdate;
8182
extern crate mime;
8283
extern crate sha1;
8384
#[cfg(all(test, feature = "nightly"))]
8485
extern crate test;
85-
extern crate time;
8686

8787
pub use headers_core::{Error, Header};
8888

src/util/http_date.rs

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::fmt;
22
use std::str::FromStr;
3-
use std::time::{Duration, SystemTime, UNIX_EPOCH};
3+
use std::time::SystemTime;
44

55
use bytes::Bytes;
66
use http::header::HeaderValue;
7-
use time;
7+
use httpdate;
88

99
use super::IterExt;
1010

@@ -32,7 +32,7 @@ use super::IterExt;
3232
// HTTP-date, the sender MUST generate those timestamps in the
3333
// IMF-fixdate format.
3434
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
35-
pub(crate) struct HttpDate(time::Tm);
35+
pub(crate) struct HttpDate(httpdate::HttpDate);
3636

3737
impl HttpDate {
3838
pub(crate) fn from_val(val: &HeaderValue) -> Option<Self> {
@@ -74,96 +74,74 @@ impl<'a> From<&'a HttpDate> for HeaderValue {
7474
impl FromStr for HttpDate {
7575
type Err = Error;
7676
fn from_str(s: &str) -> Result<HttpDate, Error> {
77-
time::strptime(s, "%a, %d %b %Y %T %Z")
78-
.or_else(|_| time::strptime(s, "%A, %d-%b-%y %T %Z"))
79-
.or_else(|_| time::strptime(s, "%c"))
80-
.map(HttpDate)
81-
.map_err(|_| Error(()))
77+
Ok(HttpDate(s.parse().map_err(|_| Error(()))?))
8278
}
8379
}
8480

8581
impl fmt::Debug for HttpDate {
8682
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
87-
fmt::Display::fmt(&self.0.to_utc().rfc822(), f)
83+
fmt::Display::fmt(&self.0, f)
8884
}
8985
}
9086

9187
impl fmt::Display for HttpDate {
9288
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
93-
fmt::Display::fmt(&self.0.to_utc().rfc822(), f)
89+
fmt::Display::fmt(&self.0, f)
9490
}
9591
}
9692

9793
impl From<SystemTime> for HttpDate {
9894
fn from(sys: SystemTime) -> HttpDate {
99-
let tmspec = match sys.duration_since(UNIX_EPOCH) {
100-
Ok(dur) => {
101-
// subsec nanos always dropped
102-
time::Timespec::new(dur.as_secs() as i64, 0)
103-
}
104-
Err(err) => {
105-
let neg = err.duration();
106-
// subsec nanos always dropped
107-
time::Timespec::new(-(neg.as_secs() as i64), 0)
108-
}
109-
};
110-
HttpDate(time::at_utc(tmspec))
95+
HttpDate(sys.into())
11196
}
11297
}
11398

11499
impl From<HttpDate> for SystemTime {
115100
fn from(date: HttpDate) -> SystemTime {
116-
let spec = date.0.to_timespec();
117-
if spec.sec >= 0 {
118-
UNIX_EPOCH + Duration::new(spec.sec as u64, spec.nsec as u32)
119-
} else {
120-
UNIX_EPOCH - Duration::new(spec.sec as u64, spec.nsec as u32)
121-
}
101+
SystemTime::from(date.0)
122102
}
123103
}
124104

125105
#[cfg(test)]
126106
mod tests {
127107
use super::HttpDate;
128-
use time::Tm;
129-
130-
const NOV_07: HttpDate = HttpDate(Tm {
131-
tm_nsec: 0,
132-
tm_sec: 37,
133-
tm_min: 48,
134-
tm_hour: 8,
135-
tm_mday: 7,
136-
tm_mon: 10,
137-
tm_year: 94,
138-
tm_wday: 0,
139-
tm_isdst: 0,
140-
tm_yday: 0,
141-
tm_utcoff: 0,
142-
});
108+
109+
use std::time::{Duration, UNIX_EPOCH};
110+
111+
// The old tests had Sunday, but 1994-11-07 is a Monday.
112+
// See https://github.com/pyfisch/httpdate/pull/6#issuecomment-846881001
113+
fn nov_07() -> HttpDate {
114+
HttpDate((UNIX_EPOCH + Duration::new(784198117, 0)).into())
115+
}
116+
117+
#[test]
118+
fn test_display_is_imf_fixdate() {
119+
assert_eq!("Mon, 07 Nov 1994 08:48:37 GMT", &nov_07().to_string());
120+
}
143121

144122
#[test]
145123
fn test_imf_fixdate() {
146124
assert_eq!(
147-
"Sun, 07 Nov 1994 08:48:37 GMT".parse::<HttpDate>().unwrap(),
148-
NOV_07
125+
"Mon, 07 Nov 1994 08:48:37 GMT".parse::<HttpDate>().unwrap(),
126+
nov_07()
149127
);
150128
}
151129

152130
#[test]
153131
fn test_rfc_850() {
154132
assert_eq!(
155-
"Sunday, 07-Nov-94 08:48:37 GMT"
133+
"Monday, 07-Nov-94 08:48:37 GMT"
156134
.parse::<HttpDate>()
157135
.unwrap(),
158-
NOV_07
136+
nov_07()
159137
);
160138
}
161139

162140
#[test]
163141
fn test_asctime() {
164142
assert_eq!(
165-
"Sun Nov 7 08:48:37 1994".parse::<HttpDate>().unwrap(),
166-
NOV_07
143+
"Mon Nov 7 08:48:37 1994".parse::<HttpDate>().unwrap(),
144+
nov_07()
167145
);
168146
}
169147

0 commit comments

Comments
 (0)