diff --git a/src/snowflake.v b/src/snowflake.v index af4be36..73c25da 100644 --- a/src/snowflake.v +++ b/src/snowflake.v @@ -2,24 +2,11 @@ module bonfire import time -// Time is a time.Time that can be formatted as a Discord timestamp. -pub type Time = time.Time +const discord_epoch_ms = 1420070400000 -// TimeFormat is an enum of all possible Discord timestamp formats. -pub enum TimeFormat as u8 { - short_time = u8(`t`) // 21:27 - long_time = u8(`T`) // 21:27:42 - short_date = u8(`d`) // 4/12/2022 - long_date = u8(`D`) // 4 December 2022 - short_date_time = u8(`f`) // 4 December 2022 21:27 - long_date_time = u8(`F`) // Sunday, 4 December 2022 21:27 - relative = u8(`R`) // 2 days ago, Now, etc. -} - -// format returns a Discord representation of the time in the given format. -[inline] -pub fn (t Time) format(format TimeFormat) string { - return '' +// 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 @@ -27,10 +14,12 @@ pub fn (t Time) format(format TimeFormat) string { // the ID of the process that created it, and a sequence number. 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] pub fn (s Snowflake) created_at() Time { - return time.unix(i64(((s >> 22) + 1420070400000) / 1000)) + created_ms := i64((s >> 22) + discord_epoch_ms) + microseconds := (created_ms % 1000) * 1000 + return time.unix2(created_ms / 1000, microseconds) } // worker_id returns the ID of the worker that created the Snowflake. diff --git a/src/snowflake_test.v b/src/snowflake_test.v index 2ac80d3..3421cf0 100644 --- a/src/snowflake_test.v +++ b/src/snowflake_test.v @@ -1,16 +1,5 @@ module bonfire -fn test_time_format() { - time := Snowflake(512640455834337290).created_at() - assert time.format(.short_time) == '' - assert time.format(.long_time) == '' - assert time.format(.short_date) == '' - assert time.format(.long_date) == '' - assert time.format(.short_date_time) == '' - assert time.format(.long_date_time) == '' - assert time.format(.relative) == '' -} - fn test_snowflake() { snowflake_0 := Snowflake(512640455834337290) assert snowflake_0.created_at().unix == 1542293409 diff --git a/src/time.v b/src/time.v new file mode 100644 index 0000000..8802380 --- /dev/null +++ b/src/time.v @@ -0,0 +1,23 @@ +module bonfire + +import time + +// Time is a time.Time that can be formatted as a Discord timestamp. +pub type Time = time.Time + +// TimeFormat is an enum of all possible Discord timestamp formats. +pub enum TimeFormat as u8 { + short_time = u8(`t`) // 21:27 + long_time = u8(`T`) // 21:27:42 + short_date = u8(`d`) // 4/12/2022 + long_date = u8(`D`) // 4 December 2022 + short_date_time = u8(`f`) // 4 December 2022 21:27 + long_date_time = u8(`F`) // Sunday, 4 December 2022 21:27 + relative = u8(`R`) // 2 days ago, Now, etc. +} + +// format returns a Discord representation of the time in the given format. +[inline] +pub fn (t Time) format(format TimeFormat) string { + return '' +} diff --git a/src/time_test.v b/src/time_test.v new file mode 100644 index 0000000..18bce01 --- /dev/null +++ b/src/time_test.v @@ -0,0 +1,14 @@ +module bonfire + +import time + +fn test_time_format() { + time := Time(time.unix(512640455834337290)) + assert time.format(.short_time) == '' + assert time.format(.long_time) == '' + assert time.format(.short_date) == '' + assert time.format(.long_date) == '' + assert time.format(.short_date_time) == '' + assert time.format(.long_date_time) == '' + assert time.format(.relative) == '' +}