Skip to content

Commit 0da5f29

Browse files
authored
Merge pull request #38 from hrvolapeter/time-0.3
Replace chrono with time 0.3
2 parents af04c7e + 3746393 commit 0da5f29

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ webpki-roots = ["hyper-rustls/webpki-roots"]
1717

1818
[dependencies]
1919
base64 = "0.13"
20-
chrono = { version = "0.4", features = ["serde"] }
20+
time = { version = "0.3.5", features = ["serde"] }
2121
hyper = { version = "0.14.2", features = ["client", "runtime", "http2"] }
2222
hyper-rustls = { version = "0.22.1", default-features = false, features = ["tokio-runtime"] }
2323
log = "0.4"

src/jwt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<'a> Claims<'a> {
6262
where
6363
T: std::string::ToString,
6464
{
65-
let iat = chrono::Utc::now().timestamp();
65+
let iat = time::OffsetDateTime::now_utc().unix_timestamp();
6666
let expiry = iat + 3600 - 5; // Max validity is 1h.
6767

6868
let scope: String = scopes

src/types.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use chrono::{DateTime, Utc};
21
use serde::Deserializer;
32
use 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

1818
impl 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>
3840
where
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

Comments
 (0)