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

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