pub struct Instant { /* private fields */ }Expand description
An absolute point in time.
An Instant is a wrapper around a signed integer that holds the number of nanoseconds since
an arbitrary point in time, e.g. system startup.
- A value of
0is arbitrary. - Values < 0 indicate a time before the start point.
Implementations§
Source§impl Instant
impl Instant
Sourcepub fn from_nanos(nanos: i64) -> Instant
pub fn from_nanos(nanos: i64) -> Instant
Sourcepub const fn as_nanos(&self) -> i64
pub const fn as_nanos(&self) -> i64
The number of nanoseconds that have passed since the ZERO point.
Sourcepub const fn subsec_nanos(&self) -> i64
pub const fn subsec_nanos(&self) -> i64
The number of fractional nanoseconds since the ZERO point.
Sourcepub fn duration_since(&self, earlier: Instant) -> Duration
pub fn duration_since(&self, earlier: Instant) -> Duration
The amount of time elapsed since an earlier Instant, or 0.
§Examples
let earlier = Instant::from_nanos(1_234_567_890);
let later = Instant::from_nanos(2_234_567_890);
assert_eq!(later.duration_since(earlier), Duration::from_nanos(1_000_000_000));
assert_eq!(earlier.duration_since(later), Duration::ZERO);
assert_eq!(earlier.duration_since(earlier), Duration::ZERO);Sourcepub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration>
pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration>
The amount of time elapsed since an earlier Instant, or None.
§Examples
let earlier = Instant::from_nanos(1_234_567_890);
let later = Instant::from_nanos(2_234_567_890);
assert_eq!(later.checked_duration_since(earlier), Some(Duration::from_nanos(1_000_000_000)));
assert_eq!(earlier.checked_duration_since(later), None);
assert_eq!(earlier.checked_duration_since(earlier), Some(Duration::ZERO));Sourcepub fn saturating_duration_since(&self, earlier: Instant) -> Duration
pub fn saturating_duration_since(&self, earlier: Instant) -> Duration
The amount of time elapsed since an earlier Instant, or 0.
Sourcepub fn checked_add(&self, duration: Duration) -> Option<Instant>
pub fn checked_add(&self, duration: Duration) -> Option<Instant>
Returns the result of self + duration if the result can be represented by the underlying
data structure, None otherwise.
§Examples
let instant = Instant::from_nanos(1_234_567_890);
assert_eq!(instant.checked_add(Duration::from_secs(1)), Some(Instant::from_nanos(2_234_567_890)));
assert_eq!(instant.checked_add(Duration::ZERO), Some(instant));Sourcepub fn checked_sub(&self, duration: Duration) -> Option<Instant>
pub fn checked_sub(&self, duration: Duration) -> Option<Instant>
Returns the result of self - duration if the result can be represented by the underlying
data structure, None otherwise.
§Examples
let instant = Instant::from_nanos(1_234_567_890);
assert_eq!(instant.checked_sub(Duration::from_secs(1)), Some(Instant::from_nanos(234_567_890)));
assert_eq!(instant.checked_sub(Duration::from_secs(2)), Some(Instant::from_nanos(-765_432_110)));
assert_eq!(instant.checked_sub(Duration::ZERO), Some(instant));Sourcepub fn from_std(instant: Instant) -> Instant
pub fn from_std(instant: Instant) -> Instant
Construct an Instant from a std::time::Instant based its the elapsed time.
Can be used if you have a base std::time::Instant where you can create Instants as
needed.
§Example
let std_instant = std::time::Instant::now();
std::thread::sleep(Duration::from_secs(1));
assert!(Instant::from_std(std_instant) >= Instant::from_nanos(1_000_000_000));Sourcepub fn from_system(sys_time: SystemTime) -> Instant
pub fn from_system(sys_time: SystemTime) -> Instant
Sourcepub fn to_std(&self, base_instant: Instant) -> Instant
pub fn to_std(&self, base_instant: Instant) -> Instant
Convert this Instant to a std::time::Instant using a base instant.
This function takes a base std::time::Instant and adds the duration represented
by this Instant to create a corresponding std::time::Instant.
§Example
let base = std::time::Instant::now();
let instant = Instant::from_nanos(1_000_000_000); // 1 second
let std_instant = instant.to_std(base);
// The resulting std_instant should be 1 second after base
assert!(std_instant.duration_since(base) == Duration::from_secs(1));Sourcepub fn to_system(&self, base_system_time: SystemTime) -> SystemTime
pub fn to_system(&self, base_system_time: SystemTime) -> SystemTime
Convert this Instant to a std::time::SystemTime using a base system time.
This function takes a base std::time::SystemTime and adds the duration represented
by this Instant to create a corresponding std::time::SystemTime.
§Example
let base = std::time::SystemTime::now();
let instant = Instant::from_nanos(1_000_000_000); // 1 second
let sys_time = instant.to_system(base);
// The resulting sys_time should be 1 second after base
assert!(sys_time.duration_since(base).unwrap() == Duration::from_secs(1));Trait Implementations§
Source§impl AddAssign<Duration> for Instant
impl AddAssign<Duration> for Instant
Source§fn add_assign(&mut self, rhs: Duration)
fn add_assign(&mut self, rhs: Duration)
+= operation. Read moreSource§impl Ord for Instant
impl Ord for Instant
Source§impl PartialOrd for Instant
impl PartialOrd for Instant
Source§impl SubAssign<Duration> for Instant
impl SubAssign<Duration> for Instant
Source§fn sub_assign(&mut self, rhs: Duration)
fn sub_assign(&mut self, rhs: Duration)
-= operation. Read moreimpl Copy for Instant
impl Eq for Instant
impl StructuralPartialEq for Instant
Auto Trait Implementations§
impl Freeze for Instant
impl RefUnwindSafe for Instant
impl Send for Instant
impl Sync for Instant
impl Unpin for Instant
impl UnwindSafe for Instant
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)