2020-01-02 05:39:52 +00:00
|
|
|
package discord
|
|
|
|
|
|
|
|
import (
|
2020-01-04 04:19:24 +00:00
|
|
|
"bytes"
|
2020-01-02 05:39:52 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const DiscordEpoch = 1420070400000 * int64(time.Millisecond)
|
|
|
|
|
|
|
|
type Snowflake uint64
|
|
|
|
|
|
|
|
func NewSnowflake(t time.Time) Snowflake {
|
|
|
|
return Snowflake(TimeToDiscordEpoch(t) << 22)
|
|
|
|
}
|
|
|
|
|
2020-01-04 04:19:24 +00:00
|
|
|
func (s *Snowflake) UnmarshalJSON(v []byte) error {
|
|
|
|
v = bytes.Trim(v, `"`)
|
|
|
|
u, err := strconv.ParseUint(string(v), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*s = Snowflake(u)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Snowflake) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(`"` + strconv.FormatUint(uint64(*s), 10) + `"`), nil
|
|
|
|
}
|
|
|
|
|
2020-01-02 05:39:52 +00:00
|
|
|
func (s Snowflake) String() string {
|
|
|
|
return strconv.FormatUint(uint64(s), 10)
|
|
|
|
}
|
|
|
|
|
2020-01-15 04:43:34 +00:00
|
|
|
func (s Snowflake) Valid() bool {
|
|
|
|
return uint64(s) < 1
|
|
|
|
}
|
|
|
|
|
2020-01-02 05:39:52 +00:00
|
|
|
func (s Snowflake) Time() time.Time {
|
|
|
|
return time.Unix(0, int64(s)>>22*1000000+DiscordEpoch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Snowflake) Worker() uint8 {
|
|
|
|
return uint8(s & 0x3E0000)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Snowflake) PID() uint8 {
|
|
|
|
return uint8(s & 0x1F000 >> 12)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Snowflake) Increment() uint16 {
|
|
|
|
return uint16(s & 0xFFF)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TimeToDiscordEpoch(t time.Time) int64 {
|
|
|
|
return t.UnixNano()/int64(time.Millisecond) - DiscordEpoch
|
|
|
|
}
|