github.com/lingyao2333/mo-zero@v1.4.1/core/utils/times.go (about)

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