1
Fork 0
mirror of https://github.com/RGBCube/bonfire.v synced 2025-07-28 14:37:44 +00:00

Simplify Snowflake.created_at logic

This commit is contained in:
RGBCube 2022-12-04 22:59:34 +03:00
parent d0cdf2553e
commit e908c460aa

View file

@ -4,11 +4,6 @@ import time
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
// object was created, the ID of the worker that created it,
// 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.
[inline]
pub fn (s Snowflake) created_at() Time {
created_ms := i64((s >> 22) + discord_epoch_ms)
microseconds := (created_ms % 1000) * 1000
return time.unix2(created_ms / 1000, microseconds)
seconds, milliseconds := v(i64((s >> 22) + .discord_epoch_ms))
return time.unix2(seconds, milliseconds * 1000)
}
// 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 {
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
}