-
Notifications
You must be signed in to change notification settings - Fork 1k
use syscall.Gettimeofday to get current time #961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
45f2ba0
78c3206
8f9c71a
9d30bd4
19d4394
d44e936
805e262
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| //go:build !unix | ||
|
|
||
| package utils | ||
|
|
||
| import "time" | ||
|
|
||
| var Now = time.Now |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,19 @@ | ||||||
| //go:build unix | ||||||
|
|
||||||
| package utils | ||||||
|
|
||||||
| import ( | ||||||
| "syscall" | ||||||
| "time" | ||||||
| ) | ||||||
|
|
||||||
| // Now is a faster method to get current time | ||||||
| func Now() time.Time { | ||||||
| var tv syscall.Timeval | ||||||
| if err := syscall.Gettimeofday(&tv); nil != err { | ||||||
|
||||||
| if err := syscall.Gettimeofday(&tv); nil != err { | |
| if err := syscall.Gettimeofday(&tv); err != nil { |
nit. just a style preference. golang will not have if err = nil {} problems
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| //go:build unix | ||
|
|
||
| package utils | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestCustomTimeNow(t *testing.T) { | ||
| precision := time.Millisecond | ||
|
|
||
| for i := 0; i < 1000; i++ { | ||
| timestamp := time.Now().UnixNano() | ||
| customTimestamp := Now().UnixNano() | ||
|
|
||
| // two timestamp should within 1 percistion | ||
| assert.Equal(t, timestamp+int64(precision) >= customTimestamp && timestamp-int64(precision) <= customTimestamp, true, fmt.Sprintf("Loop: %d: customTimestamp should within %s. timestamp: %d, customTimestamp: %d", i, precision.String(), timestamp, customTimestamp)) | ||
|
||
|
|
||
| os.Setenv("TZ", fmt.Sprintf("UTC%d", 14-i%27)) | ||
|
||
| time.Sleep(time.Nanosecond) | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkGoTimeNow(t *testing.B) { | ||
| t.ResetTimer() | ||
| for n := 0; n < t.N; n++ { | ||
| _ = time.Now() | ||
| } | ||
| t.StopTimer() | ||
| } | ||
|
|
||
| func BenchmarkCustomTimeNow(t *testing.B) { | ||
| t.ResetTimer() | ||
| for n := 0; n < t.N; n++ { | ||
| _ = Now() | ||
| } | ||
| t.StopTimer() | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would probably call this
now_unix.goand the other onenow.goornow_generic.goor so.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea and I rename files name to now_unix.go and now.go