-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c105b67
commit d0771e2
Showing
9 changed files
with
350 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use super::{DateTime, InternalDateTime}; | ||
|
||
impl InternalDateTime for DateTime { | ||
const MAX: Self = { | ||
#[cfg(feature = "time")] { | ||
DateTime::Time(time::OffsetDateTime::MAX) | ||
} | ||
|
||
#[cfg(not(feature = "time"))] { | ||
DateTime::Chrono(chrono::DateTime::MAX) | ||
} | ||
}; | ||
|
||
fn now() -> Self { | ||
#[cfg(feature = "time")] { | ||
DateTime::Time(time::OffsetDateTime::now()) | ||
} | ||
|
||
#[cfg(not(feature = "time"))] { | ||
DateTime::Chrono(chrono::DateTime::now()) | ||
} | ||
} | ||
|
||
fn destruct(&self) -> (i32, u32, u32, i32, u32, u32, u32) { | ||
match self { | ||
#[cfg(feature = "time")] | ||
DateTime::Time(inner) => inner.destruct(), | ||
#[cfg(feature = "chrono")] | ||
DateTime::Chrono(inner) => inner.destruct(), | ||
} | ||
} | ||
|
||
fn expiration_format(&self) -> Option<String> { | ||
match self { | ||
#[cfg(feature = "time")] | ||
DateTime::Time(inner) => inner.expiration_format(), | ||
#[cfg(feature = "chrono")] | ||
DateTime::Chrono(inner) => inner.expiration_format(), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "time")] | ||
impl InternalDateTime for time::OffsetDateTime { | ||
const MAX: Self = time::macros::datetime!(9999-12-31 23:59:59.999_999 UTC); | ||
|
||
fn now() -> Self { | ||
time::OffsetDateTime::now_utc() | ||
} | ||
|
||
fn destruct(&self) -> (i32, u32, u32, i32, u32, u32, u32) { | ||
let (year, month, day) = self.to_calendar_date(); | ||
let (hour, minute, second, nanos) = self.to_hms_nano(); | ||
(year, month as u32, day.into(), hour.into(), minute.into(), second.into(), nanos) | ||
} | ||
|
||
fn expiration_format(&self) -> Option<String> { | ||
self.format(&crate::parse::FMT1).ok() | ||
} | ||
} | ||
|
||
#[cfg(feature = "chrono")] | ||
impl InternalDateTime for chrono::DateTime<chrono::Utc> { | ||
const MAX: Self = chrono::DateTime::from_naive_utc_and_offset( | ||
chrono::NaiveDateTime::new( | ||
chrono::NaiveDate::from_ymd_opt(9999, 12, 31).unwrap(), | ||
chrono::NaiveTime::from_hms_micro_opt(23, 59, 59, 999_999).unwrap() | ||
), chrono::Utc); | ||
|
||
fn now() -> Self { | ||
chrono::Utc::now() | ||
} | ||
|
||
fn destruct(&self) -> (i32, u32, u32, i32, u32, u32, u32) { | ||
todo!() | ||
} | ||
|
||
fn expiration_format(&self) -> Option<String> { | ||
todo!() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
mod internal; | ||
|
||
#[derive(Debug, Clone, Copy, Eq)] | ||
pub enum DateTime { | ||
#[cfg(feature = "time")] | ||
Time(time::OffsetDateTime), | ||
#[cfg(feature = "chrono")] | ||
Chrono(chrono::DateTime<chrono::Utc>) | ||
} | ||
|
||
/// API implemented for internal use. This is private. Everything else: public. | ||
pub(crate) trait InternalDateTime: From<DateTime> + Into<DateTime> { | ||
/// The max cookie date-time. | ||
const MAX: Self; | ||
|
||
/// The datetime right now. | ||
fn now() -> Self; | ||
|
||
/// UTC based (year, month, day, hour, minute, second, nanosecond). | ||
/// * date is ISO 8601 calendar date | ||
/// * month is 1-indexed | ||
fn destruct(&self) -> (i32, u32, u32, i32, u32, u32, u32); | ||
|
||
/// The datetime as a string suitable for use as a cookie expiration. | ||
fn expiration_format(&self) -> Option<String>; | ||
} | ||
|
||
impl PartialEq for DateTime { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.destruct() == other.destruct() | ||
} | ||
} | ||
|
||
impl std::hash::Hash for DateTime { | ||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { | ||
self.destruct().hash(state) | ||
} | ||
} | ||
|
||
impl PartialOrd for DateTime { | ||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { | ||
self.destruct().partial_cmp(&other.destruct()) | ||
} | ||
} | ||
|
||
impl Ord for DateTime { | ||
fn cmp(&self, other: &Self) -> std::cmp::Ordering { | ||
self.destruct().cmp(&other.destruct()) | ||
} | ||
} | ||
|
||
#[cfg(feature = "time")] | ||
mod time_impl { | ||
use super::*; | ||
|
||
impl From<DateTime> for time::OffsetDateTime { | ||
fn from(value: DateTime) -> Self { | ||
let (yr, mon, day, hr, min, sec, nano) = value.destruct(); | ||
todo!() | ||
} | ||
} | ||
|
||
impl From<time::OffsetDateTime> for DateTime { | ||
fn from(value: time::OffsetDateTime) -> Self { | ||
DateTime::Time(value) | ||
} | ||
} | ||
|
||
impl PartialEq<time::OffsetDateTime> for DateTime { | ||
fn eq(&self, other: &time::OffsetDateTime) -> bool { | ||
self.destruct().eq(&other.destruct()) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "chrono")] | ||
mod chrono_impl { | ||
use super::*; | ||
|
||
impl From<DateTime> for chrono::DateTime<chrono::Utc> { | ||
fn from(value: DateTime) -> Self { | ||
let (yr, mon, day, hr, min, sec, nano) = value.destruct(); | ||
todo!() | ||
} | ||
} | ||
|
||
impl From<chrono::DateTime<chrono::Utc>> for DateTime { | ||
fn from(value: chrono::DateTime<chrono::Utc>) -> Self { | ||
DateTime::Chrono(value) | ||
} | ||
} | ||
|
||
impl PartialEq<chrono::DateTime<chrono::Utc>> for DateTime { | ||
fn eq(&self, other: &chrono::DateTime<chrono::Utc>) -> bool { | ||
self.destruct().eq(&other.destruct()) | ||
} | ||
} | ||
} |
Oops, something went wrong.