Skip to content

Commit 5697fc8

Browse files
authored
Merge pull request #5 from jtgeibel/update/time
Upgrade to time 0.2
2 parents 1d4e6c7 + 6805f64 commit 5697fc8

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ the response is fresh.
1313
[dependencies]
1414
conduit = "0.9.0-alpha.2"
1515
conduit-middleware = "0.9.0-alpha.2"
16-
time = "0.1.0"
16+
time = { version = "0.2", default-features = false, features = ["std"] }
1717

1818
[dev-dependencies]
1919
conduit-test = "0.9.0-alpha.2"

src/lib.rs

+21-25
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ extern crate time;
88
use conduit::{header, Body, HeaderMap, Method, RequestExt, Response, StatusCode};
99
use conduit_middleware::{AfterResult, Middleware};
1010
use std::borrow::Cow;
11-
use time::{ParseError, Tm};
11+
use time::{OffsetDateTime, ParseError, PrimitiveDateTime};
1212

1313
#[allow(missing_copy_implementations)]
1414
pub struct ConditionalGet;
@@ -66,14 +66,14 @@ fn etag_matches(none_match: &[u8], res: &Response<Body>) -> bool {
6666
value == none_match
6767
}
6868

69-
fn is_modified_since(modified_since: Tm, res: &Response<Body>) -> bool {
69+
fn is_modified_since(modified_since: OffsetDateTime, res: &Response<Body>) -> bool {
7070
let last_modified = get_and_concat_header(res.headers(), header::LAST_MODIFIED);
7171

7272
match std::str::from_utf8(&last_modified) {
7373
Err(_) => false,
7474
Ok(last_modified) => match parse_http_date(last_modified) {
7575
Err(_) => false,
76-
Ok(last_modified) => modified_since.to_timespec() >= last_modified.to_timespec(),
76+
Ok(last_modified) => modified_since.timestamp() >= last_modified.timestamp(),
7777
},
7878
}
7979
}
@@ -90,23 +90,24 @@ fn get_and_concat_header(headers: &HeaderMap, name: header::HeaderName) -> Cow<'
9090
}
9191
}
9292

93-
fn parse_http_date(string: &str) -> Result<Tm, ()> {
93+
fn parse_http_date(string: &str) -> Result<OffsetDateTime, ()> {
9494
parse_rfc1123(string)
9595
.or_else(|_| parse_rfc850(string))
9696
.or_else(|_| parse_asctime(string))
9797
.map_err(|_| ())
9898
}
9999

100-
fn parse_rfc1123(string: &str) -> Result<Tm, ParseError> {
101-
time::strptime(string, "%a, %d %b %Y %T GMT")
100+
fn parse_rfc1123(string: &str) -> Result<OffsetDateTime, ParseError> {
101+
Ok(PrimitiveDateTime::parse(string, "%a, %d %b %Y %T GMT")?.assume_utc())
102102
}
103103

104-
fn parse_rfc850(string: &str) -> Result<Tm, ParseError> {
105-
time::strptime(string, "%a, %d-%m-%y %T GMT")
104+
fn parse_rfc850(string: &str) -> Result<OffsetDateTime, ParseError> {
105+
Ok(PrimitiveDateTime::parse(string, "%a, %d-%m-%y %T GMT")?.assume_utc())
106106
}
107107

108-
fn parse_asctime(string: &str) -> Result<Tm, ParseError> {
109-
time::strptime(string, "%a %m%t%d %T %Y")
108+
fn parse_asctime(string: &str) -> Result<OffsetDateTime, ParseError> {
109+
// TODO: should this be "%a %b %d %T %Y"?
110+
Ok(PrimitiveDateTime::parse(string, "%a %m\t%d %T %Y")?.assume_utc())
110111
}
111112

112113
#[cfg(test)]
@@ -119,8 +120,7 @@ mod tests {
119120
StatusCode,
120121
};
121122
use conduit_middleware::MiddlewareBuilder;
122-
use time;
123-
use time::Tm;
123+
use time::{Duration, OffsetDateTime};
124124

125125
use super::ConditionalGet;
126126

@@ -149,17 +149,17 @@ mod tests {
149149

150150
#[test]
151151
fn test_sends_304() {
152-
let handler = returning!(header::LAST_MODIFIED => httpdate(time::now()));
152+
let handler = returning!(header::LAST_MODIFIED => httpdate(OffsetDateTime::now_utc()));
153153
expect_304(handler.call(&mut request!(
154-
header::IF_MODIFIED_SINCE => httpdate(time::now())
154+
header::IF_MODIFIED_SINCE => httpdate(OffsetDateTime::now_utc())
155155
)));
156156
}
157157

158158
#[test]
159159
fn test_sends_304_if_older_than_now() {
160160
let handler = returning!(header::LAST_MODIFIED => before_now());
161161
expect_304(handler.call(&mut request!(
162-
header::IF_MODIFIED_SINCE => httpdate(time::now())
162+
header::IF_MODIFIED_SINCE => httpdate(OffsetDateTime::now_utc())
163163
)));
164164
}
165165

@@ -228,10 +228,7 @@ mod tests {
228228

229229
#[test]
230230
fn test_does_not_affect_malformed_timestamp() {
231-
let bad_stamp = time::now()
232-
.strftime("%Y-%m-%d %H:%M:%S %z")
233-
.unwrap()
234-
.to_string();
231+
let bad_stamp = OffsetDateTime::now_utc().format("%Y-%m-%d %H:%M:%S %z");
235232
let handler = returning!(header::LAST_MODIFIED => before_now());
236233
expect_200(handler.call(&mut request!(
237234
header::IF_MODIFIED_SINCE => bad_stamp
@@ -281,16 +278,15 @@ mod tests {
281278
}
282279

283280
fn before_now() -> String {
284-
let mut now = time::now();
285-
now.tm_year -= 1;
286-
httpdate(now)
281+
let now = OffsetDateTime::now_utc();
282+
httpdate(now - Duration::weeks(52))
287283
}
288284

289285
fn now() -> String {
290-
httpdate(time::now())
286+
httpdate(OffsetDateTime::now_utc())
291287
}
292288

293-
fn httpdate(time: Tm) -> String {
294-
time.strftime("%a, %d-%m-%y %T GMT").unwrap().to_string()
289+
fn httpdate(time: OffsetDateTime) -> String {
290+
time.format("%a, %d-%m-%y %T GMT")
295291
}
296292
}

0 commit comments

Comments
 (0)