2014-08-01 00:24:54 +00:00
|
|
|
//! Temporal quantification.
|
2016-03-23 13:17:19 +00:00
|
|
|
//!
|
|
|
|
//! Example:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! use std::time::Duration;
|
|
|
|
//!
|
|
|
|
//! let five_seconds = Duration::new(5, 0);
|
|
|
|
//! // both declarations are equivalent
|
|
|
|
//! assert_eq!(Duration::new(5, 0), Duration::from_secs(5));
|
|
|
|
//! ```
|
2014-08-01 00:24:54 +00:00
|
|
|
|
2015-07-06 06:20:00 +00:00
|
|
|
#![stable(feature = "time", since = "1.3.0")]
|
2014-11-10 20:27:56 +00:00
|
|
|
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::cmp;
|
|
|
|
use crate::error::Error;
|
|
|
|
use crate::fmt;
|
2019-10-05 10:23:10 +00:00
|
|
|
use crate::ops::{Add, AddAssign, Sub, SubAssign};
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::sys::time;
|
|
|
|
use crate::sys_common::mutex::Mutex;
|
2019-10-05 10:23:10 +00:00
|
|
|
use crate::sys_common::FromInner;
|
2015-11-17 01:36:14 +00:00
|
|
|
|
2015-11-16 16:54:28 +00:00
|
|
|
#[stable(feature = "time", since = "1.3.0")]
|
2017-12-11 18:42:01 +00:00
|
|
|
pub use core::time::Duration;
|
2015-11-17 01:36:14 +00:00
|
|
|
|
2017-08-09 04:37:37 +00:00
|
|
|
/// A measurement of a monotonically nondecreasing clock.
|
2016-12-14 15:14:19 +00:00
|
|
|
/// Opaque and useful only with `Duration`.
|
2015-11-17 01:36:14 +00:00
|
|
|
///
|
2017-08-09 16:28:18 +00:00
|
|
|
/// Instants are always guaranteed to be no less than any previously measured
|
2015-11-17 01:36:14 +00:00
|
|
|
/// instant when created, and are often useful for tasks such as measuring
|
|
|
|
/// benchmarks or timing how long an operation takes.
|
|
|
|
///
|
2019-02-09 21:23:30 +00:00
|
|
|
/// Note, however, that instants are not guaranteed to be **steady**. In other
|
2015-11-25 20:46:39 +00:00
|
|
|
/// words, each tick of the underlying clock may not be the same length (e.g.
|
2015-11-17 01:36:14 +00:00
|
|
|
/// some seconds may be longer than others). An instant may jump forwards or
|
|
|
|
/// experience time dilation (slow down or speed up), but it will never go
|
|
|
|
/// backwards.
|
|
|
|
///
|
|
|
|
/// Instants are opaque types that can only be compared to one another. There is
|
2015-11-25 20:47:17 +00:00
|
|
|
/// no method to get "the number of seconds" from an instant. Instead, it only
|
|
|
|
/// allows measuring the duration between two instants (or comparing two
|
2015-11-17 01:36:14 +00:00
|
|
|
/// instants).
|
2016-03-23 13:17:19 +00:00
|
|
|
///
|
2018-03-25 03:41:34 +00:00
|
|
|
/// The size of an `Instant` struct may vary depending on the target operating
|
2018-03-11 21:17:18 +00:00
|
|
|
/// system.
|
2018-03-11 11:41:13 +00:00
|
|
|
///
|
2016-03-23 13:17:19 +00:00
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::time::{Duration, Instant};
|
|
|
|
/// use std::thread::sleep;
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let now = Instant::now();
|
|
|
|
///
|
|
|
|
/// // we sleep for 2 seconds
|
|
|
|
/// sleep(Duration::new(2, 0));
|
|
|
|
/// // it prints '2'
|
|
|
|
/// println!("{}", now.elapsed().as_secs());
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-08-24 12:49:17 +00:00
|
|
|
///
|
|
|
|
/// # Underlying System calls
|
|
|
|
/// Currently, the following system calls are being used to get the current time using `now()`:
|
|
|
|
///
|
2019-09-09 20:10:53 +00:00
|
|
|
/// | Platform | System call |
|
|
|
|
/// |:---------:|:--------------------------------------------------------------------:|
|
2020-01-18 12:33:17 +00:00
|
|
|
/// | CloudABI | [clock_time_get (Monotonic Clock)] |
|
2019-09-09 20:10:53 +00:00
|
|
|
/// | SGX | [`insecure_time` usercall]. More information on [timekeeping in SGX] |
|
2020-01-12 20:24:31 +00:00
|
|
|
/// | UNIX | [clock_gettime (Monotonic Clock)] |
|
2019-09-09 20:10:53 +00:00
|
|
|
/// | Darwin | [mach_absolute_time] |
|
|
|
|
/// | VXWorks | [clock_gettime (Monotonic Clock)] |
|
|
|
|
/// | WASI | [__wasi_clock_time_get (Monotonic Clock)] |
|
|
|
|
/// | Windows | [QueryPerformanceCounter] |
|
2019-09-13 14:29:06 +00:00
|
|
|
///
|
2019-09-09 20:10:53 +00:00
|
|
|
/// [QueryPerformanceCounter]: https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancecounter
|
|
|
|
/// [`insecure_time` usercall]: https://edp.fortanix.com/docs/api/fortanix_sgx_abi/struct.Usercalls.html#method.insecure_time
|
|
|
|
/// [timekeeping in SGX]: https://edp.fortanix.com/docs/concepts/rust-std/#codestdtimecode
|
2020-01-28 22:34:00 +00:00
|
|
|
/// [__wasi_clock_time_get (Monotonic Clock)]: https://github.com/WebAssembly/WASI/blob/master/phases/snapshot/docs.md#clock_time_get
|
2019-09-09 20:10:53 +00:00
|
|
|
/// [clock_gettime (Monotonic Clock)]: https://linux.die.net/man/3/clock_gettime
|
|
|
|
/// [mach_absolute_time]: https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/services/services.html
|
2020-01-18 12:33:17 +00:00
|
|
|
/// [clock_time_get (Monotonic Clock)]: https://nuxi.nl/cloudabi/#clock_time_get
|
2019-08-24 12:49:17 +00:00
|
|
|
///
|
|
|
|
/// **Disclaimer:** These system calls might change over time.
|
|
|
|
///
|
2017-12-18 21:35:43 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
2015-11-17 01:36:14 +00:00
|
|
|
pub struct Instant(time::Instant);
|
|
|
|
|
2016-01-12 16:52:29 +00:00
|
|
|
/// A measurement of the system clock, useful for talking to
|
|
|
|
/// external entities like the file system or other processes.
|
2015-11-17 01:36:14 +00:00
|
|
|
///
|
2016-12-14 15:14:19 +00:00
|
|
|
/// Distinct from the [`Instant`] type, this time measurement **is not
|
2015-11-17 01:36:14 +00:00
|
|
|
/// 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
|
2016-05-12 15:38:20 +00:00
|
|
|
/// `SystemTime` measurement earlier than the first**. In other words, an
|
2015-11-17 01:36:14 +00:00
|
|
|
/// operation that happens after another operation in real time may have an
|
|
|
|
/// earlier `SystemTime`!
|
|
|
|
///
|
|
|
|
/// Consequently, comparing two `SystemTime` instances to learn about the
|
2016-12-14 15:14:19 +00:00
|
|
|
/// duration between them returns a [`Result`] instead of an infallible [`Duration`]
|
2015-11-17 01:36:14 +00:00
|
|
|
/// to indicate that this sort of time drift may happen and needs to be handled.
|
|
|
|
///
|
2016-12-14 15:14:19 +00:00
|
|
|
/// Although a `SystemTime` cannot be directly inspected, the [`UNIX_EPOCH`]
|
2015-11-17 01:36:14 +00:00
|
|
|
/// constant is provided in this module as an anchor in time to learn
|
|
|
|
/// information about a `SystemTime`. By calculating the duration from this
|
2015-11-25 20:46:39 +00:00
|
|
|
/// fixed point in time, a `SystemTime` can be converted to a human-readable time,
|
2015-11-17 01:36:14 +00:00
|
|
|
/// or perhaps some other string representation.
|
2016-03-23 13:17:19 +00:00
|
|
|
///
|
2018-03-25 03:41:34 +00:00
|
|
|
/// The size of a `SystemTime` struct may vary depending on the target operating
|
2018-03-11 21:17:18 +00:00
|
|
|
/// system.
|
2018-03-11 11:41:13 +00:00
|
|
|
///
|
2016-12-14 15:14:19 +00:00
|
|
|
/// [`Instant`]: ../../std/time/struct.Instant.html
|
|
|
|
/// [`Result`]: ../../std/result/enum.Result.html
|
|
|
|
/// [`Duration`]: ../../std/time/struct.Duration.html
|
|
|
|
/// [`UNIX_EPOCH`]: ../../std/time/constant.UNIX_EPOCH.html
|
|
|
|
///
|
2016-03-23 13:17:19 +00:00
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// 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) => {
|
2017-08-15 19:45:21 +00:00
|
|
|
/// // an error occurred!
|
2016-03-23 13:17:19 +00:00
|
|
|
/// println!("Error: {:?}", e);
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-08-24 12:49:17 +00:00
|
|
|
///
|
|
|
|
/// # Underlying System calls
|
|
|
|
/// Currently, the following system calls are being used to get the current time using `now()`:
|
|
|
|
///
|
2019-09-09 20:10:53 +00:00
|
|
|
/// | Platform | System call |
|
|
|
|
/// |:---------:|:--------------------------------------------------------------------:|
|
2020-01-18 12:33:17 +00:00
|
|
|
/// | CloudABI | [clock_time_get (Realtime Clock)] |
|
2019-09-09 20:10:53 +00:00
|
|
|
/// | 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 | [GetSystemTimeAsFileTime] |
|
|
|
|
///
|
2020-01-18 12:33:17 +00:00
|
|
|
/// [clock_time_get (Realtime Clock)]: https://nuxi.nl/cloudabi/#clock_time_get
|
2019-12-31 17:14:49 +00:00
|
|
|
/// [`insecure_time` usercall]: https://edp.fortanix.com/docs/api/fortanix_sgx_abi/struct.Usercalls.html#method.insecure_time
|
|
|
|
/// [timekeeping in SGX]: https://edp.fortanix.com/docs/concepts/rust-std/#codestdtimecode
|
2019-09-09 20:10:53 +00:00
|
|
|
/// [gettimeofday]: http://man7.org/linux/man-pages/man2/gettimeofday.2.html
|
|
|
|
/// [clock_gettime (Realtime Clock)]: https://linux.die.net/man/3/clock_gettime
|
2020-01-28 22:34:00 +00:00
|
|
|
/// [__wasi_clock_time_get (Realtime Clock)]: https://github.com/WebAssembly/WASI/blob/master/phases/snapshot/docs.md#clock_time_get
|
2019-09-09 20:10:53 +00:00
|
|
|
/// [GetSystemTimeAsFileTime]: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimeasfiletime
|
2019-08-24 12:49:17 +00:00
|
|
|
///
|
|
|
|
/// **Disclaimer:** These system calls might change over time.
|
|
|
|
///
|
2017-12-18 21:35:43 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
2015-11-17 01:36:14 +00:00
|
|
|
pub struct SystemTime(time::SystemTime);
|
|
|
|
|
2016-12-14 15:14:19 +00:00
|
|
|
/// An error returned from the `duration_since` and `elapsed` methods on
|
|
|
|
/// `SystemTime`, used to learn how far in the opposite direction a system time
|
|
|
|
/// lies.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::thread::sleep;
|
|
|
|
/// use std::time::{Duration, SystemTime};
|
|
|
|
///
|
|
|
|
/// let sys_time = SystemTime::now();
|
|
|
|
/// sleep(Duration::from_secs(1));
|
|
|
|
/// let new_sys_time = SystemTime::now();
|
|
|
|
/// match sys_time.duration_since(new_sys_time) {
|
|
|
|
/// Ok(_) => {}
|
|
|
|
/// Err(e) => println!("SystemTimeError difference: {:?}", e.duration()),
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-11-17 01:36:14 +00:00
|
|
|
#[derive(Clone, Debug)]
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
2015-11-17 01:36:14 +00:00
|
|
|
pub struct SystemTimeError(Duration);
|
|
|
|
|
|
|
|
impl Instant {
|
|
|
|
/// Returns an instant corresponding to "now".
|
2016-12-14 15:14:19 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Instant;
|
|
|
|
///
|
|
|
|
/// let now = Instant::now();
|
|
|
|
/// ```
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
2015-11-17 01:36:14 +00:00
|
|
|
pub fn now() -> Instant {
|
2018-12-19 18:29:23 +00:00
|
|
|
let os_now = time::Instant::now();
|
|
|
|
|
|
|
|
// And here we come upon a sad state of affairs. The whole point of
|
|
|
|
// `Instant` is that it's monotonically increasing. We've found in the
|
|
|
|
// wild, however, that it's not actually monotonically increasing for
|
|
|
|
// one reason or another. These appear to be OS and hardware level bugs,
|
|
|
|
// and there's not really a whole lot we can do about them. Here's a
|
|
|
|
// taste of what we've found:
|
|
|
|
//
|
|
|
|
// * #48514 - OpenBSD, x86_64
|
|
|
|
// * #49281 - linux arm64 and s390x
|
|
|
|
// * #51648 - windows, x86
|
|
|
|
// * #56560 - windows, x86_64, AWS
|
|
|
|
// * #56612 - windows, x86, vm (?)
|
|
|
|
// * #56940 - linux, arm64
|
|
|
|
// * https://bugzilla.mozilla.org/show_bug.cgi?id=1487778 - a similar
|
|
|
|
// Firefox bug
|
|
|
|
//
|
2019-10-05 10:23:10 +00:00
|
|
|
// It seems that this just happens a lot in the wild.
|
|
|
|
// We're seeing panics across various platforms where consecutive calls
|
2018-12-19 18:29:23 +00:00
|
|
|
// to `Instant::now`, such as via the `elapsed` function, are panicking
|
|
|
|
// as they're going backwards. Placed here is a last-ditch effort to try
|
|
|
|
// to fix things up. We keep a global "latest now" instance which is
|
|
|
|
// returned instead of what the OS says if the OS goes backwards.
|
|
|
|
//
|
2019-10-05 10:23:10 +00:00
|
|
|
// To hopefully mitigate the impact of this, a few platforms are
|
2018-12-19 18:29:23 +00:00
|
|
|
// whitelisted as "these at least haven't gone backwards yet".
|
|
|
|
if time::Instant::actually_monotonic() {
|
2019-10-05 10:23:10 +00:00
|
|
|
return Instant(os_now);
|
2018-12-19 18:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static LOCK: Mutex = Mutex::new();
|
|
|
|
static mut LAST_NOW: time::Instant = time::Instant::zero();
|
|
|
|
unsafe {
|
|
|
|
let _lock = LOCK.lock();
|
|
|
|
let now = cmp::max(LAST_NOW, os_now);
|
|
|
|
LAST_NOW = now;
|
|
|
|
Instant(now)
|
|
|
|
}
|
2015-11-17 01:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the amount of time elapsed from another instant to this one.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
2017-07-15 20:23:11 +00:00
|
|
|
/// This function will panic if `earlier` is later than `self`.
|
2016-12-14 15:14:19 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::time::{Duration, Instant};
|
|
|
|
/// use std::thread::sleep;
|
|
|
|
///
|
|
|
|
/// let now = Instant::now();
|
|
|
|
/// sleep(Duration::new(1, 0));
|
|
|
|
/// let new_now = Instant::now();
|
|
|
|
/// println!("{:?}", new_now.duration_since(now));
|
|
|
|
/// ```
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
|
|
|
pub fn duration_since(&self, earlier: Instant) -> Duration {
|
2019-03-22 21:12:32 +00:00
|
|
|
self.0.checked_sub_instant(&earlier.0).expect("supplied instant is later than self")
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 07:56:26 +00:00
|
|
|
/// Returns the amount of time elapsed from another instant to this one,
|
2019-09-10 02:19:01 +00:00
|
|
|
/// or None if that instant is later than this one.
|
2019-02-12 07:56:26 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::time::{Duration, Instant};
|
|
|
|
/// use std::thread::sleep;
|
|
|
|
///
|
|
|
|
/// let now = Instant::now();
|
|
|
|
/// sleep(Duration::new(1, 0));
|
|
|
|
/// let new_now = Instant::now();
|
|
|
|
/// println!("{:?}", new_now.checked_duration_since(now));
|
|
|
|
/// println!("{:?}", now.checked_duration_since(new_now)); // None
|
|
|
|
/// ```
|
2019-07-21 21:54:42 +00:00
|
|
|
#[stable(feature = "checked_duration_since", since = "1.39.0")]
|
2019-02-12 07:56:26 +00:00
|
|
|
pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration> {
|
2019-03-22 21:12:32 +00:00
|
|
|
self.0.checked_sub_instant(&earlier.0)
|
2019-02-12 07:56:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 12:08:44 +00:00
|
|
|
/// Returns the amount of time elapsed from another instant to this one,
|
2020-02-29 13:40:09 +00:00
|
|
|
/// or zero duration if that instant is later than this one.
|
2019-02-13 12:08:44 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::time::{Duration, Instant};
|
|
|
|
/// use std::thread::sleep;
|
|
|
|
///
|
|
|
|
/// let now = Instant::now();
|
|
|
|
/// sleep(Duration::new(1, 0));
|
|
|
|
/// let new_now = Instant::now();
|
|
|
|
/// println!("{:?}", new_now.saturating_duration_since(now));
|
|
|
|
/// println!("{:?}", now.saturating_duration_since(new_now)); // 0ns
|
|
|
|
/// ```
|
2019-07-21 21:54:42 +00:00
|
|
|
#[stable(feature = "checked_duration_since", since = "1.39.0")]
|
2019-02-13 12:08:44 +00:00
|
|
|
pub fn saturating_duration_since(&self, earlier: Instant) -> Duration {
|
|
|
|
self.checked_duration_since(earlier).unwrap_or(Duration::new(0, 0))
|
|
|
|
}
|
|
|
|
|
2015-11-17 01:36:14 +00:00
|
|
|
/// Returns the amount of time elapsed since this instant was created.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
2015-11-25 20:50:47 +00:00
|
|
|
/// This function may panic if the current time is earlier than this
|
|
|
|
/// instant, which is something that can happen if an `Instant` is
|
|
|
|
/// produced synthetically.
|
2016-09-07 01:10:15 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::thread::sleep;
|
|
|
|
/// use std::time::{Duration, Instant};
|
|
|
|
///
|
|
|
|
/// let instant = Instant::now();
|
|
|
|
/// let three_secs = Duration::from_secs(3);
|
|
|
|
/// sleep(three_secs);
|
|
|
|
/// assert!(instant.elapsed() >= three_secs);
|
|
|
|
/// ```
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
2015-11-17 01:36:14 +00:00
|
|
|
pub fn elapsed(&self) -> Duration {
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
Instant::now() - *self
|
2015-11-17 01:36:14 +00:00
|
|
|
}
|
2018-12-04 01:29:57 +00:00
|
|
|
|
|
|
|
/// Returns `Some(t)` where `t` is the time `self + duration` if `t` can be represented as
|
|
|
|
/// `Instant` (which means it's inside the bounds of the underlying data structure), `None`
|
|
|
|
/// otherwise.
|
2019-01-31 19:46:11 +00:00
|
|
|
#[stable(feature = "time_checked_add", since = "1.34.0")]
|
2018-12-04 01:29:57 +00:00
|
|
|
pub fn checked_add(&self, duration: Duration) -> Option<Instant> {
|
2019-01-31 22:18:07 +00:00
|
|
|
self.0.checked_add_duration(&duration).map(Instant)
|
2018-12-04 01:29:57 +00:00
|
|
|
}
|
2018-12-10 22:55:53 +00:00
|
|
|
|
|
|
|
/// Returns `Some(t)` where `t` is the time `self - duration` if `t` can be represented as
|
|
|
|
/// `Instant` (which means it's inside the bounds of the underlying data structure), `None`
|
|
|
|
/// otherwise.
|
2019-01-31 19:46:11 +00:00
|
|
|
#[stable(feature = "time_checked_add", since = "1.34.0")]
|
2018-12-10 22:55:53 +00:00
|
|
|
pub fn checked_sub(&self, duration: Duration) -> Option<Instant> {
|
2019-01-31 22:18:07 +00:00
|
|
|
self.0.checked_sub_duration(&duration).map(Instant)
|
2018-12-10 22:55:53 +00:00
|
|
|
}
|
2015-11-17 01:36:14 +00:00
|
|
|
}
|
|
|
|
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
2015-11-17 01:36:14 +00:00
|
|
|
impl Add<Duration> for Instant {
|
|
|
|
type Output = Instant;
|
|
|
|
|
2018-12-14 19:07:04 +00:00
|
|
|
/// # 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.
|
|
|
|
///
|
|
|
|
/// [`checked_add`]: ../../std/time/struct.Instant.html#method.checked_add
|
2015-11-17 01:36:14 +00:00
|
|
|
fn add(self, other: Duration) -> Instant {
|
2019-10-05 10:23:10 +00:00
|
|
|
self.checked_add(other).expect("overflow when adding duration to instant")
|
2015-11-17 01:36:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 06:28:22 +00:00
|
|
|
#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
|
|
|
|
impl AddAssign<Duration> for Instant {
|
|
|
|
fn add_assign(&mut self, other: Duration) {
|
|
|
|
*self = *self + other;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
2015-11-17 01:36:14 +00:00
|
|
|
impl Sub<Duration> for Instant {
|
|
|
|
type Output = Instant;
|
|
|
|
|
|
|
|
fn sub(self, other: Duration) -> Instant {
|
2019-10-05 10:23:10 +00:00
|
|
|
self.checked_sub(other).expect("overflow when subtracting duration from instant")
|
2015-11-17 01:36:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 06:28:22 +00:00
|
|
|
#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
|
|
|
|
impl SubAssign<Duration> for Instant {
|
|
|
|
fn sub_assign(&mut self, other: Duration) {
|
|
|
|
*self = *self - other;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
|
|
|
impl Sub<Instant> for Instant {
|
|
|
|
type Output = Duration;
|
|
|
|
|
|
|
|
fn sub(self, other: Instant) -> Duration {
|
|
|
|
self.duration_since(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
2015-11-17 01:36:14 +00:00
|
|
|
impl fmt::Debug for Instant {
|
2019-03-01 08:34:11 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-11-17 01:36:14 +00:00
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SystemTime {
|
2018-03-30 10:01:36 +00:00
|
|
|
/// 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
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// 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!"),
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-05-29 00:24:01 +00:00
|
|
|
#[stable(feature = "assoc_unix_epoch", since = "1.28.0")]
|
2018-03-30 10:01:36 +00:00
|
|
|
pub const UNIX_EPOCH: SystemTime = UNIX_EPOCH;
|
|
|
|
|
2015-11-17 01:36:14 +00:00
|
|
|
/// Returns the system time corresponding to "now".
|
2016-12-14 15:14:19 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::SystemTime;
|
|
|
|
///
|
|
|
|
/// let sys_time = SystemTime::now();
|
|
|
|
/// ```
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
2015-11-17 01:36:14 +00:00
|
|
|
pub fn now() -> SystemTime {
|
|
|
|
SystemTime(time::SystemTime::now())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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).
|
2019-07-27 20:46:18 +00:00
|
|
|
/// [`Instant`] can be used to measure elapsed time without this risk of failure.
|
2015-11-17 01:36:14 +00:00
|
|
|
///
|
2016-12-14 15:14:19 +00:00
|
|
|
/// If successful, [`Ok`]`(`[`Duration`]`)` is returned where the duration represents
|
2015-11-17 01:36:14 +00:00
|
|
|
/// the amount of time elapsed from the specified measurement to this one.
|
|
|
|
///
|
2016-12-14 15:14:19 +00:00
|
|
|
/// Returns an [`Err`] if `earlier` is later than `self`, and the error
|
2015-11-17 01:36:14 +00:00
|
|
|
/// contains how far from `self` the time is.
|
2016-12-14 15:14:19 +00:00
|
|
|
///
|
|
|
|
/// [`Ok`]: ../../std/result/enum.Result.html#variant.Ok
|
|
|
|
/// [`Duration`]: ../../std/time/struct.Duration.html
|
|
|
|
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
|
2019-07-27 20:46:18 +00:00
|
|
|
/// [`Instant`]: ../../std/time/struct.Instant.html
|
2016-12-14 15:14:19 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::SystemTime;
|
|
|
|
///
|
|
|
|
/// let sys_time = SystemTime::now();
|
|
|
|
/// let difference = sys_time.duration_since(sys_time)
|
2019-07-27 20:46:18 +00:00
|
|
|
/// .expect("Clock may have gone backwards");
|
2016-12-14 15:14:19 +00:00
|
|
|
/// println!("{:?}", difference);
|
|
|
|
/// ```
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
2019-10-05 10:23:10 +00:00
|
|
|
pub fn duration_since(&self, earlier: SystemTime) -> Result<Duration, SystemTimeError> {
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
self.0.sub_time(&earlier.0).map_err(SystemTimeError)
|
|
|
|
}
|
|
|
|
|
2019-07-27 20:46:18 +00:00
|
|
|
/// Returns the difference between the clock time when this
|
|
|
|
/// system time was created, and the current clock time.
|
2015-11-17 01:36:14 +00:00
|
|
|
///
|
|
|
|
/// This function may fail as the underlying system clock is susceptible to
|
2018-11-27 02:59:49 +00:00
|
|
|
/// drift and updates (e.g., the system clock could go backwards), so this
|
2016-12-14 15:14:19 +00:00
|
|
|
/// function may not always succeed. If successful, [`Ok`]`(`[`Duration`]`)` is
|
2015-11-17 01:36:14 +00:00
|
|
|
/// returned where the duration represents the amount of time elapsed from
|
|
|
|
/// this time measurement to the current time.
|
|
|
|
///
|
2019-07-27 20:46:18 +00:00
|
|
|
/// To measure elapsed time reliably, use [`Instant`] instead.
|
|
|
|
///
|
2016-12-14 15:14:19 +00:00
|
|
|
/// Returns an [`Err`] if `self` is later than the current system time, and
|
2015-11-17 01:36:14 +00:00
|
|
|
/// the error contains how far from the current system time `self` is.
|
2016-12-14 15:14:19 +00:00
|
|
|
///
|
|
|
|
/// [`Ok`]: ../../std/result/enum.Result.html#variant.Ok
|
|
|
|
/// [`Duration`]: ../../std/time/struct.Duration.html
|
|
|
|
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
|
2019-07-27 20:46:18 +00:00
|
|
|
/// [`Instant`]: ../../std/time/struct.Instant.html
|
2016-12-14 15:14:19 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// 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);
|
|
|
|
/// ```
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
2015-11-17 01:36:14 +00:00
|
|
|
pub fn elapsed(&self) -> Result<Duration, SystemTimeError> {
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
SystemTime::now().duration_since(*self)
|
2015-11-17 01:36:14 +00:00
|
|
|
}
|
2018-10-31 05:24:33 +00:00
|
|
|
|
|
|
|
/// 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.
|
2019-01-31 19:46:11 +00:00
|
|
|
#[stable(feature = "time_checked_add", since = "1.34.0")]
|
2018-11-05 05:23:20 +00:00
|
|
|
pub fn checked_add(&self, duration: Duration) -> Option<SystemTime> {
|
2019-01-31 22:18:07 +00:00
|
|
|
self.0.checked_add_duration(&duration).map(SystemTime)
|
2018-10-31 05:24:33 +00:00
|
|
|
}
|
2018-12-10 22:55:53 +00:00
|
|
|
|
|
|
|
/// 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.
|
2019-01-31 19:46:11 +00:00
|
|
|
#[stable(feature = "time_checked_add", since = "1.34.0")]
|
2018-12-10 22:55:53 +00:00
|
|
|
pub fn checked_sub(&self, duration: Duration) -> Option<SystemTime> {
|
2019-01-31 22:18:07 +00:00
|
|
|
self.0.checked_sub_duration(&duration).map(SystemTime)
|
2018-12-10 22:55:53 +00:00
|
|
|
}
|
2015-11-17 01:36:14 +00:00
|
|
|
}
|
|
|
|
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
2015-11-17 01:36:14 +00:00
|
|
|
impl Add<Duration> for SystemTime {
|
|
|
|
type Output = SystemTime;
|
|
|
|
|
2018-12-14 19:07:04 +00:00
|
|
|
/// # 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.
|
|
|
|
///
|
|
|
|
/// [`checked_add`]: ../../std/time/struct.SystemTime.html#method.checked_add
|
2015-11-17 01:36:14 +00:00
|
|
|
fn add(self, dur: Duration) -> SystemTime {
|
2019-10-05 10:23:10 +00:00
|
|
|
self.checked_add(dur).expect("overflow when adding duration to instant")
|
2015-11-17 01:36:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 06:28:22 +00:00
|
|
|
#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
|
|
|
|
impl AddAssign<Duration> for SystemTime {
|
|
|
|
fn add_assign(&mut self, other: Duration) {
|
|
|
|
*self = *self + other;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
2015-11-17 01:36:14 +00:00
|
|
|
impl Sub<Duration> for SystemTime {
|
|
|
|
type Output = SystemTime;
|
|
|
|
|
|
|
|
fn sub(self, dur: Duration) -> SystemTime {
|
2019-10-05 10:23:10 +00:00
|
|
|
self.checked_sub(dur).expect("overflow when subtracting duration from instant")
|
2015-11-17 01:36:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 06:28:22 +00:00
|
|
|
#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
|
|
|
|
impl SubAssign<Duration> for SystemTime {
|
|
|
|
fn sub_assign(&mut self, other: Duration) {
|
|
|
|
*self = *self - other;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
2015-11-17 01:36:14 +00:00
|
|
|
impl fmt::Debug for SystemTime {
|
2019-03-01 08:34:11 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-11-17 01:36:14 +00:00
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
/// respect to the system clock. Using `duration_since` on an existing
|
2016-12-14 15:14:19 +00:00
|
|
|
/// [`SystemTime`] instance can tell how far away from this point in time a
|
2015-11-17 01:36:14 +00:00
|
|
|
/// measurement lies, and using `UNIX_EPOCH + duration` can be used to create a
|
2016-12-14 15:14:19 +00:00
|
|
|
/// [`SystemTime`] instance to represent another fixed point in time.
|
|
|
|
///
|
|
|
|
/// [`SystemTime`]: ../../std/time/struct.SystemTime.html
|
2017-09-04 16:08:08 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
///
|
|
|
|
/// match SystemTime::now().duration_since(UNIX_EPOCH) {
|
|
|
|
/// Ok(n) => println!("1970-01-01 00:00:00 UTC was {} seconds ago!", n.as_secs()),
|
|
|
|
/// Err(_) => panic!("SystemTime before UNIX EPOCH!"),
|
|
|
|
/// }
|
|
|
|
/// ```
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
2015-11-17 01:36:14 +00:00
|
|
|
pub const UNIX_EPOCH: SystemTime = SystemTime(time::UNIX_EPOCH);
|
|
|
|
|
|
|
|
impl SystemTimeError {
|
|
|
|
/// Returns the positive duration which represents how far forward the
|
|
|
|
/// second system time was from the first.
|
|
|
|
///
|
2016-12-14 15:14:19 +00:00
|
|
|
/// A `SystemTimeError` is returned from the [`duration_since`] and [`elapsed`]
|
|
|
|
/// methods of [`SystemTime`] whenever the second system time represents a point later
|
2015-11-25 20:52:12 +00:00
|
|
|
/// in time than the `self` of the method call.
|
2016-12-14 15:14:19 +00:00
|
|
|
///
|
|
|
|
/// [`duration_since`]: ../../std/time/struct.SystemTime.html#method.duration_since
|
|
|
|
/// [`elapsed`]: ../../std/time/struct.SystemTime.html#method.elapsed
|
|
|
|
/// [`SystemTime`]: ../../std/time/struct.SystemTime.html
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::thread::sleep;
|
|
|
|
/// use std::time::{Duration, SystemTime};
|
|
|
|
///
|
|
|
|
/// let sys_time = SystemTime::now();
|
|
|
|
/// sleep(Duration::from_secs(1));
|
|
|
|
/// let new_sys_time = SystemTime::now();
|
|
|
|
/// match sys_time.duration_since(new_sys_time) {
|
|
|
|
/// Ok(_) => {}
|
|
|
|
/// Err(e) => println!("SystemTimeError difference: {:?}", e.duration()),
|
|
|
|
/// }
|
|
|
|
/// ```
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
2015-11-17 01:36:14 +00:00
|
|
|
pub fn duration(&self) -> Duration {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
2015-11-17 01:36:14 +00:00
|
|
|
impl Error for SystemTimeError {
|
2019-12-01 04:01:48 +00:00
|
|
|
#[allow(deprecated)]
|
2019-10-05 10:23:10 +00:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
"other time was not earlier than self"
|
|
|
|
}
|
2015-11-17 01:36:14 +00:00
|
|
|
}
|
|
|
|
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
#[stable(feature = "time2", since = "1.8.0")]
|
2015-11-17 01:36:14 +00:00
|
|
|
impl fmt::Display for SystemTimeError {
|
2019-03-01 08:34:11 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-11-17 01:36:14 +00:00
|
|
|
write!(f, "second time provided was later than self")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-13 01:24:16 +00:00
|
|
|
impl FromInner<time::SystemTime> for SystemTime {
|
|
|
|
fn from_inner(time: time::SystemTime) -> SystemTime {
|
|
|
|
SystemTime(time)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-17 01:36:14 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-10-05 10:23:10 +00:00
|
|
|
use super::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
2015-11-17 01:36:14 +00:00
|
|
|
|
2015-11-23 22:51:45 +00:00
|
|
|
macro_rules! assert_almost_eq {
|
2019-10-05 10:23:10 +00:00
|
|
|
($a:expr, $b:expr) => {{
|
2015-11-23 22:51:45 +00:00
|
|
|
let (a, b) = ($a, $b);
|
|
|
|
if a != b {
|
2019-10-05 10:23:10 +00:00
|
|
|
let (a, b) = if a > b { (a, b) } else { (b, a) };
|
|
|
|
assert!(a - Duration::new(0, 1000) <= b, "{:?} is not almost equal to {:?}", a, b);
|
2015-11-23 22:51:45 +00:00
|
|
|
}
|
2019-10-05 10:23:10 +00:00
|
|
|
}};
|
2015-11-23 22:51:45 +00:00
|
|
|
}
|
|
|
|
|
2015-11-17 01:36:14 +00:00
|
|
|
#[test]
|
|
|
|
fn instant_monotonic() {
|
|
|
|
let a = Instant::now();
|
|
|
|
let b = Instant::now();
|
|
|
|
assert!(b >= a);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn instant_elapsed() {
|
|
|
|
let a = Instant::now();
|
|
|
|
a.elapsed();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn instant_math() {
|
|
|
|
let a = Instant::now();
|
|
|
|
let b = Instant::now();
|
2018-11-17 09:50:38 +00:00
|
|
|
println!("a: {:?}", a);
|
|
|
|
println!("b: {:?}", b);
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
let dur = b.duration_since(a);
|
2018-11-17 09:50:38 +00:00
|
|
|
println!("dur: {:?}", dur);
|
2015-11-23 22:51:45 +00:00
|
|
|
assert_almost_eq!(b - dur, a);
|
|
|
|
assert_almost_eq!(a + dur, b);
|
2015-11-17 01:36:14 +00:00
|
|
|
|
|
|
|
let second = Duration::new(1, 0);
|
2015-11-23 22:51:45 +00:00
|
|
|
assert_almost_eq!(a - second + second, a);
|
2018-12-10 22:55:53 +00:00
|
|
|
assert_almost_eq!(a.checked_sub(second).unwrap().checked_add(second).unwrap(), a);
|
2018-12-04 01:29:57 +00:00
|
|
|
|
|
|
|
// checked_add_duration will not panic on overflow
|
|
|
|
let mut maybe_t = Some(Instant::now());
|
|
|
|
let max_duration = Duration::from_secs(u64::max_value());
|
|
|
|
// in case `Instant` can store `>= now + max_duration`.
|
|
|
|
for _ in 0..2 {
|
|
|
|
maybe_t = maybe_t.and_then(|t| t.checked_add(max_duration));
|
|
|
|
}
|
|
|
|
assert_eq!(maybe_t, None);
|
|
|
|
|
|
|
|
// checked_add_duration calculates the right time and will work for another year
|
|
|
|
let year = Duration::from_secs(60 * 60 * 24 * 365);
|
|
|
|
assert_eq!(a + year, a.checked_add(year).unwrap());
|
2015-11-17 01:36:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 18:53:47 +00:00
|
|
|
#[test]
|
|
|
|
fn instant_math_is_associative() {
|
|
|
|
let now = Instant::now();
|
|
|
|
let offset = Duration::from_millis(5);
|
|
|
|
// Changing the order of instant math shouldn't change the results,
|
|
|
|
// especially when the expression reduces to X + identity.
|
|
|
|
assert_eq!((now + offset) - now, (now - now) + offset);
|
|
|
|
}
|
|
|
|
|
2015-11-17 01:36:14 +00:00
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
2019-03-22 21:11:32 +00:00
|
|
|
fn instant_duration_since_panic() {
|
2015-11-17 01:36:14 +00:00
|
|
|
let a = Instant::now();
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
(a - Duration::new(1, 0)).duration_since(a);
|
2015-11-17 01:36:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 07:56:26 +00:00
|
|
|
#[test]
|
2019-03-22 21:11:32 +00:00
|
|
|
fn instant_checked_duration_since_nopanic() {
|
|
|
|
let now = Instant::now();
|
|
|
|
let earlier = now - Duration::new(1, 0);
|
|
|
|
let later = now + Duration::new(1, 0);
|
|
|
|
assert_eq!(earlier.checked_duration_since(now), None);
|
|
|
|
assert_eq!(later.checked_duration_since(now), Some(Duration::new(1, 0)));
|
|
|
|
assert_eq!(now.checked_duration_since(now), Some(Duration::new(0, 0)));
|
2019-02-12 07:56:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 12:08:44 +00:00
|
|
|
#[test]
|
2019-03-22 21:11:32 +00:00
|
|
|
fn instant_saturating_duration_since_nopanic() {
|
2019-02-13 12:08:44 +00:00
|
|
|
let a = Instant::now();
|
|
|
|
let ret = (a - Duration::new(1, 0)).saturating_duration_since(a);
|
2019-10-05 10:23:10 +00:00
|
|
|
assert_eq!(ret, Duration::new(0, 0));
|
2019-02-13 12:08:44 +00:00
|
|
|
}
|
|
|
|
|
2015-11-17 01:36:14 +00:00
|
|
|
#[test]
|
|
|
|
fn system_time_math() {
|
|
|
|
let a = SystemTime::now();
|
|
|
|
let b = SystemTime::now();
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
match b.duration_since(a) {
|
2015-11-17 01:36:14 +00:00
|
|
|
Ok(dur) if dur == Duration::new(0, 0) => {
|
2015-11-23 22:51:45 +00:00
|
|
|
assert_almost_eq!(a, b);
|
2015-11-17 01:36:14 +00:00
|
|
|
}
|
|
|
|
Ok(dur) => {
|
|
|
|
assert!(b > a);
|
2015-11-23 22:51:45 +00:00
|
|
|
assert_almost_eq!(b - dur, a);
|
|
|
|
assert_almost_eq!(a + dur, b);
|
2015-11-17 01:36:14 +00:00
|
|
|
}
|
|
|
|
Err(dur) => {
|
|
|
|
let dur = dur.duration();
|
|
|
|
assert!(a > b);
|
2015-11-23 22:51:45 +00:00
|
|
|
assert_almost_eq!(b + dur, a);
|
2017-09-03 09:31:43 +00:00
|
|
|
assert_almost_eq!(a - dur, b);
|
2015-11-17 01:36:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let second = Duration::new(1, 0);
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 23:52:29 +00:00
|
|
|
assert_almost_eq!(a.duration_since(a - second).unwrap(), second);
|
2019-10-05 10:23:10 +00:00
|
|
|
assert_almost_eq!(a.duration_since(a + second).unwrap_err().duration(), second);
|
2015-11-17 01:36:14 +00:00
|
|
|
|
2015-11-23 22:51:45 +00:00
|
|
|
assert_almost_eq!(a - second + second, a);
|
2018-12-10 22:55:53 +00:00
|
|
|
assert_almost_eq!(a.checked_sub(second).unwrap().checked_add(second).unwrap(), a);
|
2015-11-17 01:36:14 +00:00
|
|
|
|
2015-12-02 20:55:35 +00:00
|
|
|
let one_second_from_epoch = UNIX_EPOCH + Duration::new(1, 0);
|
2019-10-05 10:23:10 +00:00
|
|
|
let one_second_from_epoch2 =
|
|
|
|
UNIX_EPOCH + Duration::new(0, 500_000_000) + Duration::new(0, 500_000_000);
|
2015-12-02 20:55:35 +00:00
|
|
|
assert_eq!(one_second_from_epoch, one_second_from_epoch2);
|
2018-10-31 05:24:33 +00:00
|
|
|
|
|
|
|
// checked_add_duration will not panic on overflow
|
|
|
|
let mut maybe_t = Some(SystemTime::UNIX_EPOCH);
|
|
|
|
let max_duration = Duration::from_secs(u64::max_value());
|
|
|
|
// in case `SystemTime` can store `>= UNIX_EPOCH + max_duration`.
|
|
|
|
for _ in 0..2 {
|
2018-11-05 05:23:20 +00:00
|
|
|
maybe_t = maybe_t.and_then(|t| t.checked_add(max_duration));
|
2018-10-31 05:24:33 +00:00
|
|
|
}
|
|
|
|
assert_eq!(maybe_t, None);
|
|
|
|
|
|
|
|
// checked_add_duration calculates the right time and will work for another year
|
|
|
|
let year = Duration::from_secs(60 * 60 * 24 * 365);
|
2018-11-05 05:23:20 +00:00
|
|
|
assert_eq!(a + year, a.checked_add(year).unwrap());
|
2015-11-17 01:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn system_time_elapsed() {
|
|
|
|
let a = SystemTime::now();
|
|
|
|
drop(a.elapsed());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn since_epoch() {
|
|
|
|
let ts = SystemTime::now();
|
2019-03-12 23:09:20 +00:00
|
|
|
let a = ts.duration_since(UNIX_EPOCH + Duration::new(1, 0)).unwrap();
|
|
|
|
let b = ts.duration_since(UNIX_EPOCH).unwrap();
|
2015-11-17 01:36:14 +00:00
|
|
|
assert!(b > a);
|
|
|
|
assert_eq!(b - a, Duration::new(1, 0));
|
|
|
|
|
|
|
|
let thirty_years = Duration::new(1, 0) * 60 * 60 * 24 * 365 * 30;
|
2017-07-13 21:17:33 +00:00
|
|
|
|
|
|
|
// Right now for CI this test is run in an emulator, and apparently the
|
|
|
|
// aarch64 emulator's sense of time is that we're still living in the
|
|
|
|
// 70s.
|
|
|
|
//
|
|
|
|
// Otherwise let's assume that we're all running computers later than
|
|
|
|
// 2000.
|
|
|
|
if !cfg!(target_arch = "aarch64") {
|
|
|
|
assert!(a > thirty_years);
|
|
|
|
}
|
2015-11-17 01:36:14 +00:00
|
|
|
|
|
|
|
// let's assume that we're all running computers earlier than 2090.
|
|
|
|
// Should give us ~70 years to fix this!
|
|
|
|
let hundred_twenty_years = thirty_years * 4;
|
|
|
|
assert!(a < hundred_twenty_years);
|
|
|
|
}
|
|
|
|
}
|