From e908c460aa80116a31dd568a07e6531b8ad9b56c Mon Sep 17 00:00:00 2001 From: RGBCube Date: Sun, 4 Dec 2022 22:59:34 +0300 Subject: [PATCH] Simplify Snowflake.created_at logic --- src/snowflake.v | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/snowflake.v b/src/snowflake.v index 73c25da..5021034 100644 --- a/src/snowflake.v +++ b/src/snowflake.v @@ -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 +}