# 1.8.0[−][src]Struct std::time::SystemTime

    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.

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);
           }
       }
    }Run

## Methods

### `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!"),
    }Run

#### `pub fn now() -> SystemTime`[src]

Returns the system time corresponding to "now".

# Examples

    use std::time::SystemTime;

    let sys_time = SystemTime::now();Run

#### `pub fn duration_since(
&self,
earlier: SystemTime
) -> Result<Duration, SystemTimeError>`[src]

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.

# Examples

    use std::time::SystemTime;

    let sys_time = SystemTime::now();
    let difference = sys_time.duration_since(sys_time)
                             .expect("SystemTime::duration_since failed");
    println!("{:?}", difference);Run

#### `pub fn elapsed(&self) -> Result<Duration, SystemTimeError>`[src]

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.

# 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);Run

#### `pub fn checked_add(&self, duration: Duration) -> Option<SystemTime>`[src]

🔬 This is a nightly-only experimental API. (`time_checked_add` #55940)

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>`[src]

🔬 This is a nightly-only experimental API. (`time_checked_add` #55940)

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 Copy for SystemTime`[src]

### `impl PartialEq<SystemTime> for SystemTime`[src]

#### `fn eq(&self, other: &SystemTime) -> bool`[src]

This method tests for `self` and `other` values to be equal, and is used by `==`. Read more

#### `fn ne(&self, other: &SystemTime) -> bool`[src]

This method tests for `!=`.

### `impl Eq for SystemTime`[src]

### `impl Ord for SystemTime`[src]

#### `fn cmp(&self, other: &SystemTime) -> Ordering`[src]

This method returns an `Ordering` between `self` and `other`. Read more

#### `fn max(self, other: Self) -> Self`
1.21.0
[src]

Compares and returns the maximum of two values. Read more

#### `fn min(self, other: Self) -> Self`
1.21.0
[src]

Compares and returns the minimum of two values. Read more

### `impl PartialOrd<SystemTime> for SystemTime`[src]

#### `fn partial_cmp(&self, other: &SystemTime) -> Option<Ordering>`[src]

This method returns an ordering between `self` and `other` values if one exists. Read more

#### `fn lt(&self, other: &SystemTime) -> bool`[src]

This method tests less than (for `self` and `other`) and is used by the `<` operator. Read more

#### `fn le(&self, other: &SystemTime) -> bool`[src]

This method tests less than or equal to (for `self` and `other`) and is used by the `<=` operator. Read more

#### `fn gt(&self, other: &SystemTime) -> bool`[src]

This method tests greater than (for `self` and `other`) and is used by the `>` operator. Read more

#### `fn ge(&self, other: &SystemTime) -> bool`[src]

This method tests greater than or equal to (for `self` and `other`) and is used by the `>=` operator. Read more

### `impl Debug for SystemTime`[src]

#### `fn fmt(&self, f: &mut Formatter) -> Result`[src]

Formats the value using the given formatter. Read more

### `impl Add<Duration> for SystemTime`[src]

#### `type Output = SystemTime`

The resulting type after applying the `+` operator.

#### `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 `checked_add` for a version without panic.

### `impl Sub<Duration> for SystemTime`[src]

#### `type Output = SystemTime`

The resulting type after applying the `-` operator.

#### `fn sub(self, dur: Duration) -> SystemTime`[src]

Performs the `-` operation.

### `impl AddAssign<Duration> for SystemTime`
1.9.0
[src]

#### `fn add_assign(&mut self, other: Duration)`[src]

Performs the `+=` operation.

### `impl SubAssign<Duration> for SystemTime`
1.9.0
[src]

#### `fn sub_assign(&mut self, other: Duration)`[src]

Performs the `-=` operation.

### `impl Hash for SystemTime`[src]

#### `fn hash<__H: Hasher>(&self, state: &mut __H)`[src]

Feeds this value into the given [`Hasher`]. Read more

#### `fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,`
1.3.0
[src]

Feeds a slice of this type into the given [`Hasher`]. Read more

### `impl Clone for SystemTime`[src]

#### `fn clone(&self) -> SystemTime`[src]

Returns a copy of the value. Read more

#### `fn clone_from(&mut self, source: &Self)`
1.0.0
[src]

Performs copy-assignment from `source`. Read more

## Auto Trait Implementations

### `impl Send for SystemTime`

### `impl Sync for SystemTime`

## Blanket Implementations

### `impl<T> From for T`[src]

#### `fn from(t: T) -> T`[src]

Performs the conversion.

### `impl<T, U> TryFrom for T where
T: From<U>,` [src]

#### `type Error = !`

🔬 This is a nightly-only experimental API. (`try_from` #33417)

The type returned in the event of a conversion error.

#### `fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>`[src]

🔬 This is a nightly-only experimental API. (`try_from` #33417)

Performs the conversion.

### `impl<T, U> TryInto for T where
U: TryFrom<T>,` [src]

#### `type Error = <U as TryFrom<T>>::Error`

🔬 This is a nightly-only experimental API. (`try_from` #33417)

The type returned in the event of a conversion error.

#### `fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>`[src]

🔬 This is a nightly-only experimental API. (`try_from` #33417)

Performs the conversion.

### `impl<T, U> Into for T where
U: From<T>,` [src]

#### `fn into(self) -> U`[src]

Performs the conversion.

### `impl<T> Borrow for T where
T: ?Sized,` [src]

####
ⓘImportant traits for &'_ mut I

### Important traits for &'_ mut I

`impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<'a, R: Read + ?Sized> Read for &'a mut Rimpl<'a, W: Write + ?Sized> Write for &'a mut W`
`fn borrow(&self) -> &T`[src]

Immutably borrows from an owned value. Read more

### `impl<T> BorrowMut for T where
T: ?Sized,` [src]

####
ⓘImportant traits for &'_ mut I

### Important traits for &'_ mut I

`impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<'a, R: Read + ?Sized> Read for &'a mut Rimpl<'a, W: Write + ?Sized> Write for &'a mut W`
`fn borrow_mut(&mut self) -> &mut T`[src]

Mutably borrows from an owned value. Read more

### `impl<T> Any for T where
T: 'static + ?Sized,` [src]

#### `fn get_type_id(&self) -> TypeId`[src]

🔬 This is a nightly-only experimental API. (`get_type_id` #27745)

this method will likely be replaced by an associated static

Gets the `TypeId` of `self`. Read more

### `impl<T> ToOwned for T where
T: Clone,` [src]

#### `type Owned = T`

#### `fn to_owned(&self) -> T`[src]

Creates owned data from borrowed data, usually by cloning. Read more

#### `fn clone_into(&self, target: &mut T)`[src]

🔬 This is a nightly-only experimental API. (`toowned_clone_into` #41263)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)