Timestamp

Struct Timestamp 

Source
#[non_exhaustive]
pub struct Timestamp { /* private fields */ }
Expand description

Well-known point in time representation for Google APIs.

§Examples

let ts = Timestamp::try_from("2025-05-16T09:46:12.500Z")?;
assert_eq!(ts.seconds(), 1747388772);
assert_eq!(ts.nanos(), 500_000_000);

assert_eq!(ts, Timestamp::new(1747388772, 500_000_000)?);
assert_eq!(ts, Timestamp::clamp(1747388772, 500_000_000));

A Timestamp represents a point in time independent of any time zone or local calendar, encoded as a count of seconds and fractions of seconds at nanosecond resolution. The count is relative to an epoch at UTC midnight on January 1, 1970, in the proleptic Gregorian calendar which extends the Gregorian calendar backwards to year one.

All minutes are 60 seconds long. Leap seconds are “smeared” so that no leap second table is needed for interpretation, using a 24-hour linear smear.

The range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. By restricting to that range, we ensure that we can convert to and from RFC 3339 date strings.

§JSON Mapping

In JSON format, the Timestamp type is encoded as a string in the RFC 3339 format. That is, the format is “{year}-{month}-{day}T{hour}:{min}:{sec}[.{frac_sec}]Z” where {year} is always expressed using four digits while {month}, {day}, {hour}, {min}, and {sec} are zero-padded to two digits each. The fractional seconds, which can go up to 9 digits (i.e. up to 1 nanosecond resolution), are optional. The “Z” suffix indicates the timezone (“UTC”); the timezone is required.

For example, “2017-01-15T01:30:15.01Z” encodes 15.01 seconds past 01:30 UTC on January 15, 2017.

Implementations§

Source§

impl Timestamp

Source

pub const MIN_SECONDS: i64 = -62_135_596_800i64

The minimum value for the seconds component. Corresponds to ‘0001-01-01T00:00:00Z’.

Source

pub const MAX_SECONDS: i64 = 253_402_300_799i64

The maximum value for the seconds component. Corresponds to ‘9999-12-31T23:59:59Z’.

Source

pub const MIN_NANOS: i32 = 0i32

The minimum value for the nanos component.

Source

pub const MAX_NANOS: i32 = 999_999_999i32

The maximum value for the nanos component.

Source

pub fn new(seconds: i64, nanos: i32) -> Result<Self, TimestampError>

Creates a new Timestamp from the seconds and nanoseconds.

If either value is out of range it returns an error.

§Examples
let ts = Timestamp::new(1747388772, 0)?;
assert_eq!(String::from(ts), "2025-05-16T09:46:12Z");

let ts = Timestamp::new(1747388772, 2_000_000_000);
assert!(matches!(ts, Err(TimestampError::OutOfRange)));
§Parameters
  • seconds - the seconds on the timestamp.
  • nanos - the nanoseconds on the timestamp.
Source

pub fn clamp(seconds: i64, nanos: i32) -> Self

Create a normalized, clamped Timestamp.

§Examples
let ts = Timestamp::clamp(1747388772, 0);
assert_eq!(String::from(ts), "2025-05-16T09:46:12Z");

let ts = Timestamp::clamp(1747388772, 2_000_000_000);
// extra nanoseconds are carried as seconds
assert_eq!(String::from(ts), "2025-05-16T09:46:14Z");

Timestamps must be between 0001-01-01T00:00:00Z and 9999-12-31T23:59:59.999999999Z, and the nanoseconds component must always be in the range [0, 999_999_999]. This function creates a new Timestamp instance clamped to those ranges.

The function effectively adds the nanoseconds part (with carry) to the seconds part, with saturation.

§Parameters
  • seconds - the seconds on the timestamp.
  • nanos - the nanoseconds added to the seconds.
Source

pub fn seconds(&self) -> i64

Represents seconds of UTC time since Unix epoch (1970-01-01T00:00:00Z).

Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive.

§Examples
let ts = Timestamp::new(120, 500_000_000)?;
assert_eq!(ts.seconds(), 120);
Source

pub fn nanos(&self) -> i32

Non-negative fractions of a second at nanosecond resolution.

Negative second values (before the Unix epoch) with fractions must still have non-negative nanos values that count forward in time. Must be from 0 to 999,999,999 inclusive.

