-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
times.go
45 lines (36 loc) · 1.05 KB
/
times.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package utils
import (
"fmt"
"time"
"github.com/zeromicro/go-zero/core/timex"
)
// A ElapsedTimer is a timer to track the elapsed time.
type ElapsedTimer struct {
start time.Duration
}
// NewElapsedTimer returns a ElapsedTimer.
func NewElapsedTimer() *ElapsedTimer {
return &ElapsedTimer{
start: timex.Now(),
}
}
// Duration returns the elapsed time.
func (et *ElapsedTimer) Duration() time.Duration {
return timex.Since(et.start)
}
// Elapsed returns the string representation of elapsed time.
func (et *ElapsedTimer) Elapsed() string {
return timex.Since(et.start).String()
}
// ElapsedMs returns the elapsed time of string on milliseconds.
func (et *ElapsedTimer) ElapsedMs() string {
return fmt.Sprintf("%.1fms", float32(timex.Since(et.start))/float32(time.Millisecond))
}
// CurrentMicros returns the current microseconds.
func CurrentMicros() int64 {
return time.Now().UnixNano() / int64(time.Microsecond)
}
// CurrentMillis returns the current milliseconds.
func CurrentMillis() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}