Blame view
timer.go
1.19 KB
1c83dc241 GUID and timer |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
package webutility import ( "fmt" "time" ) // Timer ... type Timer struct { name string running bool started time.Time stopped time.Time lastDuration time.Duration } // NewTimer ... func NewTimer(name string) *Timer { t := &Timer{name: name} t.Reset() return t } // Start ... func (t *Timer) Start() time.Time { t.running = true t.started = time.Now() return t.started } // Stop ... func (t *Timer) Stop() time.Duration { t.running = false t.stopped = time.Now() t.lastDuration = t.stopped.Sub(t.started) return t.lastDuration } // LastRunDuration ... func (t *Timer) LastRunDuration() time.Duration { return t.lastDuration } // Clear ... func (t *Timer) Clear() { t.started = time.Now() t.stopped = time.Now() t.lastDuration = 0 } // Reset ... func (t *Timer) Reset() { t.Stop() t.Start() } // Elapsed ... func (t *Timer) Elapsed() time.Duration { if t.running { return time.Now().Sub(t.started) } return 0 } // Print ... func (t *Timer) Print(s string) { status := "RUNNING" if !t.running { status = "STOPPED" } fmt.Printf("timer[%s][%s]: %v %s ", t.name, status, t.Elapsed(), s) } // Lap ... func (t *Timer) Lap(s string) { t.Print(s) t.Reset() } |