Skip to main content

Timestamp

Struct Timestamp 

Source
pub struct Timestamp { /* private fields */ }
Expand description

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§

Source§

impl Timestamp

Source

pub const MIN: Timestamp

The minimum allow Unix timestamp.

Source

pub const MAX: Timestamp

The maximum allow Unix timestamp.

Source

pub const UNIX_EPOCH: Timestamp

The Unix epoch represented as a timestamp.

Source

pub 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 nanos is not in the range specified by SignedSubsecNanosecond. An error is also returned when secs is not in the range specified by UnixEpochSeconds.

Source

pub const fn constant(second: i64, nanosecond: i32) -> Timestamp

Creates a new Timestamp value in a const context.

This is identical to Timestamp::new, except that it panics when Timestamp::new would return an error. This can be more convenient in a const context where unwrapping a Result is not ergonomic.

Source

pub const fn from_second(second: i64) -> Result<Timestamp, RangeError>

Constructs a timestamp from seconds since the Unix epoch.

This is preferred to Timestamp::new when it is known that the sub-second component is always 0. In particular, this generates less code and is likely to be faster.

An error is returned when second is not in the range specified by UnixEpochSeconds.

Source

pub const fn from_millisecond(millisecond: i64) -> Result<Timestamp, RangeError>

Constructs a timestamp from milliseconds since the Unix epoch.

An error is returned when millisecond is not in the range specified by UnixEpochMilliseconds.

Source

pub const fn from_microsecond(microsecond: i64) -> Result<Timestamp, RangeError>

Constructs a timestamp from microseconds since the Unix epoch.

An error is returned when microsecond is not in the range specified by UnixEpochMicroseconds.

Source

pub const fn from_nanosecond(nanosecond: i128) -> Result<Timestamp, RangeError>

Constructs a timestamp from nanoseconds since the Unix epoch.

An error is returned when nanosecond refers to a timestamp outside of the range Timestamp::MIN to Timestamp::MAX.

Source

pub const fn as_second(self) -> i64

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

Source

pub const fn as_millisecond(self) -> i64

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

Source

pub const fn as_microsecond(self) -> i64

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

Source

pub const fn as_nanosecond(self) -> i128

Returns this timestamp as a number of nanoseconds since the Unix epoch.

Source

pub const fn subsec_millisecond(&self) -> i32

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

Source

pub const fn subsec_microsecond(&self) -> i32

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

Source

pub const fn subsec_nanosecond(&self) -> i32

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

Source

pub const fn signum(self) -> i8

Returns a number that represents the sign of this timestamp.

The above cases are mutually exclusive.

§Example
use jiff_core::Timestamp;

assert_eq!(0, Timestamp::UNIX_EPOCH.signum());

let ts = Timestamp::new(5, -999_999_999).unwrap();
assert_eq!(ts.signum(), 1);
// The mixed signs were normalized away!
assert_eq!(ts.as_second(), 4);
assert_eq!(ts.subsec_nanosecond(), 1);

// The same applies for negative timestamps.
let ts = Timestamp::new(-5, 999_999_999).unwrap();
assert_eq!(ts.signum(), -1);
assert_eq!(ts.as_second(), -4);
assert_eq!(ts.subsec_nanosecond(), -1);
Source

pub const fn is_zero(self) -> bool

Returns true if and only if this timestamp corresponds to the instant in time known as the Unix epoch.

§Example
use jiff_core::Timestamp;

assert!(Timestamp::UNIX_EPOCH.is_zero());
Source

pub const fn is_positive(&self) -> bool

Returns true when this timestamp is positive. That is, after the Unix epoch.

§Example
use jiff_core::Timestamp;

let ts = Timestamp::new(0, 1).unwrap();
assert!(ts.is_positive());
Source

pub const fn is_negative(&self) -> bool

Returns true when this timestamp is negative. That is, before the Unix epoch.

§Example
use jiff_core::Timestamp;

let ts = Timestamp::new(0, -1).unwrap();
assert!(ts.is_negative());
Source

pub const fn to_datetime(&self, offset: Offset) -> DateTime

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

Source

pub 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 jiff_core::Timestamp;

let mkts = |sec, nano| Timestamp::new(sec, nano).unwrap();
let ts = mkts(123, 0);

