pub struct Timestamp(/* private fields */);
Expand description

Representation of a Unix timestamp.

§Display

The timestamp does not itself implement core::fmt::Display. It could have two possible display implementations: that of the Unix timestamp or that of the timestamp in ISO 8601 format. Therefore, the preferred implementation may be chosen by explicitly retrieving the Unix timestamp with seconds precision, with microseconds precision, or retrieving an ISO 8601 formatter.

Implementations§

source§

impl Timestamp

source

pub fn from_micros(unix_microseconds: i64) -> Result<Self, TimestampParseError>

Create a timestamp from a Unix timestamp with microseconds precision.

§Errors

Returns a TimestampParseErrorType::Parsing error type if the parsing failed.

source

pub fn from_secs(unix_seconds: i64) -> Result<Self, TimestampParseError>

Create a timestamp from a Unix timestamp with seconds precision.

§Errors

Returns a TimestampParseErrorType::Parsing error type if the parsing failed.

source

pub fn parse(datetime: &str) -> Result<Self, TimestampParseError>

Parse a timestamp from an ISO 8601 datetime string emitted by Discord.

Discord emits two ISO 8601 valid formats of datetimes: with microseconds (2021-01-01T01:01:01.010000+00:00) and without microseconds (2021-01-01T01:01:01+00:00). This supports parsing from either.

Supports parsing dates between the Discord epoch year (2010) and 2038.

§Examples
use std::str::FromStr;
use twilight_model::util::Timestamp;

// Date and time in UTC with +00:00 offsets are supported:
assert!(Timestamp::parse("2021-01-01T01:01:01.010000+00:00").is_ok());
assert!(Timestamp::parse("2021-01-01T01:01:01+00:00").is_ok());

// Other formats, such as dates, weeks, zero UTC offset designators, or
// ordinal dates are not supported:
assert!(Timestamp::parse("2021-08-10T18:19:59Z").is_err());
assert!(Timestamp::parse("2021-01-01").is_err());
assert!(Timestamp::parse("2021-W32-2").is_err());
assert!(Timestamp::parse("2021-222").is_err());
§Errors

Returns a TimestampParseErrorType::Format error type if the provided string is too short to be an ISO 8601 datetime without a time offset.

Returns a TimestampParseErrorType::Parsing error type if the parsing failed.

source

pub const fn as_secs(self) -> i64

Total number of seconds within the timestamp.

§Examples

Parse a formatted timestamp and then get its Unix timestamp value with seconds precision:

use std::str::FromStr;
use twilight_model::util::Timestamp;

let timestamp = Timestamp::from_str("2021-08-10T11:16:37.020000+00:00")?;
assert_eq!(1_628_594_197, timestamp.as_secs());
source

pub const fn as_micros(self) -> i64

Total number of microseconds within the timestamp.

§Examples

Parse a formatted timestamp and then get its Unix timestamp value with microseconds precision:

use std::str::FromStr;
use twilight_model::util::Timestamp;

let timestamp = Timestamp::from_str("2021-08-10T11:16:37.123456+00:00")?;
assert_eq!(1_628_594_197_123_456, timestamp.as_micros());
source

pub const fn iso_8601(self) -> TimestampIso8601Display

Create a Display implementation to format the timestamp as an ISO 8601 datetime.

Trait Implementations§

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<'de> Deserialize<'de> for Timestamp

source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Parse a timestamp from an ISO 8601 datetime string emitted by Discord.

Discord emits two ISO 8601 valid formats of datetimes: with microseconds (2021-01-01T01:01:01.010000+00:00) and without microseconds (2021-01-01T01:01:01+00:00). This supports parsing from either.

§Errors

Refer to the documentation for Timestamp::parse for a list of errors.

source§

impl FromStr for Timestamp

source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parse a timestamp from an ISO 8601 datetime string emitted by Discord.

Discord emits two ISO 8601 valid formats of datetimes: with microseconds (2021-01-01T01:01:01.010000+00:00) and without microseconds (2021-01-01T01:01:01+00:00). This supports parsing from either.

Supports parsing dates between the Discord epoch year (2010) and 2038.

§Examples

Refer to the documentation for Timestamp::parse for more examples.

use std::str::FromStr;
use twilight_model::util::Timestamp;

assert!(Timestamp::from_str("2021-01-01T01:01:01.010000+00:00").is_ok());
assert!(Timestamp::from_str("2021-01-01T01:01:01+00:00").is_ok());
§Errors

Refer to the documentation for Timestamp::parse for a list of errors.

§

type Err = TimestampParseError

The associated error which can be returned from parsing.
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 PartialEq 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 Serialize for Timestamp

source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
source§

impl TryFrom<&str> for Timestamp

source§

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

Parse a timestamp from an ISO 8601 datetime string emitted by Discord.

Discord emits two ISO 8601 valid formats of datetimes: with microseconds (2021-01-01T01:01:01.010000+00:00) and without microseconds (2021-01-01T01:01:01+00:00). This supports parsing from either.

§Examples

Refer to the documentation for Timestamp::parse for examples.

§Errors

Refer to the documentation for Timestamp::parse for a list of errors.

§

type Error = TimestampParseError

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

impl Copy for Timestamp

source§

impl Eq 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> 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,

§

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>,

§

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>,

§

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>,