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:
NANI 2020-06-17 05:43:34 +02:00 committed by GitHub
parent bfbd4bc5c3
commit 9b1ba78bc6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View file

@ -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
View 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)
}
}