Browse Source

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>
NANI 3 years ago
parent
commit
9b1ba78bc6
2 changed files with 22 additions and 1 deletions
  1. 1 1
      util.go
  2. 21 0
      util_test.go

+ 1 - 1
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 - 0
util_test.go

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