Timer.go

package util

import (
	"fmt"
	"time"
)

type Timer struct {
	startTime int64
	markTime  int64
}

func NewTimer(autostart bool) *Timer {
	t := &Timer{}
	if autostart {
		t.Start()
	}
	return t
}

func (t *Timer) Start() int64 {
	t.startTime = currentMicroseconds()
	t.markTime = t.startTime
	return t.startTime
}

func (t *Timer) Check() int64 {
	last := currentMicroseconds()
	interval := last - t.markTime
	t.markTime = last
	return interval
}

func (t *Timer) Log() {
	fmt.Println(t.Check())
}

func (t *Timer) CheckedInterval() int64 {
	return t.markTime - t.startTime
}

func (t *Timer) LastInterval() int64 {
	return currentMicroseconds() - t.markTime
}

func (t *Timer) FullInterval() int64 {
	return currentMicroseconds() - t.startTime
}

func currentMicroseconds() int64 {
	return time.Now().UnixNano() / int64(time.Microsecond)
}