Skip to content

Commit

Permalink
Merge pull request #35 from JohnTitor/replace-with-time
Browse files Browse the repository at this point in the history
Replace `chrono` with `time` crate
  • Loading branch information
jbr committed Dec 13, 2022
2 parents 176f774 + d28cef3 commit bbc072d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
7 changes: 3 additions & 4 deletions Cargo.toml
Expand Up @@ -31,10 +31,9 @@ log = "0.4.17"
version = "1.0.150"
features = ["rc", "derive"]

[dependencies.chrono]
version = "0.4.23"
default-features = false
features = ["clock", "serde", "std"]
[dependencies.time]
version = "0.3.17"
features = ["serde"]

[dev-dependencies.async-std]
version = "1.12.0"
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -63,9 +63,9 @@ pub use session_store::SessionStore;
pub use async_trait::async_trait;
pub use base64;
pub use blake3;
pub use chrono;
pub use hmac;
pub use log;
pub use serde;
pub use serde_json;
pub use sha2;
pub use time;
22 changes: 14 additions & 8 deletions src/session.rs
@@ -1,13 +1,14 @@
use chrono::{DateTime, Duration, Utc};
use rand::RngCore;
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
convert::TryFrom,
sync::{
atomic::{AtomicBool, Ordering},
Arc, RwLock,
},
};
use time::OffsetDateTime as DateTime;

/// # The main session type.
///
Expand Down Expand Up @@ -56,7 +57,7 @@ use std::{
#[derive(Debug, Serialize, Deserialize)]
pub struct Session {
id: String,
expiry: Option<DateTime<Utc>>,
expiry: Option<DateTime>,
data: Arc<RwLock<HashMap<String, String>>>,

#[serde(skip)]
Expand Down Expand Up @@ -368,7 +369,7 @@ impl Session {
/// assert!(session.expiry().is_some());
/// # Ok(()) }) }
/// ```
pub fn expiry(&self) -> Option<&DateTime<Utc>> {
pub fn expiry(&self) -> Option<&DateTime> {
self.expiry.as_ref()
}

Expand All @@ -381,11 +382,11 @@ impl Session {
/// # fn main() -> async_session::Result { async_std::task::block_on(async {
/// let mut session = Session::new();
/// assert_eq!(None, session.expiry());
/// session.set_expiry(chrono::Utc::now());
/// session.set_expiry(time::OffsetDateTime::now_utc());
/// assert!(session.expiry().is_some());
/// # Ok(()) }) }
/// ```
pub fn set_expiry(&mut self, expiry: DateTime<Utc>) {
pub fn set_expiry(&mut self, expiry: DateTime) {
self.expiry = Some(expiry);
}

Expand All @@ -403,7 +404,7 @@ impl Session {
/// # Ok(()) }) }
/// ```
pub fn expire_in(&mut self, ttl: std::time::Duration) {
self.expiry = Some(Utc::now() + Duration::from_std(ttl).unwrap());
self.expiry = Some(DateTime::now_utc() + ttl);
}

/// predicate function to determine if this session is
Expand All @@ -428,7 +429,7 @@ impl Session {
/// ```
pub fn is_expired(&self) -> bool {
match self.expiry {
Some(expiry) => expiry < Utc::now(),
Some(expiry) => expiry < DateTime::now_utc(),
None => false,
}
}
Expand Down Expand Up @@ -522,7 +523,12 @@ impl Session {
/// ```
/// Duration from now to the expiry time of this session
pub fn expires_in(&self) -> Option<std::time::Duration> {
self.expiry?.signed_duration_since(Utc::now()).to_std().ok()
let dur = self.expiry? - DateTime::now_utc();
if dur.is_negative() {
None
} else {
std::time::Duration::try_from(dur).ok()
}
}

/// takes the cookie value and consume this session.
Expand Down

0 comments on commit bbc072d

Please sign in to comment.