mirror of
https://github.com/bwmarrin/discordgo.git
synced 2025-12-23 23:17:45 -05:00
Adding millisecond precision to SnowflakeTimestamp utility (#732)
* Millisecond precision * formatting * Update util.go * remove unnecessary int64 * Add unit test for SnowflakeTimestamp Co-authored-by: NANI <nani@ksoft.si> Co-authored-by: Carson Hoffman <c@rsonhoffman.com>
This commit is contained in:
2
util.go
2
util.go
@@ -12,6 +12,6 @@ func SnowflakeTimestamp(ID string) (t time.Time, err error) {
|
||||
return
|
||||
}
|
||||
timestamp := (i >> 22) + 1420070400000
|
||||
t = time.Unix(timestamp/1000, 0)
|
||||
t = time.Unix(0, timestamp*1000000)
|
||||
return
|
||||
}
|
||||
|
||||
21
util_test.go
Normal file
21
util_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package discordgo
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSnowflakeTimestamp(t *testing.T) {
|
||||
// #discordgo channel ID :)
|
||||
id := "155361364909621248"
|
||||
parsedTimestamp, err := SnowflakeTimestamp(id)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("returned error incorrect: got %v, want nil", err)
|
||||
}
|
||||
|
||||
correctTimestamp := time.Date(2016, time.March, 4, 17, 10, 35, 869*1000000, time.UTC)
|
||||
if !parsedTimestamp.Equal(correctTimestamp) {
|
||||
t.Errorf("parsed time incorrect: got %v, want %v", parsedTimestamp, correctTimestamp)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user