mirror of
https://github.com/RGBCube/bonfire.v
synced 2025-07-28 14:37:44 +00:00
Add Snowflake, remove hello world
This commit is contained in:
parent
3382b29bb7
commit
193e7f66d1
3 changed files with 133 additions and 6 deletions
|
@ -1,6 +0,0 @@
|
|||
module bonfire
|
||||
|
||||
// hello_world prints "Hello, world!" to the console.
|
||||
pub fn hello_world() {
|
||||
println('Hello, world!')
|
||||
}
|
53
src/snowflake.v
Normal file
53
src/snowflake.v
Normal file
|
@ -0,0 +1,53 @@
|
|||
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)}>'
|
||||
}
|
||||
|
||||
// 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.
|
||||
pub type Snowflake = u64
|
||||
|
||||
// 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))
|
||||
}
|
||||
|
||||
// worker_id returns the ID of the worker that created the Snowflake.
|
||||
[inline]
|
||||
pub fn (s Snowflake) worker_id() u8 {
|
||||
return u8((s & 0x3E0000) >> 17)
|
||||
}
|
||||
|
||||
// process_id returns the ID of the process that created the Snowflake.
|
||||
[inline]
|
||||
pub fn (s Snowflake) process_id() u8 {
|
||||
return u8((s & 0x1F000) >> 12)
|
||||
}
|
||||
|
||||
// sequence_number returns the sequence number of the Snowflake (For every Snowflake that
|
||||
// is generated in the process, the sequence number is incremented by 1).
|
||||
[inline]
|
||||
pub fn (s Snowflake) sequence_number() u16 {
|
||||
return u16(s & 0xFFF)
|
||||
}
|
80
src/snowflake_test.v
Normal file
80
src/snowflake_test.v
Normal file
|
@ -0,0 +1,80 @@
|
|||
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
|
||||
assert snowflake_0.worker_id() == 0
|
||||
assert snowflake_0.process_id() == 0
|
||||
assert snowflake_0.sequence_number() == 10
|
||||
|
||||
snowflake_1 := Snowflake(254608322391572481)
|
||||
assert snowflake_1.created_at().unix == 1480773754
|
||||
assert snowflake_1.worker_id() == 1
|
||||
assert snowflake_1.process_id() == 0
|
||||
assert snowflake_1.sequence_number() == 1
|
||||
|
||||
snowflake_2 := Snowflake(170939974227591168)
|
||||
assert snowflake_2.created_at().unix == 1460825665
|
||||
assert snowflake_2.worker_id() == 0
|
||||
assert snowflake_2.process_id() == 0
|
||||
assert snowflake_2.sequence_number() == 0
|
||||
|
||||
snowflake_3 := Snowflake(107490111414882304)
|
||||
assert snowflake_3.created_at().unix == 1445698039
|
||||
assert snowflake_3.worker_id() == 0
|
||||
assert snowflake_3.process_id() == 5
|
||||
assert snowflake_3.sequence_number() == 0
|
||||
|
||||
snowflake_4 := Snowflake(616951104282034177)
|
||||
assert snowflake_4.created_at().unix == 1567163005
|
||||
assert snowflake_4.worker_id() == 2
|
||||
assert snowflake_4.process_id() == 0
|
||||
assert snowflake_4.sequence_number() == 1
|
||||
|
||||
snowflake_5 := Snowflake(582718866786877440)
|
||||
assert snowflake_5.created_at().unix == 1559001404
|
||||
assert snowflake_5.worker_id() == 0
|
||||
assert snowflake_5.process_id() == 0
|
||||
assert snowflake_5.sequence_number() == 0
|
||||
|
||||
snowflake_6 := Snowflake(150203841827045376)
|
||||
assert snowflake_6.created_at().unix == 1455881786
|
||||
assert snowflake_6.worker_id() == 0
|
||||
assert snowflake_6.process_id() == 0
|
||||
assert snowflake_6.sequence_number() == 0
|
||||
|
||||
snowflake_7 := Snowflake(111761808640978944)
|
||||
assert snowflake_7.created_at().unix == 1446716491
|
||||
assert snowflake_7.worker_id() == 0
|
||||
assert snowflake_7.process_id() == 3
|
||||
assert snowflake_7.sequence_number() == 0
|
||||
|
||||
snowflake_8 := Snowflake(705836698080247911)
|
||||
assert snowflake_8.created_at().unix == 1588354982
|
||||
assert snowflake_8.worker_id() == 1
|
||||
assert snowflake_8.process_id() == 0
|
||||
assert snowflake_8.sequence_number() == 103
|
||||
|
||||
snowflake_9 := Snowflake(735856983231561740)
|
||||
assert snowflake_9.created_at().unix == 1595512376
|
||||
assert snowflake_9.worker_id() == 1
|
||||
assert snowflake_9.process_id() == 0
|
||||
assert snowflake_9.sequence_number() == 12
|
||||
|
||||
snowflake_10 := Snowflake(785793169542479893)
|
||||
assert snowflake_10.created_at().unix == 1607418090
|
||||
assert snowflake_10.worker_id() == 1
|
||||
assert snowflake_10.process_id() == 0
|
||||
assert snowflake_10.sequence_number() == 21
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue