You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/utils/times.go

46 lines
1.0 KiB
Go

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)
}