pub struct SystemTime(/* private fields */);
Expand description
A measurement of the system clock, useful for talking toexternal entities like the file system or other processes.
Distinct from theInstant
type, this time measurementis notmonotonic. This means that you can save a file to the file system, thensave another file to the file system,and the second file has aSystemTime
measurement earlier than the first. In other words, anoperation that happens after another operation in real time may have anearlierSystemTime
!
Consequently, comparing twoSystemTime
instances to learn about theduration between them returns aResult
instead of an infallibleDuration
to indicate that this sort of time drift may happen and needs to be handled.
Although aSystemTime
cannot be directly inspected, theUNIX_EPOCH
constant is provided in this module as an anchor in time to learninformation about aSystemTime
. By calculating the duration from thisfixed point in time, aSystemTime
can be converted to a human-readable time,or perhaps some other string representation.
The size of aSystemTime
struct may vary depending on the target operatingsystem.
ASystemTime
does not count leap seconds.SystemTime::now()
’s behavior around a leap secondis the same as the operating system’s wall clock.The precise behavior 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:
usestd::time::{Duration, SystemTime};usestd::thread::sleep;fnmain() {letnow = SystemTime::now();// we sleep for 2 secondssleep(Duration::new(2,0));matchnow.elapsed() {Ok(elapsed) => {// it prints '2'println!("{}", elapsed.as_secs()); }Err(e) => {// the system clock went backwards!println!("Great Scott! {e:?}"); } }}
§Platform-specific behavior
The precision ofSystemTime
can depend on the underlying OS-specific time format.For example, on Windows the time is represented in 100 nanosecond intervals whereas Linuxcan represent nanosecond intervals.
The following system calls arecurrently being used bynow()
to find outthe current time:
Platform | System call |
---|---|
SGX | insecure_time usercall. More information ontimekeeping 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 underlyingstructure cannot represent the new point in time.
Implementations§
Source§implSystemTime
implSystemTime
1.28.0 ·Sourcepub constUNIX_EPOCH:SystemTime = UNIX_EPOCH
pub constUNIX_EPOCH:SystemTime = UNIX_EPOCH
An anchor in time which can be used to create newSystemTime
instances orlearn about where in time aSystemTime
lies.
This constant is defined to be “1970-01-01 00:00:00 UTC” on all systems withrespect to the system clock. Usingduration_since
on an existingSystemTime
instance can tell how far away from this point in time ameasurement lies, and usingUNIX_EPOCH + duration
can be used to create aSystemTime
instance to represent another fixed point in time.
duration_since(UNIX_EPOCH).unwrap().as_secs()
returnsthe number of non-leap seconds since the start of 1970 UTC.This is a POSIXtime_t
(as au64
),and is the same time representation as used in many Internet protocols.
§Examples
1.8.0 ·Sourcepub fnnow() ->SystemTime
pub fnnow() ->SystemTime
Returns the system time corresponding to “now”.
§Examples
1.8.0 ·Sourcepub fnduration_since( &self, earlier:SystemTime,) ->Result<Duration,SystemTimeError>
pub fnduration_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 notguaranteed to always be before later measurements (due to anomalies suchas 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 representsthe amount of time elapsed from the specified measurement to this one.
Returns anErr
ifearlier
is later thanself
, and the errorcontains how far fromself
the time is.
§Examples
1.8.0 ·Sourcepub fnelapsed(&self) ->Result<Duration,SystemTimeError>
pub fnelapsed(&self) ->Result<Duration,SystemTimeError>
Returns the difference from this system time to thecurrent clock time.
This function may fail as the underlying system clock is susceptible todrift and updates (e.g., the system clock could go backwards), so thisfunction might not always succeed. If successful,Ok(Duration)
isreturned where the duration represents the amount of time elapsed fromthis time measurement to the current time.
To measure elapsed time reliably, useInstant
instead.
Returns anErr
ifself
is later than the current system time, andthe error contains how far from the current system timeself
is.
§Examples
1.34.0 ·Sourcepub fnchecked_add(&self, duration:Duration) ->Option<SystemTime>
pub fnchecked_add(&self, duration:Duration) ->Option<SystemTime>
ReturnsSome(t)
wheret
is the timeself + duration
ift
can be represented asSystemTime
(which means it’s inside the bounds of the underlying data structure),None
otherwise.
1.34.0 ·Sourcepub fnchecked_sub(&self, duration:Duration) ->Option<SystemTime>
pub fnchecked_sub(&self, duration:Duration) ->Option<SystemTime>
ReturnsSome(t)
wheret
is the timeself - duration
ift
can be represented asSystemTime
(which means it’s inside the bounds of the underlying data structure),None
otherwise.
Trait Implementations§
1.8.0 ·Source§implAdd<Duration> forSystemTime
implAdd<Duration> forSystemTime
Source§fnadd(self, dur:Duration) ->SystemTime
fnadd(self, dur:Duration) ->SystemTime
§Panics
This function may panic if the resulting point in time cannot be represented by theunderlying data structure. SeeSystemTime::checked_add
for a version without panic.
Source§typeOutput =SystemTime
typeOutput =SystemTime
+
operator.1.9.0 ·Source§implAddAssign<Duration> forSystemTime
implAddAssign<Duration> forSystemTime
Source§fnadd_assign(&mut self, other:Duration)
fnadd_assign(&mut self, other:Duration)
+=
operation.Read more1.8.0 ·Source§implClone forSystemTime
implClone forSystemTime
Source§fnclone(&self) ->SystemTime
fnclone(&self) ->SystemTime
1.0.0 ·Source§fnclone_from(&mut self, source: &Self)
fnclone_from(&mut self, source: &Self)
source
.Read more1.8.0 ·Source§implDebug forSystemTime
implDebug forSystemTime
1.8.0 ·Source§implHash forSystemTime
implHash forSystemTime
1.8.0 ·Source§implOrd forSystemTime
implOrd forSystemTime
1.8.0 ·Source§implPartialEq forSystemTime
implPartialEq forSystemTime
1.8.0 ·Source§implPartialOrd forSystemTime
implPartialOrd forSystemTime
1.8.0 ·Source§implSub<Duration> forSystemTime
implSub<Duration> forSystemTime
Source§typeOutput =SystemTime
typeOutput =SystemTime
-
operator.1.9.0 ·Source§implSubAssign<Duration> forSystemTime
implSubAssign<Duration> forSystemTime
Source§fnsub_assign(&mut self, other:Duration)
fnsub_assign(&mut self, other:Duration)
-=
operation.Read more