Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace chrono with time crate #35

Merged
merged 4 commits into from Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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