Struct Timestamp
pub struct Timestamp { /* private fields */ }
An instant in time represented as the number of nanoseconds since the Unix epoch.
A timestamp is always in the Unix timescale with a UTC offset of zero.
Implementations
impl Timestamp
const MIN: Timestamp = _;The minimum allow Unix timestamp.
const MAX: Timestamp = _;The maximum allow Unix timestamp.
const UNIX_EPOCH: Timestamp = _;The Unix epoch represented as a timestamp.
const fn new(secs: i64, nanos: i32) -> Result<Timestamp, RangeError>Create a new timestamp from the given number of seconds and its sub-second component.
This returns an error if
nanosis not in the range specified bySignedSubsecNanosecond. An error is also returned whensecsis not in the range specified byUnixEpochSeconds.const fn constant(second: i64, nanosecond: i32) -> TimestampCreates a new
Timestampvalue in aconstcontext.This is identical to
Timestamp::new, except that it panics whenTimestamp::newwould return an error. This can be more convenient in aconstcontext where unwrapping aResultis not ergonomic.const fn from_second(second: i64) -> Result<Timestamp, RangeError>Constructs a timestamp from seconds since the Unix epoch.
This is preferred to
Timestamp::newwhen it is known that the sub-second component is always0. In particular, this generates less code and is likely to be faster.An error is returned when
secondis not in the range specified byUnixEpochSeconds.const fn from_millisecond(millisecond: i64) -> Result<Timestamp, RangeError>Constructs a timestamp from milliseconds since the Unix epoch.
An error is returned when
millisecondis not in the range specified byUnixEpochMilliseconds.const fn from_microsecond(microsecond: i64) -> Result<Timestamp, RangeError>Constructs a timestamp from microseconds since the Unix epoch.
An error is returned when
microsecondis not in the range specified byUnixEpochMicroseconds.const fn from_nanosecond(nanosecond: i128) -> Result<Timestamp, RangeError>Constructs a timestamp from nanoseconds since the Unix epoch.
An error is returned when
nanosecondrefers to a timestamp outside of the rangeTimestamp::MINtoTimestamp::MAX.const fn as_second(self) -> i64Returns this timestamp as a number of seconds since the Unix epoch.
This only returns the number of whole seconds. That is, if there are any fractional seconds in this timestamp, then they are truncated.
const fn as_millisecond(self) -> i64Returns this timestamp as a number of milliseconds since the Unix epoch.
This only returns the number of whole milliseconds. That is, if there are any fractional milliseconds in this timestamp, then they are truncated.
const fn as_microsecond(self) -> i64Returns this timestamp as a number of microseconds since the Unix epoch.
This only returns the number of whole microseconds. That is, if there are any fractional microseconds in this timestamp, then they are truncated.
const fn as_nanosecond(self) -> i128Returns this timestamp as a number of nanoseconds since the Unix epoch.
const fn subsec_millisecond(&self) -> i32Returns the fractional second component of this timestamp in units of microseconds.
The value returned is negative when the timestamp is negative. It is guaranteed that the range of the value returned is in the inclusive range
-999_999..=999_999.const fn subsec_microsecond(&self) -> i32Returns the fractional second component of this timestamp in units of milliseconds.
The value returned is negative when the timestamp is negative. It is guaranteed that the range of the value returned is in the inclusive range
-999..=999.const fn subsec_nanosecond(&self) -> i32Returns the fractional second component of this timestamp in units of nanoseconds.
The value returned is negative when the timestamp is negative. It is guaranteed that the range of the value returned is in the inclusive range
-999,999,999..=999,999,999.const fn signum(self) -> i8Returns a number that represents the sign of this timestamp.
- When
Timestamp::is_zerois true, this returns0. - When
Timestamp::is_positiveis true, this returns1. - When
Timestamp::is_negativeis true, this returns-1.
The above cases are mutually exclusive.
Example
use Timestamp; assert_eq!; let ts = new.unwrap; assert_eq!; // The mixed signs were normalized away! assert_eq!; assert_eq!; // The same applies for negative timestamps. let ts = new.unwrap; assert_eq!; assert_eq!; assert_eq!;- When
const fn is_zero(self) -> boolReturns true if and only if this timestamp corresponds to the instant in time known as the Unix epoch.
Example
use Timestamp; assert!;const fn is_positive(&self) -> boolReturns true when this timestamp is positive. That is, after the Unix epoch.
Example
use Timestamp; let ts = new.unwrap; assert!;const fn is_negative(&self) -> boolReturns true when this timestamp is negative. That is, before the Unix epoch.
Example
use Timestamp; let ts = new.unwrap; assert!;const fn to_datetime(&self, offset: Offset) -> DateTimeConverts a Unix timestamp with an offset to a Gregorian datetime.
The offset should correspond to the number of seconds required to add to this timestamp to get the local time.
const fn checked_add(self, seconds: i64, nanos: i32) -> Result<Timestamp, RangeError>Add the given number of seconds and nanoseconds to this timestamp.
If this would result in a timestamp outside of its boundaries, then this returns an error.
Examples
use Timestamp; let mkts = ; let ts = mkts; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert!; assert!; let ts = UNIX_EPOCH; let max = MAX.as_second; assert!; assert!; assert!; assert!;const fn checked_sub(self, seconds: i64, nanos: i32) -> Result<Timestamp, RangeError>Subtracts the given number of seconds and nanoseconds from this timestamp.
Examples
use Timestamp; let mkts = ; let ts = mkts; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert!; assert!; let ts = UNIX_EPOCH; let min = MIN.as_second; assert!; assert!; assert!; assert!;const fn checked_add_seconds(self, seconds: i64) -> Result<Timestamp, RangeError>Add the given number of seconds to this timestamp.
If this would result in a timestamp outside of its boundaries, then this returns an error.
The nanosecond component of the timestamp returned is guaranteed to match the nanosecond component of
self.Examples
use Timestamp; let mkts = ; let ts = mkts; assert_eq!; assert_eq!; assert_eq!; let ts = mkts; assert_eq!; assert_eq!; assert_eq!; assert!; assert!;const fn checked_sub_seconds(self, seconds: i64) -> Result<Timestamp, RangeError>Subtracts the given number of seconds from this timestamp.
Trait Implementations
impl Add<(i64, i32)> for Timestamp
type Output = Timestamp;fn add(self, (seconds, nanoseconds): (i64, i32)) -> Timestamp
impl Add<i64> for Timestamp
type Output = Timestamp;fn add(self, seconds: i64) -> Timestamp
impl AddAssign<(i64, i32)> for Timestamp
fn add_assign(&mut self, rhs: (i64, i32))
impl AddAssign<i64> for Timestamp
fn add_assign(&mut self, rhs: i64)
impl Clone for Timestamp
fn clone(&self) -> Timestamp
impl Copy for Timestamp
impl Debug for Timestamp
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Default for Timestamp
fn default() -> Timestamp
impl Eq for Timestamp
impl Hash for Timestamp
fn hash<__H: Hasher>(&self, state: &mut __H)
impl Ord for Timestamp
fn cmp(&self, other: &Timestamp) -> Ordering
impl PartialEq for Timestamp
fn eq(&self, other: &Timestamp) -> bool
impl PartialOrd for Timestamp
fn partial_cmp(&self, other: &Timestamp) -> Option<Ordering>
impl StructuralPartialEq for Timestamp
impl Sub<(i64, i32)> for Timestamp
type Output = Timestamp;fn sub(self, (seconds, nanoseconds): (i64, i32)) -> Timestamp
impl Sub<i64> for Timestamp
type Output = Timestamp;fn sub(self, seconds: i64) -> Timestamp
impl SubAssign<(i64, i32)> for Timestamp
fn sub_assign(&mut self, rhs: (i64, i32))
impl SubAssign<i64> for Timestamp
fn sub_assign(&mut self, rhs: i64)
Auto Trait Implementations
impl Freeze for Timestamp
impl RefUnwindSafe for Timestamp
impl Send for Timestamp
impl Sync for Timestamp
impl Unpin for Timestamp
impl UnsafeUnpin for Timestamp
impl UnwindSafe for Timestamp
Blanket Implementations
impl<T> Any for Timestamp
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Timestamp
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Timestamp
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for Timestamp
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for Timestamp
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Timestamp
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T, U> Into<U> for Timestamp
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Timestamp
where
U: Into<T>,
type Error = Infallible;fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for Timestamp
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>