2020-01-02 05:39:52 +00:00
|
|
|
package discord
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
2020-01-17 22:29:13 +00:00
|
|
|
"strings"
|
2020-01-02 05:39:52 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-05-08 20:52:46 +00:00
|
|
|
// DiscordEpoch is the Discord epoch constant in time.Duration (nanoseconds)
|
|
|
|
// since Unix epoch.
|
|
|
|
const DiscordEpoch = 1420070400000 * time.Millisecond
|
|
|
|
|
|
|
|
// DurationSinceDiscordEpoch returns the duration from the Discord epoch to
|
|
|
|
// current.
|
|
|
|
func DurationSinceDiscordEpoch(t time.Time) time.Duration {
|
|
|
|
return time.Duration(t.UnixNano()) - DiscordEpoch
|
|
|
|
}
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-01-21 04:30:13 +00:00
|
|
|
type Snowflake int64
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-05-08 20:52:46 +00:00
|
|
|
// NullSnowflake gets encoded into a null. This is used for
|
|
|
|
// optional and nullable snowflake fields.
|
|
|
|
const NullSnowflake Snowflake = -1
|
|
|
|
|
2020-01-02 05:39:52 +00:00
|
|
|
func NewSnowflake(t time.Time) Snowflake {
|
2020-05-08 20:52:46 +00:00
|
|
|
return Snowflake((DurationSinceDiscordEpoch(t) / time.Millisecond) << 22)
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 07:24:15 +00:00
|
|
|
func ParseSnowflake(sf string) (Snowflake, error) {
|
|
|
|
i, err := strconv.ParseInt(sf, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return Snowflake(i), nil
|
|
|
|
}
|
|
|
|
|
2020-01-04 04:19:24 +00:00
|
|
|
func (s *Snowflake) UnmarshalJSON(v []byte) error {
|
2020-01-17 22:29:13 +00:00
|
|
|
id := strings.Trim(string(v), `"`)
|
|
|
|
if id == "null" {
|
2020-05-08 20:56:00 +00:00
|
|
|
// Use -1 for null.
|
|
|
|
*s = -1
|
2020-01-17 22:29:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
i, err := strconv.ParseInt(id, 10, 64)
|
2020-01-04 04:19:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-17 22:29:13 +00:00
|
|
|
*s = Snowflake(i)
|
2020-01-04 04:19:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 20:52:46 +00:00
|
|
|
func (s Snowflake) MarshalJSON() ([]byte, error) {
|
|
|
|
if s < 1 {
|
2020-01-19 06:06:00 +00:00
|
|
|
return []byte("null"), nil
|
2020-05-08 20:52:46 +00:00
|
|
|
} else {
|
|
|
|
return []byte(`"` + strconv.FormatInt(int64(s), 10) + `"`), nil
|
2020-01-17 22:29:13 +00:00
|
|
|
}
|
2020-01-04 04:19:24 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2020-01-15 04:46:47 +00:00
|
|
|
return uint64(s) > 0
|
2020-01-15 04:43:34 +00:00
|
|
|
}
|
|
|
|
|
2020-01-02 05:39:52 +00:00
|
|
|
func (s Snowflake) Time() time.Time {
|
2020-05-08 20:52:46 +00:00
|
|
|
unixnano := ((time.Duration(s) >> 22) * time.Millisecond) + DiscordEpoch
|
|
|
|
return time.Unix(0, int64(unixnano))
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s Snowflake) Worker() uint8 {
|
2020-05-08 21:11:56 +00:00
|
|
|
return uint8(s & 0x3E0000 >> 17)
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s Snowflake) PID() uint8 {
|
|
|
|
return uint8(s & 0x1F000 >> 12)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Snowflake) Increment() uint16 {
|
|
|
|
return uint16(s & 0xFFF)
|
|
|
|
}
|