pub struct SystemTime(_);
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.
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 occured! println!("Error: {:?}", e); } } }
impl SystemTime
[src]
fn now() -> SystemTime
Returns the system time corresponding to "now".
use std::time::SystemTime; let sys_time = SystemTime::now();
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).
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.
use std::time::SystemTime; let sys_time = SystemTime::now(); let difference = sys_time.duration_since(sys_time) .expect("SystemTime::duration_since failed"); println!("{:?}", difference);
fn elapsed(&self) -> Result<Duration, SystemTimeError>
Returns the amount of time elapsed since this system time was created.
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.
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.
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);
impl Copy for SystemTime
[src]
impl Clone for SystemTime
[src]
fn clone(&self) -> SystemTime
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl PartialEq for SystemTime
[src]
fn eq(&self, __arg_0: &SystemTime) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &SystemTime) -> bool
This method tests for !=
.
impl Eq for SystemTime
[src]
impl PartialOrd for SystemTime
[src]
fn partial_cmp(&self, __arg_0: &SystemTime) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, __arg_0: &SystemTime) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, __arg_0: &SystemTime) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, __arg_0: &SystemTime) -> bool
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, __arg_0: &SystemTime) -> bool
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl Ord for SystemTime
[src]
fn cmp(&self, __arg_0: &SystemTime) -> Ordering
This method returns an Ordering
between self
and other
. Read more
impl Add<Duration> for SystemTime
[src]
type Output = SystemTime
The resulting type after applying the +
operator
fn add(self, dur: Duration) -> SystemTime
The method for the +
operator
impl AddAssign<Duration> for SystemTime
fn add_assign(&mut self, other: Duration)
The method for the +=
operator
impl Sub<Duration> for SystemTime
[src]
type Output = SystemTime
The resulting type after applying the -
operator
fn sub(self, dur: Duration) -> SystemTime
The method for the -
operator
impl SubAssign<Duration> for SystemTime
fn sub_assign(&mut self, other: Duration)
The method for the -=
operator
impl Debug for SystemTime
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter.
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/time/struct.SystemTime.html