Struct hdk::prelude::Timestamp[][src]

pub struct Timestamp(pub i64, pub u32);

A UTC timestamp for use in Holochain’s headers. 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 (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 milliseconds), 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 Header information committed by other random Holochain nodes!

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

  • Field 0: i64 - Seconds since UNIX epoch UTC (midnight 1970-01-01).
  • Field 1: u32 - Nanoseconds in addition to above seconds, always in positive direction.

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.

Create a new Timestamp instance from the supplied secs/nsecs. Note that we can easily create a Timestamp that cannot be converted to a valid DateTime (ie. by supplying 86,400-second days beyond range of +/- i32 offset from 0AD or 1970AD, nsecs beyond 1e9, etc.; see its code.)

Implementations

impl Timestamp[src]

pub fn normalize(secs: i64, nanos: i64) -> Option<Timestamp>[src]

Construct a normalized Timestamp from the given secs/nanos. Allows a full, signed range of seconds and/or nanoseconds; produces a Timestamp with a properly signed i64 seconds, and an always positive-offset u32 nanoseconds. Differs from typical new implementation in that it returns an Option.

use holochain_zome_types::prelude::*;
assert_eq!( Timestamp::normalize( 0, -1 ).unwrap(), Timestamp( -1, 999_999_999 ))

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

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 milliseconds (the full range of a signed chrono::Duration). Note that, surprisingly, there is almost no way to create a chrono::Duration that does not (directly or indirectly) have the possibility of panic! One of the few paths is Duration::milliseconds() and smaller (all larger use Duration::seconds, which may directly panic!), followed by a Duration::checked_add for the nanoseconds.

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

Add a signed chrono::Duration{ secs: i64, nanos: i32 } (-’ve nanos are invalid) to a Timestamp( i64, u32 ). May overflow. Unfortunately, there is no way in the provided API to actually obtain the raw { secs, nanos }, nor their component parts without overflow! The closest is to obtain the millis, subtract them out and obtain the residual nanoseconds…

use holochain_zome_types::prelude::*;

assert_eq!( Timestamp::normalize( 0, 1 ).unwrap()
                .checked_sub_signed(&chrono::Duration::nanoseconds(2)),
            Some(Timestamp( -1, 999_999_999 )));
//assert_eq!((Timestamp::normalize( 0, 1 ).unwrap()
//            - chrono::Duration::nanoseconds(2)),
//            Some(Timestamp( -1, 999_999_999 )));

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

Subtracts a chrono::Duration from a Timestamp

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

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

use holochain_zome_types::prelude::*;

assert_eq!( Timestamp::normalize( 0, -3 ).unwrap()
                .checked_add(&core::time::Duration::from_nanos(2)),
            Some(Timestamp( -1, 999_999_999 )));
assert_eq!( Timestamp::normalize( 0, 0 ).unwrap()
                .checked_add(&core::time::Duration::from_secs(2_u64.pow(32)-1)),
            Some(Timestamp( 2_i64.pow(32)-1, 0 )));
assert_eq!( Timestamp::normalize( 0, 0 ).unwrap()
                .checked_add(&core::time::Duration::from_secs(2_u64.pow(63)-1)),
            Some(Timestamp( (2_u64.pow(63)-1) as i64, 0 )));
assert_eq!( Timestamp::normalize( 0, 0 ).unwrap()
                .checked_add(&core::time::Duration::from_secs(2_u64.pow(63))),
            None);

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

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

use holochain_zome_types::prelude::*;

assert_eq!( Timestamp::normalize( 0, 1 ).unwrap()
                .checked_sub(&core::time::Duration::from_nanos(2)),
            Some(Timestamp( -1, 999_999_999 )));
assert_eq!((Timestamp::normalize( 0, 1 ).unwrap()
            - core::time::Duration::from_nanos(2)),
            Ok(Timestamp( -1, 999_999_999 )));
assert_eq!( Timestamp::normalize( 550, 5_500_000_000 ).unwrap()
                .checked_sub(&core::time::Duration::from_nanos(2)),
            Some(Timestamp( 555, 499_999_998 )));

Trait Implementations

impl<'_, D> Add<D> for &'_ Timestamp where
    D: Into<Duration>, 
[src]

type Output = Result<Timestamp, TimestampError>

The resulting type after applying the + operator.

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

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.

impl Clone for Timestamp[src]

impl Copy for Timestamp[src]

impl Debug for Timestamp[src]

impl<'de> Deserialize<'de> for Timestamp[src]

impl Display for Timestamp[src]

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

impl Eq for Timestamp[src]

impl<'_> From<&'_ DateTime<Utc>> for Timestamp[src]

impl From<DateTime<Utc>> for Timestamp[src]

impl From<i32> for Timestamp[src]

impl From<i64> for Timestamp[src]

Infallible conversions into a Timestamp. The only infallible ways to create a Timestamp are from a Unix timestamp, or normalize with a timestamp and nanoseconds, or converting from a DateTime.

impl From<u32> for Timestamp[src]

impl FromStr for Timestamp[src]

type Err = TimestampError

The associated error which can be returned from parsing.

impl Hash for Timestamp[src]

impl Ord for Timestamp[src]

impl PartialEq<Timestamp> for Timestamp[src]

impl PartialOrd<Timestamp> for Timestamp[src]

impl Serialize for Timestamp[src]

impl StructuralEq for Timestamp[src]

impl StructuralPartialEq for Timestamp[src]

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

Timestamp - core::time::Duration.

type Output = Result<Timestamp, TimestampError>

The resulting type after applying the - operator.

impl<'_, D> Sub<D> for &'_ Timestamp where
    D: Into<Duration>, 
[src]

type Output = Result<Timestamp, TimestampError>

The resulting type after applying the - operator.

impl Sub<Timestamp> for Timestamp[src]

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

type Output = Result<Duration, TimestampError>

The resulting type after applying the - operator.

impl<'_> TryFrom<&'_ String> for Timestamp[src]

type Error = TimestampError

The type returned in the event of a conversion error.

impl<'_> TryFrom<&'_ Timestamp> for SerializedBytes[src]

type Error = SerializedBytesError

The type returned in the event of a conversion error.

impl<'_> TryFrom<&'_ Timestamp> for DateTime<Utc>[src]

type Error = TimestampError

The type returned in the event of a conversion error.

impl<'_> TryFrom<&'_ str> for Timestamp[src]

type Error = TimestampError

The type returned in the event of a conversion error.

impl TryFrom<SerializedBytes> for Timestamp[src]

type Error = SerializedBytesError

The type returned in the event of a conversion error.

impl TryFrom<String> for Timestamp[src]

type Error = TimestampError

The type returned in the event of a conversion error.

impl TryFrom<Timestamp> for SerializedBytes[src]

type Error = SerializedBytesError

The type returned in the event of a conversion error.

impl TryFrom<Timestamp> for DateTime<Utc>[src]

type Error = TimestampError

The type returned in the event of a conversion error.

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

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

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

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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