go-zero/core/utils/times.go

46 lines
1.0 KiB
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package utils
import (
"fmt"
"time"
"github.com/zeromicro/go-zero/core/timex"
2020-07-26 17:09:05 +08:00
)
// A ElapsedTimer is a timer to track the elapsed time.
2020-07-26 17:09:05 +08:00
type ElapsedTimer struct {
start time.Duration
}
// NewElapsedTimer returns a ElapsedTimer.
2020-07-26 17:09:05 +08:00
func NewElapsedTimer() *ElapsedTimer {
return &ElapsedTimer{
start: timex.Now(),
}
}
// Duration returns the elapsed time.
2020-07-26 17:09:05 +08:00
func (et *ElapsedTimer) Duration() time.Duration {
return timex.Since(et.start)
}
// Elapsed returns the string representation of elapsed time.
2020-07-26 17:09:05 +08:00
func (et *ElapsedTimer) Elapsed() string {
return timex.Since(et.start).String()
}
// ElapsedMs returns the elapsed time of string on milliseconds.
2020-07-26 17:09:05 +08:00
func (et *ElapsedTimer) ElapsedMs() string {
return fmt.Sprintf("%.1fms", float32(timex.Since(et.start))/float32(time.Millisecond))
}
// CurrentMicros returns the current microseconds.
2020-07-26 17:09:05 +08:00
func CurrentMicros() int64 {
return time.Now().UnixNano() / int64(time.Microsecond)
}
// CurrentMillis returns the current milliseconds.
2020-07-26 17:09:05 +08:00
func CurrentMillis() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}