Struct Timestamp

Source
pub struct Timestamp(pub i64);
Expand description

A microsecond-precision UTC timestamp for use in Holochain’s actions.

It is assumed to be untrustworthy: it may contain times offset from the UNIX epoch with the full +/- i64 range. Most of these times are not representable by a chrono::DateTime<Utc> (which limits itself to a +/- i32 offset in days from Jan 1, 0AD and from 1970AD).

Also, most differences between two Timestamps are not representable by either a chrono::Duration (which limits itself to +/- i64 microseconds), nor by core::time::Duration (which limits itself to +’ve u64 seconds). Many constructions of these chrono and core::time types will panic!, so painful measures must be taken to avoid this outcome – it is not acceptable for our core Holochain algorithms to panic when accessing DHT Action information committed by other random Holochain nodes!

Timestamp implements Serialize and Display as rfc3339 time strings (if possible).

Supports +/- chrono::Duration directly. There is no Timestamp::now() method, since this is not supported by WASM; however, holochain_types provides a Timestamp::now() method.

Tuple Fields§

§0: i64

Microseconds from UNIX Epoch, positive or negative

Implementations§

Source§

impl Timestamp

Source

pub fn now() -> Timestamp

Returns the current system time as a Timestamp.

This is behind a feature because we need Timestamp to be WASM compatible, and chrono doesn’t have a now() implementation for WASM.

Source

pub fn checked_difference_signed(&self, rhs: &Timestamp) -> Option<TimeDelta>

Compute signed difference between two Timestamp, returning None if overflow occurred, or Some(chrono::Duration). Produces Duration for differences of up to +/- i64::MIN/MAX microseconds.

Source

pub fn checked_add_signed(&self, rhs: &TimeDelta) -> Option<Timestamp>

Add a signed chrono::Duration{ secs: i64, nanos: i32 } to a Timestamp.

Source

pub fn checked_sub_signed(&self, rhs: &TimeDelta) -> Option<Timestamp>

Subtracts a chrono::Duration from a Timestamp

Source§

impl Timestamp

Source

pub const ZERO: Timestamp

The Timestamp corresponding to the UNIX epoch

Source

pub const MIN: Timestamp

The smallest possible Timestamp

Source

pub const MAX: Timestamp

The largest possible Timestamp

Source

pub const HOLOCHAIN_EPOCH: Timestamp

Jan 1, 2022, 12:00:00 AM UTC

Source

pub fn max() -> Timestamp

Largest possible Timestamp.

Source

pub fn from_micros(micros: i64) -> Timestamp

Construct from microseconds

Source

pub fn as_micros(&self) -> i64

Access time as microseconds since UNIX epoch

Source

pub fn as_millis(&self) -> i64

Access time as milliseconds since UNIX epoch

Source

pub fn as_seconds_and_nanos(&self) -> (i64, u32)

Access seconds since UNIX epoch plus nanosecond offset

Source

pub fn checked_add(&self, rhs: &Duration) -> Option<Timestamp>

Add unsigned core::time::Duration{ secs: u64, nanos: u32 } to a Timestamp. See: https://doc.rust-lang.org/src/core/time.rs.html#53-56

Source

pub fn checked_sub(&self, rhs: &Duration) -> Option<Timestamp>

Sub unsigned core::time::Duration{ secs: u64, nanos: u32 } from a Timestamp.

Source

pub fn saturating_add(&self, rhs: &Duration) -> Timestamp

Add a duration, clamping to MAX if overflow

Source

pub fn saturating_sub(&self, rhs: &Duration) -> Timestamp

Subtract a duration, clamping to MIN if overflow

Source

pub fn saturating_from_dur(duration: &Duration) -> Timestamp

Create a Timestamp from a core::time::Duration saturating at i64::MAX.

Trait Implementations§

Source§

impl<D> Add<D> for &Timestamp
where D: Into<Duration>,

Source§

type Output = Result<Timestamp, TimestampError>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: D) -> <&Timestamp as Add<D>>::Output

Performs the + operation. Read more
Source§

impl<D> Add<D> for Timestamp
where D: Into<Duration>,

Timestamp +/- Intocore::time::Duration: Anything that can be converted into a core::time::Duration can be used as an overflow-checked offset (unsigned) for a Timestamp. A core::time::Duration allows only +’ve offsets

Source§

type Output = Result<Timestamp, TimestampError>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: D) -> <Timestamp as Add<D>>::Output

