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
impl Timestamp
sourcepub const fn new(seconds: i64, nanos: u32) -> Timestamp
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)
.
sourcepub const fn from_nanos(nanos: i128) -> Timestamp
pub const fn from_nanos(nanos: i128) -> Timestamp
Create a timestamp from the given number of nanoseconds.
sourcepub const fn from_micros(micros: i64) -> Timestamp
pub const fn from_micros(micros: i64) -> Timestamp
Create a timestamp from the given number of microseconds.
sourcepub const fn from_millis(millis: i64) -> Timestamp
pub const fn from_millis(millis: i64) -> Timestamp
Create a timestamp from the given number of milliseconds.
sourcepub const fn seconds(&self) -> i64
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);
sourcepub const fn at_precision(&self, e: u8) -> i128
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);
sourcepub fn subsec(&self, e: u8) -> u32
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 AddAssign<Duration> for Timestamp
impl AddAssign<Duration> for Timestamp
source§fn add_assign(&mut self, other: Duration)
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
impl<T: Into<i64> + Int> AddAssign<T> for Timestamp
source§fn add_assign(&mut self, other: T)
fn add_assign(&mut self, other: T)
Add the provided duration to the timestamp, in-place.
source§impl Ord for Timestamp
impl Ord for Timestamp
source§impl PartialEq<Timestamp> for Timestamp
impl PartialEq<Timestamp> for Timestamp
source§impl PartialOrd<Timestamp> for Timestamp
impl PartialOrd<Timestamp> for Timestamp
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl SubAssign<Duration> for Timestamp
impl SubAssign<Duration> for Timestamp
source§fn sub_assign(&mut self, other: Duration)
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
impl<T: Into<i64> + Int> SubAssign<T> for Timestamp
source§fn sub_assign(&mut self, other: T)
fn sub_assign(&mut self, other: T)
Subtract the provided duration to the timestamp, in-place.