§Examples
let ts = Timestamp::new(120, 500_000_000)?;
assert_eq!(ts.nanos(), 500_000_000);

Trait Implementations§

Source§

impl Clone for Timestamp

Source§

fn clone(&self) -> Timestamp

Returns a duplicate 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 From<Timestamp> for String

Converts a Timestamp to its String representation.

§Example

let ts = Timestamp::new(1747388772, 0)?;
assert_eq!(String::from(ts), "2025-05-16T09:46:12Z");
Source§

fn from(timestamp: Timestamp) -> Self

Converts to this type from the input type.
Source§

impl Message for Timestamp

Source§

fn typename() -> &'static str

The typename of this message.
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 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 · 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 · 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 · 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 · 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 TryFrom<&String> for Timestamp

Converts the string representation of a timestamp to Timestamp.

§Example

let s = "2025-05-16T09:46:12.500Z".to_string();
let ts = Timestamp::try_from(&s)?;
assert_eq!(ts.seconds(), 1747388772);
assert_eq!(ts.nanos(), 500_000_000);
Source§

type Error = TimestampError

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

fn try_from(value: &String) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&str> for Timestamp

Converts the string representation of a timestamp to Timestamp.

§Example

let ts = Timestamp::try_from("2025-05-16T09:46:12.500Z")?;
assert_eq!(ts.seconds(), 1747388772);
assert_eq!(ts.nanos(), 500_000_000);
Source§

type Error = TimestampError

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

fn try_from(value: &str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<DateTime<Utc>> for Timestamp

Available on crate feature chrono only.

Converts from chrono::DateTime to Timestamp.

This conversion may fail if the chrono::DateTime value is out of range.

§Example

use chrono::{DateTime, TimeZone, Utc};
let date : DateTime<Utc> = Utc.with_ymd_and_hms(2025, 5, 16, 10, 15, 00).unwrap();
let ts = Timestamp::try_from(date)?;
assert_eq!(String::from(ts), "2025-05-16T10:15:00Z");
Source§

type Error = TimestampError

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

fn try_from(value: DateTime<Utc>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<OffsetDateTime> for Timestamp

Available on crate feature time only.

This conversion may fail if the time::OffsetDateTime value is out of range.

§Examples

use time::{macros::datetime, OffsetDateTime};
let dt = datetime!(2025-05-16 09:46:12 UTC);
let ts = Timestamp::try_from(dt)?;
assert_eq!(String::from(ts), "2025-05-16T09:46:12Z");
Source§

type Error = TimestampError

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

fn try_from(value: OffsetDateTime) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<SystemTime> for Timestamp

Converts from std::time::SystemTime to Timestamp.

This conversion may fail if the std::time::SystemTime value is out of range.

§Example

let ts = Timestamp::try_from(SystemTime::now())?;
println!("now={ts:?}");
Source§

type Error = TimestampError

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

fn try_from(value: SystemTime) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Timestamp> for DateTime<Utc>

Available on crate feature chrono only.

Converts from Timestamp to chrono::DateTime.

§Example

use chrono::{DateTime, TimeZone, Utc};
let ts = Timestamp::try_from("2025-05-16T10:15:00Z")?;
let date = DateTime::try_from(ts)?;
Source§

type Error = TimestampError

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

fn try_from(value: Timestamp) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Timestamp> for OffsetDateTime

Available on crate feature time only.

Convert from Timestamp to OffsetDateTime

This conversion may fail if the Timestamp value is out of range.

§Examples

use time::{macros::datetime, OffsetDateTime};
let ts = Timestamp::try_from("2025-05-16T09:46:12Z")?;
let dt = OffsetDateTime::try_from(ts)?;
assert_eq!(dt, datetime!(2025-05-16 09:46:12 UTC));
Source§

type Error = ComponentRange

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

fn try_from(value: Timestamp) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Timestamp> for SystemTime

Converts from Timestamp to std::time::SystemTime.

This conversion may fail if the std::time::SystemTime value is out of range.

§Example

let ts = Timestamp::new(0, 0)?;
let epoch = SystemTime::try_from(ts)?;
assert_eq!(epoch, SystemTime::UNIX_EPOCH);
Source§

type Error = TimestampError

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

fn try_from(value: Timestamp) -> Result<SystemTime, Self::Error>

Performs the conversion.
Source§

impl Copy 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, 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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,