Performs the + operation. Read more
Source§

impl Clone for Timestamp

Source§

fn clone(&self) -> Timestamp

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 Timestamp

Source§

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

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

impl<'de> Deserialize<'de> for Timestamp

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Timestamp, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

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

impl Display for Timestamp

Display as RFC3339 Date+Time for sane value ranges (0000-9999AD). Beyond that, format as (seconds, nanoseconds) tuple (output and parsing of large +/- years is unreliable).

Source§

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

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

impl From<&DateTime<Utc>> for Timestamp

Source§

fn from(t: &DateTime<Utc>) -> Timestamp

Converts to this type from the input type.
Source§

impl From<&HumanTimestamp> for Timestamp

Source§

fn from(h: &HumanTimestamp) -> Timestamp

Converts to this type from the input type.
Source§

impl From<DateTime<Utc>> for Timestamp

Source§

fn from(t: DateTime<Utc>) -> Timestamp

Converts to this type from the input type.
Source§

impl From<HumanTimestamp> for Timestamp

Source§

fn from(h: HumanTimestamp) -> Timestamp

Converts to this type from the input type.
Source§

impl From<Timestamp> for HumanTimestamp

Source§

fn from(t: Timestamp) -> HumanTimestamp

Converts to this type from the input type.
Source§

impl FromStr for Timestamp

Source§

type Err = TimestampError

The associated error which can be returned from parsing.
Source§

fn from_str(t: &str) -> Result<Timestamp, <Timestamp as FromStr>::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Timestamp

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Timestamp

Source§

fn cmp(&self, other: &Timestamp) -> 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 Timestamp

Source§

fn eq(&self, other: &Timestamp) -> 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 Timestamp

Source§

fn partial_cmp(&self, other: &Timestamp) -> 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 Timestamp

Source§

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

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

impl<D> Sub<D> for &Timestamp
where D: Into<Duration>,

Source§

type Output = Result<Timestamp, TimestampError>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: D) -> <&Timestamp as Sub<D>>::Output

Performs the - operation. Read more
Source§

impl<D> Sub<D> for Timestamp
where D: Into<Duration>,

Timestamp - core::time::Duration.

Source§

type Output = Result<Timestamp, TimestampError>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: D) -> <Timestamp as Sub<D>>::Output

Performs the - operation. Read more
Source§

impl Sub for Timestamp

Distance between two Timestamps as a chrono::Duration (subject to overflow). A Timestamp represents a signed distance from the UNIX Epoch (1970-01-01T00:00:00Z). A chrono::Duration is limited to +/- i64::MIN/MAX microseconds.

Source§

type Output = Result<TimeDelta, TimestampError>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Timestamp) -> <Timestamp as Sub>::Output

Performs the - operation. Read more
Source§

impl TryFrom<&String> for Timestamp

Source§

type Error = TimestampError

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

fn try_from( t: &String, ) -> Result<Timestamp, <Timestamp as TryFrom<&String>>::Error>

Performs the conversion.
Source§

impl TryFrom<&Timestamp> for DateTime<Utc>

Source§

type Error = TimestampError

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

fn try_from( t: &Timestamp, ) -> Result<DateTime<Utc>, <DateTime<Utc> as TryFrom<&Timestamp>>::Error>

Performs the conversion.
Source§

impl TryFrom<&str> for Timestamp

Source§

type Error = TimestampError

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

fn try_from(t: &str) -> Result<Timestamp, <Timestamp as TryFrom<&str>>::Error>

Performs the conversion.
Source§

impl TryFrom<Duration> for Timestamp

Source§

type Error = TimestampError

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

fn try_from( value: Duration, ) -> Result<Timestamp, <Timestamp as TryFrom<Duration>>::Error>

Performs the conversion.
Source§

impl TryFrom<String> for Timestamp

Source§

type Error = TimestampError

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

fn try_from( t: String, ) -> Result<Timestamp, <Timestamp as TryFrom<String>>::Error>

Performs the conversion.
Source§

impl TryFrom<Timestamp> for DateTime<Utc>

Source§

type Error = TimestampError

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

fn try_from( t: Timestamp, ) -> Result<DateTime<Utc>, <DateTime<Utc> as TryFrom<Timestamp>>::Error>

Performs the conversion.
Source§

impl Copy for Timestamp

Source§

impl Eq for Timestamp

Source§

impl StructuralPartialEq for Timestamp

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> 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.
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
Source§

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