Skip to content

Commit 9b1ba78

Browse files
AndyTempelNANICarsonHoffman
authored
Adding millisecond precision to SnowflakeTimestamp utility (bwmarrin#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>
1 parent bfbd4bc commit 9b1ba78

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ func SnowflakeTimestamp(ID string) (t time.Time, err error) {
1212
return
1313
}
1414
timestamp := (i >> 22) + 1420070400000
15-
t = time.Unix(timestamp/1000, 0)
15+
t = time.Unix(0, timestamp*1000000)
1616
return
1717
}

util_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package discordgo
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func TestSnowflakeTimestamp(t *testing.T) {
9+
// #discordgo channel ID :)
10+
id := "155361364909621248"
11+
parsedTimestamp, err := SnowflakeTimestamp(id)
12+
13+
if err != nil {
14+
t.Errorf("returned error incorrect: got %v, want nil", err)
15+
}
16+
17+
correctTimestamp := time.Date(2016, time.March, 4, 17, 10, 35, 869*1000000, time.UTC)
18+
if !parsedTimestamp.Equal(correctTimestamp) {
19+
t.Errorf("parsed time incorrect: got %v, want %v", parsedTimestamp, correctTimestamp)
20+
}
21+
}

0 commit comments

Comments
 (0)