Struct deltachat::tools::Time

1.8.0 · source ·
pub struct Time(SystemTime);
Expand description

A measurement of the system clock, useful for talking to external entities like the file system or other processes.

Distinct from the Instant type, this time measurement is not monotonic. This means that you can save a file to the file system, then save another file to the file system, and the second file has a SystemTime measurement earlier than the first. In other words, an operation that happens after another operation in real time may have an earlier SystemTime!

Consequently, comparing two SystemTime instances to learn about the duration between them returns a Result instead of an infallible Duration to indicate that this sort of time drift may happen and needs to be handled.

Although a SystemTime cannot be directly inspected, the UNIX_EPOCH constant is provided in this module as an anchor in time to learn information about a SystemTime. By calculating the duration from this fixed point in time, a SystemTime can be converted to a human-readable time, or perhaps some other string representation.

The size of a SystemTime struct may vary depending on the target operating system.

A SystemTime does not count leap seconds. SystemTime::now()’s behaviour around a leap second is the same as the operating system’s wall clock. The precise behaviour near a leap second (e.g. whether the clock appears to run slow or fast, or stop, or jump) depends on platform and configuration, so should not be relied on.

Example:

use std::time::{Duration, SystemTime};
use std::thread::sleep;

fn main() {
   let now = SystemTime::now();

   // we sleep for 2 seconds
   sleep(Duration::new(2, 0));
   match now.elapsed() {
       Ok(elapsed) => {
           // it prints '2'
           println!("{}", elapsed.as_secs());
       }
       Err(e) => {
           // an error occurred!
           println!("Error: {e:?}");
       }
   }
}

§Platform-specific behavior

The precision of SystemTime can depend on the underlying OS-specific time format. For example, on Windows the time is represented in 100 nanosecond intervals whereas Linux can represent nanosecond intervals.

The following system calls are currently being used by now() to find out the current time:

Disclaimer: These system calls might change over time.

Note: mathematical operations like add may panic if the underlying structure cannot represent the new point in time.

Tuple Fields§

§0: SystemTime

Implementations§

source§

impl SystemTime

1.28.0 · source

pub const UNIX_EPOCH: SystemTime = UNIX_EPOCH

An anchor in time which can be used to create new SystemTime instances or learn about where in time a SystemTime lies.

This constant is defined to be “1970-01-01 00:00:00 UTC” on all systems with respect to the system clock. Using duration_since on an existing SystemTime instance can tell how far away from this point in time a measurement lies, and using UNIX_EPOCH + duration can be used to create a SystemTime instance to represent another fixed point in time.

duration_since(UNIX_EPOCH).unwrap().as_secs() returns the number of non-leap seconds since the start of 1970 UTC. This is a POSIX time_t (as a u64), and is the same time representation as used in many Internet protocols.

§Examples
use std::time::SystemTime;

match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
    Ok(n) => println!("1970-01-01 00:00:00 UTC was {} seconds ago!", n.as_secs()),
    Err(_) => panic!("SystemTime before UNIX EPOCH!"),
}
1.8.0 · source

pub fn now() -> SystemTime

Returns the system time corresponding to “now”.

§Examples
use std::time::SystemTime;

let sys_time = SystemTime::now();
1.8.0 · source

pub fn duration_since( &self, earlier: SystemTime, ) -> Result<Duration, SystemTimeError>

Returns the amount of time elapsed from an earlier point in time.

This function may fail because measurements taken earlier are not guaranteed to always be before later measurements (due to anomalies such as the system clock being adjusted either forwards or backwards). Instant can be used to measure elapsed time without this risk of failure.

If successful, Ok(Duration) is returned where the duration represents the amount of time elapsed from the specified measurement to this one.

Returns an Err if earlier is later than self, and the error contains how far from self the time is.

§Examples
use std::time::SystemTime;

let sys_time = SystemTime::now();
let new_sys_time = SystemTime::now();
let difference = new_sys_time.duration_since(sys_time)
    .expect("Clock may have gone backwards");
println!("{difference:?}");
1.8.0 · source

pub fn elapsed(&self) -> Result<Duration, SystemTimeError>

Returns the difference from this system time to the current clock time.

This function may fail as the underlying system clock is susceptible to drift and updates (e.g., the system clock could go backwards), so this function might not always succeed. If successful, Ok(Duration) is returned where the duration represents the amount of time elapsed from this time measurement to the current time.

To measure elapsed time reliably, use Instant instead.

Returns an Err if self is later than the current system time, and the error contains how far from the current system time self is.

§Examples
use std::thread::sleep;
use std::time::{Duration, SystemTime};

