Skip to main content

NumericDate

Struct NumericDate 

Source
pub struct NumericDate {
    timestamp: u64,
}
Expand description

Represents the value of the iat, exp, nbf claims.

According to RFC7519, it should be:

“A JSON numeric value representing the number of seconds from 1970-01-01T00:00:00Z UTC until the specified UTC date/time, ignoring leap seconds. This is equivalent to the IEEE Std 1003.1, 2013 Edition [POSIX.1] definition “Seconds Since the Epoch”, in which each day is accounted for by exactly 86400 seconds, other than that non-integer values can be represented.“

But contrary to this, we reject negative timestamps — and timestamps beyond the invariant upper bound below (the last second of year 9999) — with an error, and silently round down non-negative decimals to the nearest u64.

§Invariant

timestamp <= MAX_TIMESTAMP_SECS, the last second of year 9999 and the largest instant representable in RFC3339. Construction and arithmetic go through new_clamp, add_clamp and sub_clamp, which saturate into range; the Deserialize and TryFrom<SystemTime> impls instead reject an out-of-range value with an error.

The bound is a safety property, not just tidiness. The exp/nbf/iat claims are attacker-controlled, and a value near u64::MAX overflows std::time::SystemTime — whose addition panics above i64::MAX seconds — the moment the date is formatted into a log or error message (e.g. the “not yet valid” message built from a bogus far-future nbf). Staying in range also keeps humantime rendering the year correctly. Because the invariant holds, the From<&NumericDate> for SystemTime conversion is total and lossless, so serializing and then deserializing a NumericDate round-trips faithfully.

The range operations are named (*_clamp) rather than +/-/new so the saturating behaviour is explicit at the call site and never silently surprises the reader.

Fields§

§timestamp: u64

Implementations§

Source§

impl NumericDate

Source

pub fn new_clamp(timestamp: u64) -> Self

Creates a numeric date from timestamp, the number of seconds since the unix epoch (ignoring leap seconds), saturating at MAX_TIMESTAMP_SECS to uphold the invariant.

Source

pub fn add_clamp(self, secs: u64) -> Self

Adds secs seconds, saturating at MAX_TIMESTAMP_SECS.

Source

pub fn sub_clamp(self, secs: u64) -> Self

Subtracts secs seconds, saturating at zero.

Source

pub fn now() -> Self

Creates a numeric date representing the current moment

Source

pub fn timestamp(&self) -> u64

Returns the number of seconds this date is after the unix epoch.

Source

pub fn date(&self) -> String

Returns the UTC date for this timestamp

Trait Implementations§

Source§

impl Clone for NumericDate

Source§

fn clone(&self) -> NumericDate

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NumericDate

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for NumericDate

Source§

fn default() -> NumericDate

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for NumericDate

Source§

fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for NumericDate

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&NumericDate> for SystemTime

Source§

fn from(nd: &NumericDate) -> Self

Converts to this type from the input type.
Source§

impl From<NumericDate> for Epoch

Source§

fn from(nd: NumericDate) -> Self

Converts to this type from the input type.
Source§

impl Ord for NumericDate

Source§

fn cmp(&self, other: &NumericDate) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for NumericDate

Source§

fn eq(&self, other: &NumericDate) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for NumericDate

Source§

fn partial_cmp(&self, other: &NumericDate) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for NumericDate

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl TryFrom<SystemTime> for NumericDate

Source§

fn try_from(st: SystemTime) -> Result<Self, Self::Error>

Fallible (not From with a clamp) so a far-future SystemTime can never be silently rewritten — even if a future caller feeds in a time that isn’t the (sane) system clock.

Source§

type Error = OutOfRange

The type returned in the event of a conversion error.
Source§

impl Copy for NumericDate

Source§

impl Eq for NumericDate

Source§

impl StructuralPartialEq for NumericDate

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PayloadTrait for T

Source§

type JsonType = T

Source§

fn to_payload(&self) -> Payload<&T>

Used when creating requests
Source§

fn into_payload(self) -> Payload<T>

Used when forming responses
Source§

fn from_payload(payload: Payload<T>) -> Result<T, Error>

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,