-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnowflake.go
47 lines (36 loc) · 794 Bytes
/
snowflake.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
35
36
37
38
39
40
41
42
43
44
45
46
47
package snowflake
import (
"strconv"
)
type Snowflake uint64
type SnowflakeStruct struct {
timestamp uint64
machineID uint
sequence uint
}
func (s Snowflake) String() string {
return strconv.FormatUint(s.Number(), 10)
}
func (s Snowflake) Number() uint64 {
return uint64(s)
}
func (s Snowflake) Bytes() []byte {
return []byte(s.String())
}
func (s Snowflake) Timestamp() uint {
return uint(s.Number() >> 22)
}
func (s Snowflake) MachineID() uint {
return uint((s.Number() & 0x3FF0000) >> 12)
}
func (s Snowflake) Sequence() uint {
return uint(s.Number() & 0xFFF)
}
func (s *Snowflake) Parse() *SnowflakeStruct {
sNum := s.Number()
return &SnowflakeStruct{
timestamp: sNum >> 22,
machineID: uint((s.Number() & 0x3FF0000) >> 12),
sequence: uint(sNum & 0xFFF),
}
}