mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-27 00:00:12 +02:00
examples: replace time crate with httpdate (#4169)
This commit is contained in:
+1
-1
@@ -20,7 +20,7 @@ serde = "1.0"
|
|||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
httparse = "1.0"
|
httparse = "1.0"
|
||||||
time = "0.1"
|
httpdate = "1.0"
|
||||||
once_cell = "1.5.2"
|
once_cell = "1.5.2"
|
||||||
rand = "0.8.3"
|
rand = "0.8.3"
|
||||||
|
|
||||||
|
|||||||
+14
-10
@@ -221,8 +221,9 @@ mod date {
|
|||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::fmt::{self, Write};
|
use std::fmt::{self, Write};
|
||||||
use std::str;
|
use std::str;
|
||||||
|
use std::time::SystemTime;
|
||||||
|
|
||||||
use time::{self, Duration};
|
use httpdate::HttpDate;
|
||||||
|
|
||||||
pub struct Now(());
|
pub struct Now(());
|
||||||
|
|
||||||
@@ -252,22 +253,26 @@ mod date {
|
|||||||
struct LastRenderedNow {
|
struct LastRenderedNow {
|
||||||
bytes: [u8; 128],
|
bytes: [u8; 128],
|
||||||
amt: usize,
|
amt: usize,
|
||||||
next_update: time::Timespec,
|
unix_date: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
thread_local!(static LAST: RefCell<LastRenderedNow> = RefCell::new(LastRenderedNow {
|
thread_local!(static LAST: RefCell<LastRenderedNow> = RefCell::new(LastRenderedNow {
|
||||||
bytes: [0; 128],
|
bytes: [0; 128],
|
||||||
amt: 0,
|
amt: 0,
|
||||||
next_update: time::Timespec::new(0, 0),
|
unix_date: 0,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
impl fmt::Display for Now {
|
impl fmt::Display for Now {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
LAST.with(|cache| {
|
LAST.with(|cache| {
|
||||||
let mut cache = cache.borrow_mut();
|
let mut cache = cache.borrow_mut();
|
||||||
let now = time::get_time();
|
let now = SystemTime::now();
|
||||||
if now >= cache.next_update {
|
let now_unix = now
|
||||||
cache.update(now);
|
.duration_since(SystemTime::UNIX_EPOCH)
|
||||||
|
.map(|since_epoch| since_epoch.as_secs())
|
||||||
|
.unwrap_or(0);
|
||||||
|
if cache.unix_date != now_unix {
|
||||||
|
cache.update(now, now_unix);
|
||||||
}
|
}
|
||||||
f.write_str(cache.buffer())
|
f.write_str(cache.buffer())
|
||||||
})
|
})
|
||||||
@@ -279,11 +284,10 @@ mod date {
|
|||||||
str::from_utf8(&self.bytes[..self.amt]).unwrap()
|
str::from_utf8(&self.bytes[..self.amt]).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, now: time::Timespec) {
|
fn update(&mut self, now: SystemTime, now_unix: u64) {
|
||||||
self.amt = 0;
|
self.amt = 0;
|
||||||
write!(LocalBuffer(self), "{}", time::at(now).rfc822()).unwrap();
|
self.unix_date = now_unix;
|
||||||
self.next_update = now + Duration::seconds(1);
|
write!(LocalBuffer(self), "{}", HttpDate::from(now)).unwrap();
|
||||||
self.next_update.nsec = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user