-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
34 lines (28 loc) · 913 Bytes
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package dgoflake
import (
"strconv"
"golang.org/x/xerrors"
)
// ParseInt parses an int to a snowflake.
func ParseInt(id uint64) *Snowflake {
return &Snowflake{id}
}
// ParseString parses a string to a snowflake.
func ParseString(id string) (*Snowflake, error) {
i, err := strconv.ParseUint(id, 10, 64)
if err != nil {
return nil, xerrors.Errorf("dgoflake: failed to parse string to snowflake: %w", err)
}
return &Snowflake{i}, nil
}
// ParseEpochMilli parses an epoch milliseconds timestamp to a snowflake.
func ParseEpochMilli(epoch uint64) (*Snowflake, error) {
if epoch < DiscordEpoch {
return nil, xerrors.New("dgoflake: epoch cannot be earlier than the Discord epoch")
}
return &Snowflake{(epoch - DiscordEpoch) << 22}, nil
}
// ParseEpochSec parses an epoch seconds timestamp to a snowflake.
func ParseEpochSec(epoch uint64) (*Snowflake, error) {
return ParseEpochMilli(epoch * 1000)
}