assert_eq!(ts.checked_add(1, 0), Ok(mkts(124, 0)));
assert_eq!(ts.checked_add(1, 1), Ok(mkts(124, 1)));
assert_eq!(ts.checked_add(1, -1), Ok(mkts(123, 999_999_999)));
assert_eq!(ts.checked_add(0, 1), Ok(mkts(123, 1)));
assert_eq!(ts.checked_add(0, -1), Ok(mkts(122, 999_999_999)));
assert_eq!(ts.checked_add(-1, 0), Ok(mkts(122, 0)));
assert_eq!(ts.checked_add(-1, 1), Ok(mkts(122, 1)));
assert_eq!(ts.checked_add(-1, -1), Ok(mkts(121, 999_999_999)));

assert_eq!(ts.checked_add(0, i32::MIN), Ok(mkts(121, -147_483_648)));
assert_eq!(ts.checked_add(1, i32::MIN), Ok(mkts(122, -147_483_648)));
assert_eq!(ts.checked_add(-1, i32::MIN), Ok(mkts(120, -147_483_648)));
assert_eq!(ts.checked_add(0, i32::MAX), Ok(mkts(125, 147_483_647)));
assert_eq!(ts.checked_add(1, i32::MAX), Ok(mkts(126, 147_483_647)));
assert_eq!(ts.checked_add(-1, i32::MAX), Ok(mkts(124, 147_483_647)));

assert!(ts.checked_add(i64::MAX, 0).is_err());
assert!(ts.checked_add(i64::MIN, 0).is_err());

let ts = Timestamp::UNIX_EPOCH;
let max = Timestamp::MAX.as_second();
assert!(ts.checked_add(max, 0).is_ok());
assert!(ts.checked_add(max + 1, 0).is_err());
assert!(ts.checked_add(max, 999_999_999).is_ok());
assert!(ts.checked_add(max, 1_000_000_000).is_err());
Source

pub 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 jiff_core::Timestamp;

let mkts = |sec, nano| Timestamp::new(sec, nano).unwrap();
let ts = mkts(123, 0);

assert_eq!(ts.checked_sub(1, 0), Ok(mkts(122, 0)));
assert_eq!(ts.checked_sub(1, 1), Ok(mkts(121, 999_999_999)));
assert_eq!(ts.checked_sub(1, -1), Ok(mkts(122, 1)));
assert_eq!(ts.checked_sub(0, 1), Ok(mkts(122, 999_999_999)));
assert_eq!(ts.checked_sub(0, -1), Ok(mkts(123, 1)));
assert_eq!(ts.checked_sub(-1, 0), Ok(mkts(124, 0)));
assert_eq!(ts.checked_sub(-1, 1), Ok(mkts(123, 999_999_999)));
assert_eq!(ts.checked_sub(-1, -1), Ok(mkts(124, 1)));

assert_eq!(ts.checked_sub(0, i32::MIN), Ok(mkts(125, 147_483_648)));
assert_eq!(ts.checked_sub(1, i32::MIN), Ok(mkts(124, 147_483_648)));
assert_eq!(ts.checked_sub(-1, i32::MIN), Ok(mkts(126, 147_483_648)));
assert_eq!(ts.checked_sub(0, i32::MAX), Ok(mkts(121, -147_483_647)));
assert_eq!(ts.checked_sub(1, i32::MAX), Ok(mkts(120, -147_483_647)));
assert_eq!(ts.checked_sub(-1, i32::MAX), Ok(mkts(122, -147_483_647)));

assert!(ts.checked_sub(i64::MAX, 0).is_err());
assert!(ts.checked_sub(i64::MIN, 0).is_err());

let ts = Timestamp::UNIX_EPOCH;
let min = Timestamp::MIN.as_second();
assert!(ts.checked_sub(-min, 0).is_ok());
assert!(ts.checked_sub(-(min - 1), 0).is_err());
assert!(ts.checked_sub(-min, 999_999_999).is_ok());
assert!(ts.checked_sub(-min, 1_000_000_000).is_err());
Source

pub 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 jiff_core::Timestamp;

let mkts = |sec, nano| Timestamp::new(sec, nano).unwrap();

let ts = mkts(123, 0);
assert_eq!(ts.checked_add_seconds(0), Ok(mkts(123, 0)));
assert_eq!(ts.checked_add_seconds(1), Ok(mkts(124, 0)));
assert_eq!(ts.checked_add_seconds(-1), Ok(mkts(122, 0)));

