Struct holochain_types::prelude::Timestamp
source · [−]Expand description
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
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
Tuple Fields
0: i64
1: u32
Implementations
sourceimpl Timestamp
impl Timestamp
sourcepub fn normalize(secs: i64, nanos: i64) -> Option<Timestamp>
pub fn normalize(secs: i64, nanos: i64) -> Option<Timestamp>
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 ))
sourcepub fn checked_difference_signed(&self, rhs: &Timestamp) -> Option<Duration>
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
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.
sourcepub fn checked_add_signed(&self, rhs: &Duration) -> Option<Timestamp>
pub fn checked_add_signed(&self, rhs: &Duration) -> Option<Timestamp>
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 )));
sourcepub fn checked_sub_signed(&self, rhs: &Duration) -> Option<Timestamp>
pub fn checked_sub_signed(&self, rhs: &Duration) -> Option<Timestamp>
Subtracts a chrono::Duration from a Timestamp
sourcepub fn checked_add(&self, rhs: &Duration) -> Option<Timestamp>
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
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);
sourcepub fn checked_sub(&self, rhs: &Duration) -> Option<Timestamp>
pub fn checked_sub(&self, rhs: &Duration) -> Option<Timestamp>
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 )));
sourcepub fn to_sql_ms_lossy(self) -> i64
pub fn to_sql_ms_lossy(self) -> i64
Convert this timestamp to fit into a sqlite integer which is an i64. The value will be clamped between 0 and i64::MAX.
Trait Implementations
sourceimpl<D> Add<D> for Timestamp where
D: Into<Duration>,
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
sourceimpl<'arbitrary> Arbitrary<'arbitrary> for Timestamp
impl<'arbitrary> Arbitrary<'arbitrary> for Timestamp
sourcepub fn arbitrary(u: &mut Unstructured<'arbitrary>) -> Result<Timestamp, Error>
pub fn arbitrary(u: &mut Unstructured<'arbitrary>) -> Result<Timestamp, Error>
Generate an arbitrary value of Self
from the given unstructured data. Read more
sourcepub fn arbitrary_take_rest(
u: Unstructured<'arbitrary>
) -> Result<Timestamp, Error>
pub fn arbitrary_take_rest(
u: Unstructured<'arbitrary>
) -> Result<Timestamp, Error>
Generate an arbitrary value of Self
from the entirety of the given unstructured data. Read more
sourceimpl<'de> Deserialize<'de> for Timestamp
impl<'de> Deserialize<'de> for Timestamp
sourcepub fn deserialize<__D>(
__deserializer: __D
) -> Result<Timestamp, <__D as Deserializer<'de>>::Error> where
__D: Deserializer<'de>,
pub 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
sourceimpl Display for Timestamp
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).
sourceimpl From<Timestamp> for TimestampKey
impl From<Timestamp> for TimestampKey
sourcefn from(t: Timestamp) -> TimestampKey
fn from(t: Timestamp) -> TimestampKey
Performs the conversion.
sourceimpl From<TimestampKey> for Timestamp
impl From<TimestampKey> for Timestamp
sourcefn from(k: TimestampKey) -> Timestamp
fn from(k: TimestampKey) -> Timestamp
Performs the conversion.
sourceimpl From<i64> for Timestamp
impl From<i64> for Timestamp
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
sourceimpl Ord for Timestamp
impl Ord for Timestamp
sourceimpl PartialOrd<Timestamp> for Timestamp
impl PartialOrd<Timestamp> for Timestamp
sourcepub fn partial_cmp(&self, other: &Timestamp) -> Option<Ordering>
pub 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 · sourcefn lt(&self, other: &Rhs) -> bool
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 · sourcefn le(&self, other: &Rhs) -> bool
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
sourceimpl Serialize for Timestamp
impl Serialize for Timestamp
sourcepub fn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error> where
__S: Serializer,
pub 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
sourceimpl Sub<Timestamp> for Timestamp
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 milliseconds.
sourceimpl TryFrom<&'_ Timestamp> for SerializedBytes
impl TryFrom<&'_ Timestamp> for SerializedBytes
type Error = SerializedBytesError
type Error = SerializedBytesError
The type returned in the event of a conversion error.
sourcepub fn try_from(t: &Timestamp) -> Result<SerializedBytes, SerializedBytesError>
pub fn try_from(t: &Timestamp) -> Result<SerializedBytes, SerializedBytesError>
Performs the conversion.
sourceimpl TryFrom<SerializedBytes> for Timestamp
impl TryFrom<SerializedBytes> for Timestamp
type Error = SerializedBytesError
type Error = SerializedBytesError
The type returned in the event of a conversion error.
sourcepub fn try_from(sb: SerializedBytes) -> Result<Timestamp, SerializedBytesError>
pub fn try_from(sb: SerializedBytes) -> Result<Timestamp, SerializedBytesError>
Performs the conversion.
sourceimpl TryFrom<Timestamp> for SerializedBytes
impl TryFrom<Timestamp> for SerializedBytes
type Error = SerializedBytesError
type Error = SerializedBytesError
The type returned in the event of a conversion error.
sourcepub fn try_from(t: Timestamp) -> Result<SerializedBytes, SerializedBytesError>
pub fn try_from(t: Timestamp) -> Result<SerializedBytes, SerializedBytesError>
Performs the conversion.
impl Copy for Timestamp
impl Eq for Timestamp
impl StructuralEq for Timestamp
impl StructuralPartialEq for Timestamp
Auto Trait Implementations
impl RefUnwindSafe for Timestamp
impl Send for Timestamp
impl Sync for Timestamp
impl Unpin for Timestamp
impl UnwindSafe for Timestamp
Blanket Implementations
impl<T> Any for T where
T: Any + ?Sized,
impl<T> Any for T where
T: Any + ?Sized,
fn type_id_compat(&self) -> TypeId
fn type_id_compat(&self) -> TypeId
TODO: once 1.33.0 is the minimum supported compiler version, remove Any::type_id_compat and use StdAny::type_id instead. https://github.com/rust-lang/rust/issues/27745 Read more
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcepub fn equivalent(&self, key: &K) -> bool
pub fn equivalent(&self, key: &K) -> bool
Compare self to key
and return true
if they are equal.
impl<T> FutureExt for T
impl<T> FutureExt for T
fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> Pointable for T
impl<T> Pointable for T
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
pub fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
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
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more