|
1 | 1 | use std::fmt; |
2 | 2 | use std::str::FromStr; |
3 | | -use std::time::{Duration, SystemTime, UNIX_EPOCH}; |
| 3 | +use std::time::SystemTime; |
4 | 4 |
|
5 | 5 | use bytes::Bytes; |
6 | 6 | use http::header::HeaderValue; |
7 | | -use time; |
| 7 | +use httpdate; |
8 | 8 |
|
9 | 9 | use super::IterExt; |
10 | 10 |
|
@@ -32,7 +32,7 @@ use super::IterExt; |
32 | 32 | // HTTP-date, the sender MUST generate those timestamps in the |
33 | 33 | // IMF-fixdate format. |
34 | 34 | #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
35 | | -pub(crate) struct HttpDate(time::Tm); |
| 35 | +pub(crate) struct HttpDate(httpdate::HttpDate); |
36 | 36 |
|
37 | 37 | impl HttpDate { |
38 | 38 | pub(crate) fn from_val(val: &HeaderValue) -> Option<Self> { |
@@ -74,96 +74,74 @@ impl<'a> From<&'a HttpDate> for HeaderValue { |
74 | 74 | impl FromStr for HttpDate { |
75 | 75 | type Err = Error; |
76 | 76 | 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(()))?)) |
82 | 78 | } |
83 | 79 | } |
84 | 80 |
|
85 | 81 | impl fmt::Debug for HttpDate { |
86 | 82 | 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) |
88 | 84 | } |
89 | 85 | } |
90 | 86 |
|
91 | 87 | impl fmt::Display for HttpDate { |
92 | 88 | 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) |
94 | 90 | } |
95 | 91 | } |
96 | 92 |
|
97 | 93 | impl From<SystemTime> for HttpDate { |
98 | 94 | 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()) |
111 | 96 | } |
112 | 97 | } |
113 | 98 |
|
114 | 99 | impl From<HttpDate> for SystemTime { |
115 | 100 | 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) |
122 | 102 | } |
123 | 103 | } |
124 | 104 |
|
125 | 105 | #[cfg(test)] |
126 | 106 | mod tests { |
127 | 107 | 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 | + } |
143 | 121 |
|
144 | 122 | #[test] |
145 | 123 | fn test_imf_fixdate() { |
146 | 124 | 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() |
149 | 127 | ); |
150 | 128 | } |
151 | 129 |
|
152 | 130 | #[test] |
153 | 131 | fn test_rfc_850() { |
154 | 132 | assert_eq!( |
155 | | - "Sunday, 07-Nov-94 08:48:37 GMT" |
| 133 | + "Monday, 07-Nov-94 08:48:37 GMT" |
156 | 134 | .parse::<HttpDate>() |
157 | 135 | .unwrap(), |
158 | | - NOV_07 |
| 136 | + nov_07() |
159 | 137 | ); |
160 | 138 | } |
161 | 139 |
|
162 | 140 | #[test] |
163 | 141 | fn test_asctime() { |
164 | 142 | 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() |
167 | 145 | ); |
168 | 146 | } |
169 | 147 |
|
|
0 commit comments