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
impl Timestamp
Sourcepub fn from_micros(unix_microseconds: i64) -> Result<Self, TimestampParseError>
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.
Sourcepub fn from_secs(unix_seconds: i64) -> Result<Self, TimestampParseError>
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.
Sourcepub fn parse(datetime: &str) -> Result<Self, TimestampParseError>
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.
Sourcepub const fn as_secs(self) -> i64
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());
Sourcepub const fn as_micros(self) -> i64
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());
Sourcepub const fn iso_8601(self) -> TimestampIso8601Display
pub const fn iso_8601(self) -> TimestampIso8601Display
Create a Display implementation to format the timestamp as an ISO 8601 datetime.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Timestamp
impl<'de> Deserialize<'de> for Timestamp
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
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
impl FromStr for Timestamp
Source§fn from_str(s: &str) -> Result<Self, Self::Err>
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.
Source§type Err = TimestampParseError
type Err = TimestampParseError
Source§impl TryFrom<&str> for Timestamp
impl TryFrom<&str> for Timestamp
Source§fn try_from(value: &str) -> Result<Self, Self::Error>
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.