times.go 1.0 KB

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