From 937cb414e989ba9ca6b3103479ff3ddf1863bc99 Mon Sep 17 00:00:00 2001 From: Godruoyi Date: Fri, 26 Nov 2021 13:54:11 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=92=20fix=20timezone=20to=20utc=20and?= =?UTF-8?q?=20perfect=20example=20demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/example.go | 7 ++++++- snowflake.go | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/example/example.go b/example/example.go index 16d4eb9..6e8adfe 100644 --- a/example/example.go +++ b/example/example.go @@ -2,13 +2,18 @@ package main import ( "fmt" + "time" "github.com/godruoyi/go-snowflake" ) func main() { + // set starttime and machineID for the first time if you wan't to use the default value + snowflake.SetStartTime(time.Date(2021, 9, 1, 0, 0, 0, 0, time.UTC)) + snowflake.SetMachineID(snowflake.PrivateIPToMachineID()) // testing, not to be used in production + id := snowflake.ID() - fmt.Println(id) + fmt.Println(id) // 1537200202186752 sid := snowflake.ParseID(id) // SID { diff --git a/snowflake.go b/snowflake.go index cab2d38..9b5eb19 100644 --- a/snowflake.go +++ b/snowflake.go @@ -84,7 +84,7 @@ func SetStartTime(s time.Time) { panic("The start time cannot be a zero value") } - if s.After(time.Now()) { + if s.After(time.Now().UTC()) { panic("The s cannot be greater than the current millisecond") }