let sys_time = SystemTime::now();
let one_sec = Duration::from_secs(1);
sleep(one_sec);
assert!(sys_time.elapsed().unwrap() >= one_sec);
1.34.0 · source

pub fn checked_add(&self, duration: Duration) -> Option<SystemTime>

Returns Some(t) where t is the time self + duration if t can be represented as SystemTime (which means it’s inside the bounds of the underlying data structure), None otherwise.

1.34.0 · source

pub fn checked_sub(&self, duration: Duration) -> Option<SystemTime>

Returns Some(t) where t is the time self - duration if t can be represented as SystemTime (which means it’s inside the bounds of the underlying data structure), None otherwise.

Trait Implementations§

1.8.0 · source§

impl Add<Duration> for SystemTime

source§

fn add(self, dur: Duration) -> SystemTime

§Panics

This function may panic if the resulting point in time cannot be represented by the underlying data structure. See SystemTime::checked_add for a version without panic.

source§

type Output = SystemTime

The resulting type after applying the + operator.
§

impl Add<Duration> for SystemTime

§

type Output = SystemTime

The resulting type after applying the + operator.
§

fn add(self, duration: Duration) -> <SystemTime as Add<Duration>>::Output

Performs the + operation. Read more
§

impl Add<Nanos> for SystemTime

§

type Output = SystemTime

The resulting type after applying the + operator.
§

fn add(self, other: Nanos) -> SystemTime

Performs the + operation. Read more
1.9.0 · source§

impl AddAssign<Duration> for SystemTime

source§

fn add_assign(&mut self, other: Duration)

Performs the += operation. Read more
§

impl AddAssign<Duration> for SystemTime

§

fn add_assign(&mut self, rhs: Duration)

Performs the += operation. Read more
1.8.0 · source§

impl Clone for SystemTime

source§

fn clone(&self) -> SystemTime

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
1.8.0 · source§

impl Debug for SystemTime

source§

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

Formats the value using the given formatter. Read more
§

impl<'a> DecodeValue<'a> for SystemTime

§

fn decode_value<R>(reader: &mut R, header: Header) -> Result<SystemTime, Error>
where R: Reader<'a>,

Attempt to decode this message using the provided [Reader].
source§

impl<'de> Deserialize<'de> for SystemTime

source§

fn deserialize<D>( deserializer: D, ) -> Result<SystemTime, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

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

impl EncodeValue for SystemTime

§

fn value_len(&self) -> Result<Length, Error>

Compute the length of this value (sans [Tag]+[Length] header) when encoded as ASN.1 DER.
§

fn encode_value(&self, writer: &mut impl Writer) -> Result<(), Error>

Encode value (sans [Tag]+[Length] header) as ASN.1 DER using the provided [Writer].
§

fn header(&self) -> Result<Header, Error>
where Self: Tagged,

Get the [Header] used to encode this value.
§

impl FixedTag for SystemTime

§

const TAG: Tag = Tag::GeneralizedTime

ASN.1 tag
§

impl From<&DateTime> for SystemTime

§

fn from(time: &DateTime) -> SystemTime

Converts to this type from the input type.
§

impl From<&GeneralizedTime> for SystemTime

§

fn from(time: &GeneralizedTime) -> SystemTime

Converts to this type from the input type.
source§

impl<Tz> From<DateTime<Tz>> for SystemTime
where Tz: TimeZone,

source§

fn from(dt: DateTime<Tz>) -> SystemTime

Converts to this type from the input type.
§

impl From<DateTime> for SystemTime

§

fn from(time: DateTime) -> SystemTime

Converts to this type from the input type.
§

impl From<GeneralizedTime> for SystemTime

§

fn from(time: GeneralizedTime) -> SystemTime

Converts to this type from the input type.
§

impl From<HttpDate> for SystemTime

§

fn from(v: HttpDate) -> SystemTime

Converts to this type from the input type.
§

impl From<OffsetDateTime> for SystemTime

§

fn from(datetime: OffsetDateTime) -> SystemTime

Converts to this type from the input type.
§

impl From<UnixTime> for SystemTime

§

fn from(unix_time: UnixTime) -> SystemTime

Converts to this type from the input type.
§

impl From<UtcTime> for SystemTime

§

fn from(utc_time: UtcTime) -> SystemTime

Converts to this type from the input type.
1.8.0 · source§

impl Hash for SystemTime

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
1.8.0 · source§

impl Ord for SystemTime

source§

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

Restrict a value to a certain interval. Read more
§

impl PartialEq<OffsetDateTime> for SystemTime

§

fn eq(&self, rhs: &OffsetDateTime) -> 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.
1.8.0 · source§

impl PartialEq for SystemTime

source§

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

impl PartialOrd<OffsetDateTime> for SystemTime

§

