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

Fix Snowflake and add examples for Time

This commit is contained in:
RGBCube 2022-12-05 18:23:10 +03:00
parent 34f330beb2
commit e1bd956256
2 changed files with 7 additions and 6 deletions

View file

@ -2,7 +2,7 @@ module bonfire
import time
const discord_epoch_ms = 1420070400000
const discord_epoch_milliseconds = u64(1420070400000)
// Snowflake is a Discord snowflake ID. It holds the time the
// object was created, the ID of the worker that created it,
@ -10,9 +10,9 @@ const discord_epoch_ms = 1420070400000
pub type Snowflake = u64
// created_at returns the Time the Snowflake was created at.
[inline]
pub fn (s Snowflake) created_at() Time {
seconds, milliseconds := separate_seconds_and_milliseconds(i64((s >> 22) + .discord_epoch_ms))
seconds, milliseconds := separate_seconds_and_milliseconds(u64(s >> 22) +
.discord_epoch_milliseconds)
return time.unix2(seconds, milliseconds * 1000)
}
@ -38,10 +38,10 @@ pub fn (s Snowflake) sequence_number() u16 {
// 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
return ((unix * 1000) - .discord_epoch_milliseconds) << 22
}
[inline]
fn separate_seconds_and_milliseconds(ms u64) (i64, int) {
return i64(ms / 1000), ms % 1000
fn separate_seconds_and_milliseconds(milliseconds u64) (i64, int) {
return i64(milliseconds / 1000), int(milliseconds % 1000)
}

View file

@ -17,6 +17,7 @@ pub enum TimeFormat as u8 {
}
// format returns a Discord representation of the time in the given format.
// E.g. <t:1670178420:f> for 4 December 2022 21:27.
[inline]
pub fn (t Time) format(format TimeFormat) string {
return '<t:${t.unix}:${rune(format)}>'