mirror of
https://github.com/RGBCube/bonfire.v
synced 2025-07-29 06:57:44 +00:00
Simplify Snowflake.created_at logic
This commit is contained in:
parent
d0cdf2553e
commit
e908c460aa
1 changed files with 13 additions and 8 deletions
|
@ -4,11 +4,6 @@ import time
|
||||||
|
|
||||||
const discord_epoch_ms = 1420070400000
|
const discord_epoch_ms = 1420070400000
|
||||||
|
|
||||||
// new_snowflake returns a snowflake ID from the given unix timestamp (in seconds).
|
|
||||||
fn new_snowflake(unix u64) Snowflake {
|
|
||||||
return ((unix*1000 - discord_epoch_ms)) << 22
|
|
||||||
}
|
|
||||||
|
|
||||||
// Snowflake is a Discord snowflake ID. It holds the time the
|
// Snowflake is a Discord snowflake ID. It holds the time the
|
||||||
// object was created, the ID of the worker that created it,
|
// object was created, the ID of the worker that created it,
|
||||||
// the ID of the process that created it, and a sequence number.
|
// the ID of the process that created it, and a sequence number.
|
||||||
|
@ -17,9 +12,8 @@ pub type Snowflake = u64
|
||||||
// created_at returns the Time the Snowflake was created at.
|
// created_at returns the Time the Snowflake was created at.
|
||||||
[inline]
|
[inline]
|
||||||
pub fn (s Snowflake) created_at() Time {
|
pub fn (s Snowflake) created_at() Time {
|
||||||
created_ms := i64((s >> 22) + discord_epoch_ms)
|
seconds, milliseconds := v(i64((s >> 22) + .discord_epoch_ms))
|
||||||
microseconds := (created_ms % 1000) * 1000
|
return time.unix2(seconds, milliseconds * 1000)
|
||||||
return time.unix2(created_ms / 1000, microseconds)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// worker_id returns the ID of the worker that created the Snowflake.
|
// worker_id returns the ID of the worker that created the Snowflake.
|
||||||
|
@ -40,3 +34,14 @@ pub fn (s Snowflake) process_id() u8 {
|
||||||
pub fn (s Snowflake) sequence_number() u16 {
|
pub fn (s Snowflake) sequence_number() u16 {
|
||||||
return u16(s & 0xFFF)
|
return u16(s & 0xFFF)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// new_snowflake returns a snowflake ID from the given unix timestamp (in seconds).
|
||||||
|
[inline]
|
||||||
|
fn new_snowflake(unix u64) Snowflake {
|
||||||
|
return (unix * 1000 - .discord_epoch_ms) << 22
|
||||||
|
}
|
||||||
|
|
||||||
|
[inline]
|
||||||
|
fn separate_seconds_and_milliseconds(ms u64) (i64, int) {
|
||||||
|
return i64(ms / 1000), ms % 1000
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue