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

Seperate time from snowflake, add internal new_snowflake func, and make Snowflake.created_at() milisecond sensitive

This commit is contained in:
RGBCube 2022-12-04 22:21:44 +03:00
parent 05a7aa8702
commit d0cdf2553e
4 changed files with 45 additions and 30 deletions

View file

@ -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 '<t:${t.unix}:${rune(format)}>'
// 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.

View file

@ -1,16 +1,5 @@
module bonfire
fn test_time_format() {
time := Snowflake(512640455834337290).created_at()
assert time.format(.short_time) == '<t:1542293409:t>'
assert time.format(.long_time) == '<t:1542293409:T>'
assert time.format(.short_date) == '<t:1542293409:d>'
assert time.format(.long_date) == '<t:1542293409:D>'
assert time.format(.short_date_time) == '<t:1542293409:f>'
assert time.format(.long_date_time) == '<t:1542293409:F>'
assert time.format(.relative) == '<t:1542293409:R>'
}
fn test_snowflake() {
snowflake_0 := Snowflake(512640455834337290)
assert snowflake_0.created_at().unix == 1542293409

23
src/time.v Normal file
View file

@ -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 '<t:${t.unix}:${rune(format)}>'
}

14
src/time_test.v Normal file
View file

@ -0,0 +1,14 @@
module bonfire
import time
fn test_time_format() {
time := Time(time.unix(512640455834337290))
assert time.format(.short_time) == '<t:1542293409:t>'
assert time.format(.long_time) == '<t:1542293409:T>'
assert time.format(.short_date) == '<t:1542293409:d>'
assert time.format(.long_date) == '<t:1542293409:D>'
assert time.format(.short_date_time) == '<t:1542293409:f>'
assert time.format(.long_date_time) == '<t:1542293409:F>'
assert time.format(.relative) == '<t:1542293409:R>'
}