Struct unix_ts::Timestamp

source ·
pub struct Timestamp { /* private fields */ }
Expand description

A representation of a timestamp (seconds and nanos since the Unix epoch).

Timestamps are able to be easily converted into chrono DateTimes.

Implementations§

source§

impl Timestamp

source

pub const fn new(seconds: i64, nanos: u32) -> Timestamp

Create a new timestamp from the given number of seconds and nanos (nanoseconds).

The use of the ts!() macro in the unix-ts-macros crate is advised in lieu of calling this method directly for most situations.

Note: For negative timestamps, the nanos argument is always a positive offset. Therefore, the correct way to represent a timestamp of -0.25 seconds is to call new(-1, 750_000_000).

source

pub const fn from_nanos(nanos: i128) -> Timestamp

Create a timestamp from the given number of nanoseconds.

source

pub const fn from_micros(micros: i64) -> Timestamp

Create a timestamp from the given number of microseconds.

source

pub const fn from_millis(millis: i64) -> Timestamp

Create a timestamp from the given number of milliseconds.

source

pub const fn seconds(&self) -> i64

Return the seconds since the Unix epoch. Sub-second values are discarded.

Examples
use unix_ts::Timestamp;

let t = Timestamp::from(1335020400);
assert_eq!(t.seconds(), 1335020400);
source

pub const fn at_precision(&self, e: u8) -> i128

Return the time since the Unix epoch, as an integer, with the given precision.

Arguments
  • e (u8) - The precision for the returned integer, as a power of 10. (ex. 3 for milliseconds, 6 for microseconds, etc.). Must be a value between 0 and 9.
Examples
use unix_ts::Timestamp;

let t = Timestamp::from(1335020400);
assert_eq!(t.at_precision(3), 1335020400_000);
assert_eq!(t.at_precision(6), 1335020400_000_000);
source

pub fn subsec(&self, e: u8) -> u32

Return the subsecond component at the specified precision (ex. 3 for milliseconds, 6 for microseconds); max precision is 9.

Arguments
  • e (u8) - The precision for the returned subsecond value, as a power of 10 (ex. 3 for milliseconds, 6 for microseconds, etc.). Must be a value between 0 and 9.
Examples
use unix_ts::Timestamp;

let t = Timestamp::new(1335020400, 500_000_000);
assert_eq!(t.subsec(1), 5);
assert_eq!(t.subsec(3), 500);

Trait Implementations§

source§

impl Add<Duration> for Timestamp

source§

fn add(self, other: Duration) -> Timestamp

Add the provided duration to the timestamp.

§

type Output = Timestamp

The resulting type after applying the + operator.
source§

impl<T: Into<i64> + Int> Add<T> for Timestamp

source§

fn add(self, other: T) -> Timestamp

Add the provided duration to the timestamp.

§

type Output = Timestamp

The resulting type after applying the + operator.
source§

impl Add<Timestamp> for Timestamp

source§

fn add(self, other: Timestamp) -> Timestamp

Add two timestamps to one another and return the result.

§

type Output = Timestamp

The resulting type after applying the + operator.
source§

impl AddAssign<Duration> for Timestamp

source§

fn add_assign(&mut self, other: Duration)

Add the provided duration to the timestamp, in-place.

source§

impl<T: Into<i64> + Int> AddAssign<T> for Timestamp

source§

fn add_assign(&mut self, other: T)

Add the provided duration to the timestamp, in-place.

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 Default for Timestamp

source§

fn default() -> Timestamp

Returns the “default value” for a type. Read more
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<Duration> for Timestamp

source§

fn from(dur: Duration) -> Self

Create a new timestamp from the given std::time::Duration.

source§

impl<T: Into<i64> + Int> From<T> for Timestamp

source§

fn from(secs: T) -> Self

Create a new timestamp for the given number of seconds.

source§

impl From<Timestamp> for Duration

source§

fn from(ts: Timestamp) -> Self

Create a new Duration from the given timestamp.

source§

impl Ord for Timestamp

source§

fn cmp(&self, other: &Timestamp) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<Timestamp> for Timestamp

source§

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

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

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

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

impl PartialOrd<Timestamp> for Timestamp

source§

fn partial_cmp(&self, other: &Timestamp) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

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

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

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

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

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

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

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

impl<T: Into<i64> + Int> Rem<T> for Timestamp

source§

fn rem(self, other: T) -> Timestamp

Subtract the provided duration to the timestamp.

§

type Output = Timestamp

The resulting type after applying the % operator.
source§

impl Sub<Duration> for Timestamp

source§

fn sub(self, other: Duration) -> Timestamp

Subtract the provided duration from the timestamp.

§

type Output = Timestamp

The resulting type after applying the - operator.
source§

impl<T: Into<i64> + Int> Sub<T> for Timestamp

source§

fn sub(self, other: T) -> Timestamp

Subtract the provided duration to the timestamp.

§

type Output = Timestamp

The resulting type after applying the - operator.
source§

impl Sub<Timestamp> for Timestamp

source§

fn sub(self, other: Timestamp) -> Timestamp

Subtract the provided timestamp from this one and return the result.

§

type Output = Timestamp

The resulting type after applying the - operator.
source§

impl SubAssign<Duration> for Timestamp

source§

fn sub_assign(&mut self, other: Duration)

Subtract the provided duration to the timestamp, in-place.

source§

impl<T: Into<i64> + Int> SubAssign<T> for Timestamp

source§

fn sub_assign(&mut self, other: T)

Subtract the provided duration to the timestamp, in-place.

source§

impl Copy for Timestamp

source§

impl Eq for Timestamp

source§

impl StructuralEq for Timestamp

source§

impl StructuralPartialEq for Timestamp

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.