2017-03-06 25 views

回答

3

exp的字段是「NumericDate」類型,根據RFC 7519這是「秒數從1970-01-01T00:00:00Z UTC直到指定UTC日期/時間,忽略閏秒

此描述與012m相同,即「」將時間轉換爲1970年1月1日秒以內的Tm當前時區*。

這樣:

let mut timer = time::now_utc(); 
timer = timer + Duration::days(1); 
token.claims.reg.exp = Some(timer.to_timespec().sec as u64); 

(請注意,雖然time + duration總是返回UTC時間v0.1.36的,它可以說是a defect可能被固定在未來要向前兼容,我用now_utc()代替的now()

(*:。to_timespec基本上就POSIX調用gmtime()和POSIX標準忽略閏秒在Windows上的結構轉換成FILETIME which again ignores leap seconds所以to_timespec是安全的,如果你雷爾使用


如果使用std::time::SystemTime,同樣可以使用

let mut timer = SystemTime::now(); 
timer += Duration::from_secs(86400); 
token.claims.reg.exp = Some(timer.duration_since(UNIX_EPOCH).unwrap().as_secs()); 
+0

你救了我一天可以得到約27秒差Y照顧。)! – NotBad4U