pub trait Snowflake {
// Required method
fn id(&self) -> u64;
// Provided methods
fn timestamp(&self) -> i64 { ... }
fn worker_id(&self) -> u8 { ... }
fn process_id(&self) -> u8 { ... }
fn increment(&self) -> u16 { ... }
}
Available on crate feature
snowflake
only.Expand description
Snowflake is a trait for defining extractable information from a Snowflake. A Snowflake is a u64 generated by Discord to uniquely identify a resource.
Required Methods§
Provided Methods§
Sourcefn timestamp(&self) -> i64
fn timestamp(&self) -> i64
The Unix epoch of the Snowflake in milliseconds, indicating when it was generated.
Derived from bits 22..63 of the id.
§Examples
See when a user was created using chrono
:
use chrono::{TimeZone, Utc};
use twilight_model::id::{marker::UserMarker, Id};
use twilight_util::snowflake::Snowflake;
let id = Id::<UserMarker>::new(105484726235607040);
assert_eq!(
"2015-10-19T01:58:38.546+00:00",
Utc.timestamp_millis(id.timestamp()).to_rfc3339()
);
See when a user was created using time
:
use time::{format_description::well_known::Rfc3339, Duration, OffsetDateTime};
use twilight_model::id::{marker::UserMarker, Id};
use twilight_util::snowflake::Snowflake;
let id = Id::<UserMarker>::new(105484726235607040);
// Convert milliseconds to seconds or nanoseconds.
let dur = Duration::milliseconds(id.timestamp());
let ts = OffsetDateTime::from_unix_timestamp(dur.whole_seconds())?;
let ts_milli = OffsetDateTime::from_unix_timestamp_nanos(dur.whole_nanoseconds())?;
assert_eq!("2015-10-19T01:58:38Z", ts.format(&Rfc3339)?);
assert_eq!("2015-10-19T01:58:38.546Z", ts_milli.format(&Rfc3339)?);
Sourcefn worker_id(&self) -> u8
fn worker_id(&self) -> u8
The id of the internal worker that generated the Snowflake.
Derived from bits 17..21 of the id.
Sourcefn process_id(&self) -> u8
fn process_id(&self) -> u8
The id of the internal process that generated the Snowflake.
Derived from bits 12..16 of the id.