Struct wasm_timer::SystemTime 1.8.0[−][src]
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.
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); } } }
Underlying System calls
Currently, the following system calls are being used to get the current time using now()
:
Platform | System call |
---|---|
SGX | insecure_time usercall. More information on timekeeping in SGX |
UNIX | clock_gettime (Realtime Clock) |
Darwin | gettimeofday |
VXWorks | clock_gettime (Realtime Clock) |
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
impl SystemTime
[src]
pub const UNIX_EPOCH: SystemTime
1.28.0[src]
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.
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!"), }
pub fn now() -> SystemTime
[src]
Returns the system time corresponding to “now”.
Examples
use std::time::SystemTime; let sys_time = SystemTime::now();
pub fn duration_since(
&self,
earlier: SystemTime
) -> Result<Duration, SystemTimeError>
[src]
&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);
pub fn elapsed(&self) -> Result<Duration, SystemTimeError>
[src]
Returns the difference between the clock time when this system time was created, and 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 may 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);
pub fn checked_add(&self, duration: Duration) -> Option<SystemTime>
1.34.0[src]
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.
pub fn checked_sub(&self, duration: Duration) -> Option<SystemTime>
1.34.0[src]
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
impl Add<Duration> for SystemTime
[src]
type Output = SystemTime
The resulting type after applying the +
operator.
pub fn add(self, dur: Duration) -> SystemTime
[src]
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.
impl AddAssign<Duration> for SystemTime
1.9.0[src]
pub fn add_assign(&mut self, other: Duration)
[src]
impl Clone for SystemTime
[src]
pub fn clone(&self) -> SystemTime
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl Copy for SystemTime
[src]
impl Debug for SystemTime
[src]
impl Eq for SystemTime
[src]
impl Hash for SystemTime
[src]
pub fn hash<__H>(&self, state: &mut __H) where
__H: Hasher,
[src]
__H: Hasher,
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl Ord for SystemTime
[src]
pub fn cmp(&self, other: &SystemTime) -> Ordering
[src]
#[must_use]pub fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]
impl PartialEq<SystemTime> for SystemTime
[src]
pub fn eq(&self, other: &SystemTime) -> bool
[src]
pub fn ne(&self, other: &SystemTime) -> bool
[src]
impl PartialOrd<SystemTime> for SystemTime
[src]
pub fn partial_cmp(&self, other: &SystemTime) -> Option<Ordering>
[src]
#[must_use]pub fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn le(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
impl StructuralEq for SystemTime
[src]
impl StructuralPartialEq for SystemTime
[src]
impl Sub<Duration> for SystemTime
[src]
type Output = SystemTime
The resulting type after applying the -
operator.
pub fn sub(self, dur: Duration) -> SystemTime
[src]
impl SubAssign<Duration> for SystemTime
1.9.0[src]
pub fn sub_assign(&mut self, other: Duration)
[src]
Auto Trait Implementations
impl RefUnwindSafe for SystemTime
impl Send for SystemTime
impl Sync for SystemTime
impl Unpin for SystemTime
impl UnwindSafe for SystemTime
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,