1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#![allow(unused_imports)]
extern crate chrono;
use std::time::{SystemTime,UNIX_EPOCH};
use chrono::{DateTime,TimeZone};
pub fn convert<T:TimeZone>(st:SystemTime,tz:T) -> DateTime<T>{
let duration = st.duration_since(UNIX_EPOCH).unwrap().as_secs();
return tz.timestamp(duration as i64,0);
}
#[cfg(test)]
mod tests {
use super::convert;
use std::time::{Duration,SystemTime,UNIX_EPOCH};
use chrono::Utc;
#[test]
fn test() {
let d = Duration::from_secs(1501301355);
let t = UNIX_EPOCH + d;
let dt = convert(t,Utc);
assert_eq!("Sat, 29 Jul 2017 04:09:15 +0000",dt.to_rfc2822());
}
}