pub struct Instant { /* private fields */ }
Expand description
An precise instant relative to the UNIX epoch, with nanosecond precision.
Implementations§
Source§impl Instant
impl Instant
Sourcepub fn at(secs: u64, nanos: u32) -> Self
pub fn at(secs: u64, nanos: u32) -> Self
Creates an Instant
at the specified seconds and nanoseconds after the
UNIX epoch.
§Examples
use unix_time::Instant;
let instant = Instant::at(42, 182870024);
assert_eq!(format!("{:?}", instant), "Instant { secs: 42, nanos: 182870024 }");
Sourcepub fn secs(&self) -> u64
pub fn secs(&self) -> u64
Returns the number of whole seconds that spaces self
from the UNIX
epoch.
The returned value does not include the fractional (nanosecond) part of
the duration, which can be obtained using subsec_nanos
.
§Examples
use unix_time::Instant;
let duration = Instant::at(5, 730023852);
assert_eq!(duration.secs(), 5);
To determine the total number of seconds represented by the Duration
,
use secs
in combination with subsec_nanos
:
use unix_time::Instant;
let instant = Instant::at(5, 730023852);
assert_eq!(5.730023852,
instant.secs() as f64
+ instant.subsec_nanos() as f64 * 1e-9);
Sourcepub fn subsec_nanos(&self) -> u32
pub fn subsec_nanos(&self) -> u32
Returns the fractional part that spaces self
from the UNIX epoch in
nanoseconds.
This method does not return the total duration since the UNIX epoch in nanoseconds. The returned number always represents a fractional portion of a second (i.e., it is less than one billion).
§Examples
use unix_time::Instant;
let instant = Instant::at(5, 10_000_000);
assert_eq!(instant.secs(), 5);
assert_eq!(instant.subsec_nanos(), 10_000_000);
Sourcepub fn checked_add(&self, duration: Duration) -> Option<Instant>
pub fn checked_add(&self, duration: Duration) -> Option<Instant>
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.
Sourcepub fn checked_sub(&self, duration: Duration) -> Option<Instant>
pub fn checked_sub(&self, duration: Duration) -> Option<Instant>
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.
Sourcepub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration>
pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration>
Returns the amount of time elapsed from another instant to this one, or None if that instant is later than this one.
§Examples
use unix_time::Instant;
use std::time::Duration;
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
Sourcepub fn duration_since(&self, earlier: Instant) -> Duration
pub fn duration_since(&self, earlier: Instant) -> Duration
Returns the amount of time elapsed from another instant to this one.
§Panics
This function will panic if earlier
is later than self
.
§Examples
use std::time::Duration;
use unix_time::Instant;
use std::thread::sleep;
let now = Instant::now();
sleep(Duration::new(1, 0).into());
let new_now = Instant::now();
println!("{:?}", new_now.duration_since(now));
Sourcepub fn saturating_duration_since(&self, earlier: Instant) -> Duration
pub fn saturating_duration_since(&self, earlier: Instant) -> Duration
Returns the amount of time elapsed from another instant to this one, or zero duration if that instant is later than this one.
§Examples
use std::time::Duration;
use unix_time::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
Sourcepub fn elapsed(&self) -> Duration
pub fn elapsed(&self) -> Duration
Returns the amount of time elapsed since this instant was created.
§Panics
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.
§Examples
use std::thread::sleep;
use std::time::Duration;
use unix_time::Instant;
let instant = Instant::now();
let three_secs = Duration::from_secs(3);
sleep(three_secs);
assert!(instant.elapsed() >= three_secs);
Trait Implementations§
Source§impl AddAssign<Duration> for Instant
impl AddAssign<Duration> for Instant
Source§fn add_assign(&mut self, other: Duration)
fn add_assign(&mut self, other: Duration)
+=
operation. Read moreSource§impl Archive for Instant
impl Archive for Instant
Source§const COPY_OPTIMIZATION: CopyOptimization<Self>
const COPY_OPTIMIZATION: CopyOptimization<Self>
serialize
. Read moreSource§type Archived = ArchivedInstant
type Archived = ArchivedInstant
Source§type Resolver = InstantResolver
type Resolver = InstantResolver
Source§impl<'de> Deserialize<'de> for Instant
impl<'de> Deserialize<'de> for Instant
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§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, other: Duration)
fn sub_assign(&mut self, other: 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§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> ArchiveUnsized for Twhere
T: Archive,
impl<T> ArchiveUnsized for Twhere
T: Archive,
Source§type Archived = <T as Archive>::Archived
type Archived = <T as Archive>::Archived
Archive
, it may be
unsized. Read moreSource§fn archived_metadata(
&self,
) -> <<T as ArchiveUnsized>::Archived as ArchivePointee>::ArchivedMetadata
fn archived_metadata( &self, ) -> <<T as ArchiveUnsized>::Archived as ArchivePointee>::ArchivedMetadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out
indicating that a T
is niched.