Struct hdk::prelude::Timestamp

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§

§

impl Timestamp

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

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.

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

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

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

Subtracts a chrono::Duration from a Timestamp

§

impl Timestamp

pub const ZERO: Timestamp = Timestamp(0)

The Timestamp corresponding to the UNIX epoch

pub const MIN: Timestamp = Timestamp(i64::MIN)

The smallest possible Timestamp

pub const MAX: Timestamp = Timestamp(i64::MAX)

The largest possible Timestamp

pub const HOLOCHAIN_EPOCH: Timestamp = Timestamp(1640995200000000)

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

pub fn max() -> Timestamp

Largest possible Timestamp.

pub fn from_micros(micros: i64) -> Timestamp

Construct from microseconds

pub fn as_micros(&self) -> i64

Access time as microseconds since UNIX epoch

pub fn as_millis(&self) -> i64

Access time as milliseconds since UNIX epoch

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

Access seconds since UNIX epoch plus nanosecond offset

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

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

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

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

Add a duration, clamping to MAX if overflow

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

Subtract a duration, clamping to MIN if overflow

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

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

pub fn into_sql_lossy(self) -> Timestamp

Convert this timestamp to fit into a SQLite integer which is an i64. The value will be clamped to the valid range supported by SQLite

Trait Implementations§

§

impl<D> Add<D> for &Timestampwhere
    D: Into<Duration>,

§

type Output = Result<Timestamp, TimestampError>

The resulting type after applying the + operator.
§

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

Performs the + operation. Read more
§

impl<D> Add<D> for Timestampwhere
    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

§

type Output = Result<Timestamp, TimestampError>

The resulting type after applying the + operator.
§

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

Performs the + operation. Read more
§

impl Clone for Timestamp

§

fn clone(&self) -> Timestamp

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
§

impl Debug for Timestamp

§

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

Formats the value using the given formatter. Read more
§

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

§

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
§

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).

§

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

Formats the value using the given formatter. Read more
§

impl From<&DateTime<Utc>> for Timestamp

§

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

Converts to this type from the input type.
§

impl From<&HumanTimestamp> for Timestamp

§

fn from(h: &HumanTimestamp) -> Timestamp

Converts to this type from the input type.
§

impl From<DateTime<Utc>> for Timestamp

§

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

Converts to this type from the input type.
§

impl From<HumanTimestamp> for Timestamp

§

fn from(h: HumanTimestamp) -> Timestamp

Converts to this type from the input type.
§

impl From<Timestamp> for HumanTimestamp

§

fn from(t: Timestamp) -> HumanTimestamp

Converts to this type from the input type.
§

impl FromStr for Timestamp

§

type Err = TimestampError

The associated error which can be returned from parsing.
§

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

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

impl Hash for Timestamp

§

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
§

impl Ord for Timestamp

§

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) -> Selfwhere
    Self: Sized,

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

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

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

fn clamp(self, min: Self, max: Self) -> Selfwhere
    Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
§

impl PartialEq<Timestamp> for Timestamp

§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

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

impl PartialOrd<Timestamp> for Timestamp

§

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

This method 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

This method 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

This method 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

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

impl Serialize for Timestamp

§

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
§

impl<D> Sub<D> for &Timestampwhere
    D: Into<Duration>,

§

type Output = Result<Timestamp, TimestampError>

The resulting type after applying the - operator.
§

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

Performs the - operation. Read more
§

impl<D> Sub<D> for Timestampwhere
    D: Into<Duration>,

Timestamp - core::time::Duration.

§

type Output = Result<Timestamp, TimestampError>

The resulting type after applying the - operator.
§

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

Performs the - operation. Read more
§

impl Sub<Timestamp> 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.

§

type Output = Result<Duration, TimestampError>

The resulting type after applying the - operator.
§

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

Performs the - operation. Read more
§

impl TryFrom<&String> for Timestamp

§

type Error = TimestampError

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

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

Performs the conversion.
§

impl TryFrom<&Timestamp> for DateTime<Utc>

§

type Error = TimestampError

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

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

Performs the conversion.
§

impl TryFrom<&str> for Timestamp

§

type Error = TimestampError

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

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

Performs the conversion.
§

impl TryFrom<Duration> for Timestamp

§

type Error = TimestampError

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

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

Performs the conversion.
§

impl TryFrom<String> for Timestamp

§

type Error = TimestampError

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

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

Performs the conversion.
§

impl TryFrom<Timestamp> for DateTime<Utc>

§

type Error = TimestampError

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

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

Performs the conversion.
§

impl Copy for Timestamp

§

impl Eq for Timestamp

§

impl StructuralEq for Timestamp

§

impl StructuralPartialEq for Timestamp

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> ArchivePointee for T

§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
§

fn pointer_metadata(
    _: &<T as ArchivePointee>::ArchivedMetadata
) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
§

impl<T> CallHasher for Twhere
    T: Hash + ?Sized,

§

default fn get_hash<H, B>(value: &H, build_hasher: &B) -> u64where
    H: Hash + ?Sized,
    B: BuildHasher,

§

impl<F, W, T, D> Deserialize<With<T, W>, D> for Fwhere
    W: DeserializeWith<F, T, D>,
    D: Fallible + ?Sized,
    F: ?Sized,

§

fn deserialize(
    &self,
    deserializer: &mut D
) -> Result<With<T, W>, <D as Fallible>::Error>

Deserializes using the given deserializer
source§

impl<Q, K> Equivalent<K> for Qwhere
    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

const: unstable · 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 Twhere
    U: From<T>,

const: unstable · 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Pointee for T

§

type Metadata = ()

The type for metadata in pointers and references to Self.
source§

impl<T> ToOwned for Twhere
    T: Clone,

§

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 Twhere
    T: Display + ?Sized,

source§

default fn to_string(&self) -> String

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

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

impl<T> Upcastable for Twhere
    T: Any + Send + Sync + 'static,

§

fn upcast_any_ref(&self) -> &(dyn Any + 'static)

upcast ref
§

fn upcast_any_mut(&mut self) -> &mut (dyn Any + 'static)

upcast mut ref
§

fn upcast_any_box(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>

upcast boxed dyn
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 Twhere
    T: for<'de> Deserialize<'de>,

§

impl<T> WritableBase for Twhere
    T: Copy + Clone + PartialEq<T> + Eq + Hash + PartialOrd<T> + Ord + Debug,