Add a method to encode the snowflake as a byte slice

This commit is contained in:
Connor Peet
2016-07-26 11:35:39 -07:00
parent cd8e42350a
commit 95d961a628
2 changed files with 22 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ package snowflake
import (
"encoding/base64"
"encoding/binary"
"errors"
"strconv"
"sync"
@@ -105,11 +106,19 @@ func (f ID) Base64() string {
return base64.StdEncoding.EncodeToString(f.Bytes())
}
// Bytes returns a byte array of the snowflake ID
// Bytes returns a byte slice of the snowflake ID
func (f ID) Bytes() []byte {
return []byte(f.String())
}
// IntBytes returns an array of bytes of the snowflake ID, encoded as a
// big endian integer.
func (f ID) IntBytes() [8]byte {
var b [8]byte
binary.BigEndian.PutUint64(b[:], uint64(f))
return b
}
// Time returns an int64 unix timestamp of the snowflake ID time
func (f ID) Time() int64 {
return (int64(f) >> 22) + Epoch