Files
gl01/internal/util/clock.go
2025-12-19 13:48:48 +08:00

30 lines
572 B
Go

package util
import "time"
// Clock is a clock to measure elapsed time.
// It is a shorthand for the time.Since() and time.Now() combo.
type Clock struct {
t time.Time
}
// NewClock returns a new clock.
// It also starts it.
func NewClock() (c *Clock) {
return &Clock{t: time.Now()}
}
// Restart resets the start time.
// It also returns the elapsed time.
func (c *Clock) Restart() (t time.Duration) {
now := time.Now()
t = now.Sub(c.t)
c.t = now
return
}
// Elapsed returns the elapsed time.
func (c *Clock) Elapsed() time.Duration {
return time.Since(c.t)
}