Struct humantime::Timestamp

source ·
pub struct Timestamp(/* private fields */);
Expand description

A wrapper for SystemTime that has FromStr implementation

This is useful if you want to use it somewhere where FromStr is expected.

See parse_rfc3339_weak for the description of the format. The “weak” format is used as it’s more pemissive for human input as this is the expected use of the type (e.g. command-line parsing).

§Example

use std::time::SystemTime;
let x: SystemTime;
x = "2018-02-16T00:31:37Z".parse::<humantime::Timestamp>().unwrap().into();
assert_eq!(humantime::format_rfc3339(x).to_string(), "2018-02-16T00:31:37Z");

Methods from Deref<Target = SystemTime>§

1.28.0 · source

pub const UNIX_EPOCH: SystemTime = UNIX_EPOCH

1.8.0 · source

pub fn duration_since( &self, earlier: SystemTime, ) -> Result<Duration, SystemTimeError>

Returns the amount of time elapsed from an earlier point in time.

This function may fail because measurements taken earlier are not guaranteed to always be before later measurements (due to anomalies such as the system clock being adjusted either forwards or backwards). Instant can be used to measure elapsed time without this risk of failure.

If successful, Ok(Duration) is returned where the duration 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 new_sys_time = SystemTime::now();
let difference = new_sys_time.duration_since(sys_time)
    .expect("Clock may have gone backwards");
println!("{difference:?}");
1.8.0 · source

pub fn elapsed(&self) -> Result<Duration, SystemTimeError>

Returns the difference from this system time to the current clock time.

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 might 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.

To measure elapsed time reliably, use Instant instead.

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);
1.34.0 · source

pub fn checked_add(&self, duration: Duration) -> Option<SystemTime>

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.

1.34.0 · source

pub fn checked_sub(&self, duration: Duration) -> Option<SystemTime>

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§

source§

impl AsRef<SystemTime> for Timestamp

source§

fn as_ref(&self) -> &SystemTime

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Clone for Timestamp

source§

fn clone(&self) -> Timestamp

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Timestamp

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Deref for Timestamp

source§

type Target = SystemTime

The resulting type after dereferencing.
source§

fn deref(&self) -> &SystemTime

Dereferences the value.
source§

impl Display for Timestamp

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<SystemTime> for Timestamp

source§

fn from(dur: SystemTime) -> Timestamp

Converts to this type from the input type.
source§

impl FromStr for Timestamp

source§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Timestamp, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl Into<SystemTime> for Timestamp

source§

fn into(self) -> SystemTime

Converts this type into the (usually inferred) input type.
source§

impl PartialEq for Timestamp

source§

fn eq(&self, other: &Timestamp) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Timestamp

source§

impl StructuralPartialEq for Timestamp

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.