Struct matrix_sdk_base::ruma::time::SystemTime
1.8.0 · source · pub struct SystemTime(/* private fields */);
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:
Platform | System call |
---|---|
SGX | insecure_time usercall. More information on timekeeping in SGX |
UNIX | clock_gettime (Realtime Clock) |
Darwin | clock_gettime (Realtime Clock) |
VXWorks | clock_gettime (Realtime Clock) |
SOLID | SOLID_RTC_ReadTime |
WASI | __wasi_clock_time_get (Realtime Clock) |
Windows | GetSystemTimePreciseAsFileTime / GetSystemTimeAsFileTime |
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.
Implementations§
source§impl SystemTime
impl SystemTime
1.28.0 · sourcepub const UNIX_EPOCH: SystemTime = UNIX_EPOCH
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 · sourcepub fn now() -> SystemTime
pub fn now() -> SystemTime
Returns the system time corresponding to “now”.
§Examples
use std::time::SystemTime;
let sys_time = SystemTime::now();
1.8.0 · sourcepub fn duration_since(
&self,
earlier: SystemTime,
) -> Result<Duration, SystemTimeError>
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 · sourcepub fn elapsed(&self) -> Result<Duration, SystemTimeError>
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 · sourcepub fn checked_add(&self, duration: Duration) -> Option<SystemTime>
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 · sourcepub fn checked_sub(&self, duration: Duration) -> Option<SystemTime>
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
impl Add<Duration> for SystemTime
source§fn add(self, dur: Duration) -> SystemTime
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.
§type Output = SystemTime
type Output = SystemTime
+
operator.source§impl Add<Duration> for SystemTime
Available on crate feature std
only.
impl Add<Duration> for SystemTime
std
only.1.9.0 · source§impl AddAssign<Duration> for SystemTime
impl AddAssign<Duration> for SystemTime
source§fn add_assign(&mut self, other: Duration)
fn add_assign(&mut self, other: Duration)
+=
operation. Read moresource§impl AddAssign<Duration> for SystemTime
Available on crate feature std
only.
impl AddAssign<Duration> for SystemTime
std
only.source§fn add_assign(&mut self, rhs: Duration)
fn add_assign(&mut self, rhs: Duration)
+=
operation. Read more1.8.0 · source§impl Clone for SystemTime
impl Clone for SystemTime
source§fn clone(&self) -> SystemTime
fn clone(&self) -> SystemTime
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<UT> ConvertError<UT> for SystemTime
impl<UT> ConvertError<UT> for SystemTime
1.8.0 · source§impl Debug for SystemTime
impl Debug for SystemTime
source§impl<'a> DecodeValue<'a> for SystemTime
Available on crate feature std
only.
impl<'a> DecodeValue<'a> for SystemTime
std
only.source§fn decode_value<R>(reader: &mut R, header: Header) -> Result<SystemTime, Error>where
R: Reader<'a>,
fn decode_value<R>(reader: &mut R, header: Header) -> Result<SystemTime, Error>where
R: Reader<'a>,
Reader
.source§impl<'de> Deserialize<'de> for SystemTime
Available on crate feature std
only.
impl<'de> Deserialize<'de> for SystemTime
std
only.source§fn deserialize<D>(
deserializer: D,
) -> Result<SystemTime, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<SystemTime, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
source§impl EncodeValue for SystemTime
Available on crate feature std
only.
impl EncodeValue for SystemTime
std
only.source§impl<UT> FfiConverter<UT> for SystemTime
impl<UT> FfiConverter<UT> for SystemTime
Support for passing timestamp values via the FFI.
Timestamps values are currently always passed by serializing to a buffer.
Timestamps are represented on the buffer by an i64 that indicates the direction and the magnitude in seconds of the offset from epoch, and a u32 that indicates the nanosecond portion of the offset magnitude. The nanosecond portion is expected to be between 0 and 999,999,999.
To build an epoch offset the absolute value of the seconds portion of the offset should be combined with the nanosecond portion. This is because the sign of the seconds portion represents the direction of the offset overall. The sign of the seconds portion can then be used to determine if the total offset should be added to or subtracted from the unix epoch.
§type FfiType = RustBuffer
type FfiType = RustBuffer
source§fn lower(v: SystemTime) -> RustBuffer
fn lower(v: SystemTime) -> RustBuffer
source§fn try_lift(buf: RustBuffer) -> Result<SystemTime, Error>
fn try_lift(buf: RustBuffer) -> Result<SystemTime, Error>
source§fn write(obj: SystemTime, buf: &mut Vec<u8>)
fn write(obj: SystemTime, buf: &mut Vec<u8>)
source§fn try_read(buf: &mut &[u8]) -> Result<SystemTime, Error>
fn try_read(buf: &mut &[u8]) -> Result<SystemTime, Error>
source§const TYPE_ID_META: MetadataBuffer = _
const TYPE_ID_META: MetadataBuffer = _
source§impl FixedTag for SystemTime
Available on crate feature std
only.
impl FixedTag for SystemTime
std
only.source§impl From<&DateTime> for SystemTime
Available on crate feature std
only.
impl From<&DateTime> for SystemTime
std
only.source§fn from(time: &DateTime) -> SystemTime
fn from(time: &DateTime) -> SystemTime
source§impl From<&GeneralizedTime> for SystemTime
Available on crate feature std
only.
impl From<&GeneralizedTime> for SystemTime
std
only.source§fn from(time: &GeneralizedTime) -> SystemTime
fn from(time: &GeneralizedTime) -> SystemTime
source§impl From<DateTime> for SystemTime
Available on crate feature std
only.
impl From<DateTime> for SystemTime
std
only.source§fn from(time: DateTime) -> SystemTime
fn from(time: DateTime) -> SystemTime
source§impl From<GeneralizedTime> for SystemTime
Available on crate feature std
only.
impl From<GeneralizedTime> for SystemTime
std
only.source§fn from(time: GeneralizedTime) -> SystemTime
fn from(time: GeneralizedTime) -> SystemTime
source§impl From<OffsetDateTime> for SystemTime
Available on crate feature std
only.
impl From<OffsetDateTime> for SystemTime
std
only.source§fn from(datetime: OffsetDateTime) -> SystemTime
fn from(datetime: OffsetDateTime) -> SystemTime
source§impl From<SystemTime> for OffsetDateTime
Available on crate feature std
only.
impl From<SystemTime> for OffsetDateTime
std
only.source§fn from(system_time: SystemTime) -> OffsetDateTime
fn from(system_time: SystemTime) -> OffsetDateTime
source§impl From<UtcTime> for SystemTime
Available on crate feature std
only.
impl From<UtcTime> for SystemTime
std
only.source§fn from(utc_time: UtcTime) -> SystemTime
fn from(utc_time: UtcTime) -> SystemTime
1.8.0 · source§impl Hash for SystemTime
impl Hash for SystemTime
source§impl<UT> Lift<UT> for SystemTime
impl<UT> Lift<UT> for SystemTime
type FfiType = <SystemTime as FfiConverter<UT>>::FfiType
fn try_lift(v: <SystemTime as Lift<UT>>::FfiType) -> Result<SystemTime, Error>
fn try_read(buf: &mut &[u8]) -> Result<SystemTime, Error>
source§fn try_lift_from_rust_buffer(v: RustBuffer) -> Result<Self, Error>
fn try_lift_from_rust_buffer(v: RustBuffer) -> Result<Self, Error>
source§impl<UT> LiftRef<UT> for SystemTime
impl<UT> LiftRef<UT> for SystemTime
type LiftType = SystemTime
source§impl<UT> LiftReturn<UT> for SystemTime
impl<UT> LiftReturn<UT> for SystemTime
§type ReturnType = <SystemTime as Lift<UT>>::FfiType
type ReturnType = <SystemTime as Lift<UT>>::FfiType
source§fn try_lift_successful_return(
v: <SystemTime as LiftReturn<UT>>::ReturnType,
) -> Result<SystemTime, Error>
fn try_lift_successful_return( v: <SystemTime as LiftReturn<UT>>::ReturnType, ) -> Result<SystemTime, Error>
source§fn lift_foreign_return(
ffi_return: Self::ReturnType,
call_status: RustCallStatus,
) -> Self
fn lift_foreign_return( ffi_return: Self::ReturnType, call_status: RustCallStatus, ) -> Self
source§fn lift_error(_buf: RustBuffer) -> Self
fn lift_error(_buf: RustBuffer) -> Self
source§fn handle_callback_unexpected_error(e: UnexpectedUniFFICallbackError) -> Self
fn handle_callback_unexpected_error(e: UnexpectedUniFFICallbackError) -> Self
source§impl<UT> Lower<UT> for SystemTime
impl<UT> Lower<UT> for SystemTime
type FfiType = <SystemTime as FfiConverter<UT>>::FfiType
fn lower(obj: SystemTime) -> <SystemTime as Lower<UT>>::FfiType
fn write(obj: SystemTime, buf: &mut Vec<u8>)
source§fn lower_into_rust_buffer(obj: Self) -> RustBuffer
fn lower_into_rust_buffer(obj: Self) -> RustBuffer
source§impl<UT> LowerError<UT> for SystemTime
impl<UT> LowerError<UT> for SystemTime
source§fn lower_error(obj: SystemTime) -> RustBuffer
fn lower_error(obj: SystemTime) -> RustBuffer
source§impl<UT> LowerReturn<UT> for SystemTime
impl<UT> LowerReturn<UT> for SystemTime
§type ReturnType = <SystemTime as Lower<UT>>::FfiType
type ReturnType = <SystemTime as Lower<UT>>::FfiType
source§fn lower_return(
obj: SystemTime,
) -> Result<<SystemTime as LowerReturn<UT>>::ReturnType, RustBuffer>
fn lower_return( obj: SystemTime, ) -> Result<<SystemTime as LowerReturn<UT>>::ReturnType, RustBuffer>
1.8.0 · source§impl Ord for SystemTime
impl Ord for SystemTime
source§fn cmp(&self, other: &SystemTime) -> Ordering
fn cmp(&self, other: &SystemTime) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl PartialEq<OffsetDateTime> for SystemTime
Available on crate feature std
only.
impl PartialEq<OffsetDateTime> for SystemTime
std
only.source§fn eq(&self, rhs: &OffsetDateTime) -> bool
fn eq(&self, rhs: &OffsetDateTime) -> bool
self
and other
values to be equal, and is used
by ==
.1.8.0 · source§impl PartialEq for SystemTime
impl PartialEq for SystemTime
source§fn eq(&self, other: &SystemTime) -> bool
fn eq(&self, other: &SystemTime) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd<OffsetDateTime> for SystemTime
Available on crate feature std
only.
impl PartialOrd<OffsetDateTime> for SystemTime
std
only.source§fn partial_cmp(&self, other: &OffsetDateTime) -> Option<Ordering>
fn partial_cmp(&self, other: &OffsetDateTime) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more1.8.0 · source§impl PartialOrd for SystemTime
impl PartialOrd for SystemTime
source§fn partial_cmp(&self, other: &SystemTime) -> Option<Ordering>
fn partial_cmp(&self, other: &SystemTime) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl Serialize for SystemTime
Available on crate feature std
only.
impl Serialize for SystemTime
std
only.source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
1.8.0 · source§impl Sub<Duration> for SystemTime
impl Sub<Duration> for SystemTime
§type Output = SystemTime
type Output = SystemTime
-
operator.source§impl Sub<Duration> for SystemTime
Available on crate feature std
only.
impl Sub<Duration> for SystemTime
std
only.source§impl Sub<OffsetDateTime> for SystemTime
Available on crate feature std
only.
impl Sub<OffsetDateTime> for SystemTime
std
only.source§fn sub(self, rhs: OffsetDateTime) -> <SystemTime as Sub<OffsetDateTime>>::Output
fn sub(self, rhs: OffsetDateTime) -> <SystemTime as Sub<OffsetDateTime>>::Output
§Panics
This may panic if an overflow occurs.
1.9.0 · source§impl SubAssign<Duration> for SystemTime
impl SubAssign<Duration> for SystemTime
source§fn sub_assign(&mut self, other: Duration)
fn sub_assign(&mut self, other: Duration)
-=
operation. Read moresource§impl SubAssign<Duration> for SystemTime
Available on crate feature std
only.
impl SubAssign<Duration> for SystemTime
std
only.source§fn sub_assign(&mut self, rhs: Duration)
fn sub_assign(&mut self, rhs: Duration)
-=
operation. Read moresource§impl TryFrom<&SystemTime> for DateTime
Available on crate feature std
only.
impl TryFrom<&SystemTime> for DateTime
std
only.source§impl TryFrom<&SystemTime> for GeneralizedTime
Available on crate feature std
only.
impl TryFrom<&SystemTime> for GeneralizedTime
std
only.source§fn try_from(time: &SystemTime) -> Result<GeneralizedTime, Error>
fn try_from(time: &SystemTime) -> Result<GeneralizedTime, Error>
source§impl<'a> TryFrom<AnyRef<'a>> for SystemTime
Available on crate feature std
only.
impl<'a> TryFrom<AnyRef<'a>> for SystemTime
std
only.source§impl TryFrom<SystemTime> for DateTime
Available on crate feature std
only.
impl TryFrom<SystemTime> for DateTime
std
only.source§impl TryFrom<SystemTime> for GeneralizedTime
Available on crate feature std
only.
impl TryFrom<SystemTime> for GeneralizedTime
std
only.source§fn try_from(time: SystemTime) -> Result<GeneralizedTime, Error>
fn try_from(time: SystemTime) -> Result<GeneralizedTime, Error>
source§impl<UT> TypeId<UT> for SystemTime
impl<UT> TypeId<UT> for SystemTime
const TYPE_ID_META: MetadataBuffer = _
impl Copy for SystemTime
impl Eq for SystemTime
impl StructuralPartialEq for SystemTime
Auto Trait Implementations§
impl Freeze for SystemTime
impl RefUnwindSafe for SystemTime
impl Send for SystemTime
impl Sync for SystemTime
impl Unpin for SystemTime
impl UnwindSafe for SystemTime
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<T> CompatExt for T
impl<T> CompatExt for T
source§impl<'a, T> Decode<'a> for Twhere
T: DecodeValue<'a> + FixedTag,
impl<'a, T> Decode<'a> for Twhere
T: DecodeValue<'a> + FixedTag,
source§impl<T> Encode for Twhere
T: EncodeValue + Tagged,
impl<T> Encode for Twhere
T: EncodeValue + Tagged,
source§fn encoded_len(&self) -> Result<Length, Error>
fn encoded_len(&self) -> Result<Length, Error>
Compute the length of this value in bytes when encoded as ASN.1 DER.
source§fn encode(&self, writer: &mut impl Writer) -> Result<(), Error>
fn encode(&self, writer: &mut impl Writer) -> Result<(), Error>
Encode this value as ASN.1 DER using the provided Writer
.
source§fn encode_to_slice<'a>(&self, buf: &'a mut [u8]) -> Result<&'a [u8], Error>
fn encode_to_slice<'a>(&self, buf: &'a mut [u8]) -> Result<&'a [u8], Error>
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<T, UT> HandleAlloc<UT> for T
impl<T, UT> HandleAlloc<UT> for T
source§fn consume_handle(handle: Handle) -> Arc<T>
fn consume_handle(handle: Handle) -> Arc<T>
Arc<>
source§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more