mirror of
https://github.com/RGBCube/bonfire.v
synced 2025-07-28 22:47:45 +00:00
Fix Snowflake and add examples for Time
This commit is contained in:
parent
34f330beb2
commit
e1bd956256
2 changed files with 7 additions and 6 deletions
|
@ -2,7 +2,7 @@ module bonfire
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
const discord_epoch_ms = 1420070400000
|
const discord_epoch_milliseconds = u64(1420070400000)
|
||||||
|
|
||||||
// Snowflake is a Discord snowflake ID. It holds the time the
|
// Snowflake is a Discord snowflake ID. It holds the time the
|
||||||
// object was created, the ID of the worker that created it,
|
// object was created, the ID of the worker that created it,
|
||||||
|
@ -10,9 +10,9 @@ const discord_epoch_ms = 1420070400000
|
||||||
pub type Snowflake = u64
|
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 {
|
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)
|
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).
|
// new_snowflake returns a snowflake ID from the given unix timestamp (in seconds).
|
||||||
[inline]
|
[inline]
|
||||||
fn new_snowflake(unix u64) Snowflake {
|
fn new_snowflake(unix u64) Snowflake {
|
||||||
return (unix * 1000 - .discord_epoch_ms) << 22
|
return ((unix * 1000) - .discord_epoch_milliseconds) << 22
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
[inline]
|
||||||
fn separate_seconds_and_milliseconds(ms u64) (i64, int) {
|
fn separate_seconds_and_milliseconds(milliseconds u64) (i64, int) {
|
||||||
return i64(ms / 1000), ms % 1000
|
return i64(milliseconds / 1000), int(milliseconds % 1000)
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ pub enum TimeFormat as u8 {
|
||||||
}
|
}
|
||||||
|
|
||||||
// format returns a Discord representation of the time in the given format.
|
// format returns a Discord representation of the time in the given format.
|
||||||
|
// E.g. <t:1670178420:f> for 4 December 2022 21:27.
|
||||||
[inline]
|
[inline]
|
||||||
pub fn (t Time) format(format TimeFormat) string {
|
pub fn (t Time) format(format TimeFormat) string {
|
||||||
return '<t:${t.unix}:${rune(format)}>'
|
return '<t:${t.unix}:${rune(format)}>'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue