Skip to content

Commit

Permalink
chore: remove unnecessary time convert
Browse files Browse the repository at this point in the history
  • Loading branch information
qshuai committed Jan 18, 2025
1 parent f4c2c59 commit 5137739
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
13 changes: 6 additions & 7 deletions util/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ import (
)

const (
TimeFormat = "2006-01-02 15:04:05"
DateFormat = "2006-01-02"
UnixTimeUnitOffset = uint64(time.Millisecond / time.Nanosecond)
TimeFormat = "2006-01-02 15:04:05"
DateFormat = "2006-01-02"
)

var (
Expand Down Expand Up @@ -84,7 +83,7 @@ func (t *RealClock) CurrentTimeMillis() uint64 {
if tickerNow > uint64(0) {
return tickerNow
}
return uint64(time.Now().UnixNano()) / UnixTimeUnitOffset
return uint64(time.Now().UnixMilli())
}

func (t *RealClock) CurrentTimeNano() uint64 {
Expand Down Expand Up @@ -120,7 +119,7 @@ func (t *MockClock) Sleep(d time.Duration) {
}

func (t *MockClock) CurrentTimeMillis() uint64 {
return uint64(t.Now().UnixNano()) / UnixTimeUnitOffset
return uint64(t.Now().UnixMilli())
}

func (t *MockClock) CurrentTimeNano() uint64 {
Expand Down Expand Up @@ -272,12 +271,12 @@ func NewTicker(d time.Duration) Ticker {

// FormatTimeMillis formats Unix timestamp (ms) to time string.
func FormatTimeMillis(tsMillis uint64) string {
return time.Unix(0, int64(tsMillis*UnixTimeUnitOffset)).Format(TimeFormat)
return time.Unix(0, int64(tsMillis)*int64(time.Millisecond)).Format(TimeFormat)
}

// FormatDate formats Unix timestamp (ms) to date string
func FormatDate(tsMillis uint64) string {
return time.Unix(0, int64(tsMillis*UnixTimeUnitOffset)).Format(DateFormat)
return time.Unix(0, int64(tsMillis)*int64(time.Millisecond)).Format(DateFormat)
}

// Returns the current Unix timestamp in milliseconds.
Expand Down
2 changes: 1 addition & 1 deletion util/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func BenchmarkCurrentTimeInMs(b *testing.B) {
if tickerNow > uint64(0) {
return tickerNow
}
return uint64(time.Now().UnixNano()) / UnixTimeUnitOffset
return uint64(time.Now().UnixMilli())
}

b.ReportAllocs()
Expand Down
4 changes: 2 additions & 2 deletions util/time_ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ var nowInMs = uint64(0)
// StartTimeTicker starts a background task that caches current timestamp per millisecond,
// which may provide better performance in high-concurrency scenarios.
func StartTimeTicker() {
atomic.StoreUint64(&nowInMs, uint64(time.Now().UnixNano())/UnixTimeUnitOffset)
atomic.StoreUint64(&nowInMs, uint64(time.Now().UnixMilli()))
go func() {
for {
now := uint64(time.Now().UnixNano()) / UnixTimeUnitOffset
now := uint64(time.Now().UnixMilli())
atomic.StoreUint64(&nowInMs, now)
time.Sleep(time.Millisecond)
}
Expand Down

0 comments on commit 5137739

Please sign in to comment.