let ts = mkts(123, 999_999_999);
assert_eq!(ts.checked_add_seconds(0), Ok(mkts(123, 999_999_999)));
assert_eq!(ts.checked_add_seconds(1), Ok(mkts(124, 999_999_999)));
assert_eq!(ts.checked_add_seconds(-1), Ok(mkts(122, 999_999_999)));

assert!(ts.checked_add_seconds(i64::MIN).is_err());
assert!(ts.checked_add_seconds(i64::MAX).is_err());
Source

pub const fn checked_sub_seconds( self, seconds: i64, ) -> Result<Timestamp, RangeError>

Subtracts the given number of seconds from this timestamp.

Trait Implementations§

Source§

impl Add<(i64, i32)> for Timestamp

Adds a number of seconds and nanoseconds to a Timestamp.

§Panics

When adding would result in a value outside the boundaries of a Timestamp.

§Example

use jiff_core::Timestamp;

let ts = Timestamp::new(123, 999_999_999).unwrap();;
assert_eq!(ts + (400, 1), Timestamp::new(524, 0).unwrap());
Source§

type Output = Timestamp

The resulting type after applying the + operator.
Source§

fn add(self, (seconds, nanoseconds): (i64, i32)) -> Timestamp

Performs the + operation. Read more
Source§

impl Add<i64> for Timestamp

Adds a number of seconds to a Timestamp.

§Panics

When adding would result in a value outside the boundaries of a Timestamp.

§Example

use jiff_core::Timestamp;

let ts = Timestamp::new(123, 999_999_999).unwrap();;
assert_eq!(ts + 400, Timestamp::new(523, 999_999_999).unwrap());
Source§

type Output = Timestamp

The resulting type after applying the + operator.
Source§

fn add(self, seconds: i64) -> Timestamp

Performs the + operation. Read more
Source§

impl AddAssign<(i64, i32)> for Timestamp

Adds a number of seconds and nanoseconds to a Timestamp.

§Panics

When adding would result in a value outside the boundaries of a Timestamp.

Source§

fn add_assign(&mut self, rhs: (i64, i32))

Performs the += operation. Read more
Source§

impl AddAssign<i64> for Timestamp

Adds a number of seconds to a Timestamp.

§Panics

When adding would result in a value outside the boundaries of a Timestamp.

Source§

fn add_assign(&mut self, rhs: i64)

Performs the += operation. Read more
Source§

impl Clone for Timestamp

Source§

fn clone(&self) -> Timestamp

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Timestamp

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

Source§

impl Format for Timestamp

Source§

fn format(&self, f: Formatter<'_>)

Writes the defmt representation of self to fmt.
Source§

impl Hash for Timestamp

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
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 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

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

impl PartialEq for Timestamp

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialOrd 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 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Timestamp

Source§

impl Sub<(i64, i32)> for Timestamp

Subtracts a number of seconds and nanoseconds from a Timestamp.

§Panics

When adding would result in a value outside the boundaries of a Timestamp.

§Example

use jiff_core::Timestamp;

let ts = Timestamp::new(523, 999_999_999).unwrap();;
assert_eq!(ts - (400, 1), Timestamp::new(123, 999_999_998).unwrap());
Source§

type Output = Timestamp

The resulting type after applying the - operator.
Source§

fn sub(self, (seconds, nanoseconds): (i64, i32)) -> Timestamp

Performs the - operation. Read more
Source§

impl Sub<i64> for Timestamp

Subtracts a number of seconds from a Timestamp.

§Panics

When adding would result in a value outside the boundaries of a Timestamp.

§Example

use jiff_core::Timestamp;

let ts = Timestamp::new(523, 999_999_999).unwrap();;
assert_eq!(ts - 400, Timestamp::new(123, 999_999_999).unwrap());
Source§

type Output = Timestamp

The resulting type after applying the - operator.
Source§

fn sub(self, seconds: i64) -> Timestamp

Performs the - operation. Read more
Source§

impl SubAssign<(i64, i32)> for Timestamp

Subtracts a number of seconds and nanoseconds from a Timestamp.

§Panics

When subtracting would result in a value outside the boundaries of a Timestamp.

Source§

fn sub_assign(&mut self, rhs: (i64, i32))

Performs the -= operation. Read more
Source§

impl SubAssign<i64> for Timestamp

Subtracts a number of seconds from a Timestamp.

§Panics

When subtracting would result in a value outside the boundaries of a Timestamp.

Source§

fn sub_assign(&mut self, rhs: i64)

Performs the -= operation. Read more

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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, 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.