1- use chrono:: { DateTime , Utc } ;
21use serde:: Deserializer ;
32use serde:: { Deserialize , Serialize } ;
3+ use time:: { Duration , OffsetDateTime } ;
44
55/// Represents an access token. All access tokens are Bearer tokens.
66/// Token cannot be cached.
@@ -12,14 +12,16 @@ pub struct Token {
1212 deserialize_with = "deserialize_time" ,
1313 rename( deserialize = "expires_in" )
1414 ) ]
15- expires_at : Option < DateTime < Utc > > ,
15+ expires_at : Option < OffsetDateTime > ,
1616}
1717
1818impl Token {
1919 /// Define if the token has has_expired
2020 pub fn has_expired ( & self ) -> bool {
2121 self . expires_at
22- . map ( |expiration_time| expiration_time - chrono:: Duration :: seconds ( 30 ) <= Utc :: now ( ) )
22+ . map ( |expiration_time| {
23+ expiration_time - Duration :: seconds ( 30 ) <= OffsetDateTime :: now_utc ( )
24+ } )
2325 . unwrap_or ( false )
2426 }
2527
@@ -29,17 +31,18 @@ impl Token {
2931 }
3032
3133 /// Get expiry of token, if available
32- pub fn expires_at ( & self ) -> Option < DateTime < Utc > > {
34+ pub fn expires_at ( & self ) -> Option < OffsetDateTime > {
3335 self . expires_at
3436 }
3537}
3638
37- fn deserialize_time < ' de , D > ( deserializer : D ) -> Result < Option < DateTime < Utc > > , D :: Error >
39+ fn deserialize_time < ' de , D > ( deserializer : D ) -> Result < Option < OffsetDateTime > , D :: Error >
3840where
3941 D : Deserializer < ' de > ,
4042{
4143 let s: Option < i64 > = Deserialize :: deserialize ( deserializer) ?;
42- let s = s. map ( |seconds_from_now| Utc :: now ( ) + chrono:: Duration :: seconds ( seconds_from_now) ) ;
44+ let s =
45+ s. map ( |seconds_from_now| OffsetDateTime :: now_utc ( ) + Duration :: seconds ( seconds_from_now) ) ;
4346 Ok ( s)
4447}
4548
0 commit comments