fn partial_cmp(&self, other: &OffsetDateTime) -> 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
1.8.0 · source§

impl PartialOrd for SystemTime

source§

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

impl Reference for SystemTime

§

fn duration_since(&self, earlier: SystemTime) -> Nanos

Returns the difference in times between the two SystemTimes. Due to the fallible nature of SystemTimes, returns the zero duration if a negative duration would result (e.g. due to system clock adjustments).

§

fn saturating_sub(&self, duration: Nanos) -> SystemTime

Returns a reference point that lies at most duration in the past from the current reference. If an underflow should occur, returns the current reference.
source§

impl Serialize for SystemTime

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
1.8.0 · source§

impl Sub<Duration> for SystemTime

source§

type Output = SystemTime

The resulting type after applying the - operator.
source§

fn sub(self, dur: Duration) -> SystemTime

Performs the - operation. Read more
§

impl Sub<Duration> for SystemTime

§

type Output = SystemTime

The resulting type after applying the - operator.
§

fn sub(self, duration: Duration) -> <SystemTime as Sub<Duration>>::Output

Performs the - operation. Read more
§

impl Sub<OffsetDateTime> for SystemTime

§

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

§Panics

This may panic if an overflow occurs.

§

type Output = Duration

The resulting type after applying the - operator.
1.9.0 · source§

impl SubAssign<Duration> for SystemTime

source§

fn sub_assign(&mut self, other: Duration)

Performs the -= operation. Read more
§

impl SubAssign<Duration> for SystemTime

§

fn sub_assign(&mut self, rhs: Duration)

Performs the -= operation. Read more
§

impl<'a> TryFrom<AnyRef<'a>> for SystemTime

§

type Error = Error

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

fn try_from(any: AnyRef<'a>) -> Result<SystemTime, Error>

Performs the conversion.
1.8.0 · source§

impl Copy for SystemTime

1.8.0 · source§

impl Eq for SystemTime

1.8.0 · source§

impl StructuralPartialEq for SystemTime

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
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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
§

impl<'a, T> Choice<'a> for T
where T: Decode<'a> + FixedTag,

§

fn can_decode(tag: Tag) -> bool

Is the provided [Tag] decodable as a variant of this CHOICE?
source§

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

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

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

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

§

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

Compare self to key and return their ordering.
§

impl<'a, T> Decode<'a> for T
where T: DecodeValue<'a> + FixedTag,

§

fn decode<R>(reader: &mut R) -> Result<T, Error>
where R: Reader<'a>,

Attempt to decode this message using the provided decoder.
§

fn from_der(bytes: &'a [u8]) -> Result<Self, Error>

Parse Self from the provided DER-encoded byte slice.
§

impl<T> DerOrd for T
where T: EncodeValue + ValueOrd + Tagged,

§

fn der_cmp(&self, other: &T) -> Result<Ordering, Error>

Return an Ordering between self and other when serialized as ASN.1 DER.
source§

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

§

impl<T> Encode for T
where T: EncodeValue + Tagged,

§

fn encoded_len(&self) -> Result<Length, Error>

Compute the length of this value in bytes when encoded as ASN.1 DER.

§

fn encode(&self, writer: &mut impl Writer) -> Result<(), Error>

Encode this value as ASN.1 DER using the provided [Writer].

§

fn encode_to_slice<'a>(&self, buf: &'a mut [u8]) -> Result<&'a [u8], Error>

Encode this value to the provided byte slice, returning a sub-slice containing the encoded message.
§

fn encode_to_vec(&self, buf: &mut Vec<u8>) -> Result<Length, Error>

Encode this message as ASN.1 DER, appending it to the provided byte vector.
§

fn to_der(&self) -> Result<Vec<u8>, Error>

Encode this type as DER, returning a byte vector.
§

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

§

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

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

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

§

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.

§

impl<T> Instrument for T

§

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

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

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
§

impl<T> Tagged for T
where T: FixedTag,

§

fn tag(&self) -> Tag

Get the ASN.1 tag that this type is encoded with.
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, 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

§

impl<T> ValueOrd for T
where T: OrdIsValueOrd,

§

fn value_cmp(&self, other: &T) -> Result<Ordering, Error>

Return an Ordering between value portion of TLV-encoded self and other when serialized as ASN.1 DER.
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> DecodeOwned for T
where T: for<'a> Decode<'a>,

source§

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

§

impl<T, Rhs, Output> GroupOps<Rhs, Output> for T
where T: Add<Rhs, Output = Output> + Sub<Rhs, Output = Output> + AddAssign<Rhs> + SubAssign<Rhs>,

§

impl<T> PeerIdentity for T

source§

impl<T> RuleType for T
where T: Copy + Debug + Eq + Hash + Ord,