2016-01-19 13:39:46 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2015-2016 Christopher Young
|
|
|
|
Distributable under the terms of The "BSD New"" License
|
|
|
|
that can be found in the LICENSE file, herein included
|
|
|
|
as part of this header.
|
|
|
|
|
2016-01-27 05:27:09 +00:00
|
|
|
Modifications (c) 2016 AvSquirrel (https://github.com/AvSquirrel)
|
2016-01-19 13:39:46 +00:00
|
|
|
monotonic.go: Create monotonic clock using time.Timer - necessary because of real time clock changes on RPi.
|
|
|
|
*/
|
|
|
|
|
2016-01-07 16:08:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-01-07 16:54:38 +00:00
|
|
|
humanize "github.com/dustin/go-humanize"
|
2016-01-07 16:08:54 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Timer (since start).
|
|
|
|
|
|
|
|
type monotonic struct {
|
2016-01-27 05:27:09 +00:00
|
|
|
Milliseconds uint64
|
|
|
|
Time time.Time
|
|
|
|
ticker *time.Ticker
|
2016-01-07 16:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *monotonic) Watcher() {
|
|
|
|
for {
|
|
|
|
<-m.ticker.C
|
2016-04-07 22:34:01 +00:00
|
|
|
m.Milliseconds += 10
|
|
|
|
m.Time = m.Time.Add(10 * time.Millisecond)
|
2016-01-07 16:08:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 16:20:06 +00:00
|
|
|
func (m *monotonic) Since(t time.Time) time.Duration {
|
|
|
|
return m.Time.Sub(t)
|
|
|
|
}
|
|
|
|
|
2016-01-07 16:54:38 +00:00
|
|
|
func (m *monotonic) HumanizeTime(t time.Time) string {
|
2016-01-27 05:27:09 +00:00
|
|
|
return humanize.RelTime(t, m.Time, "ago", "from now")
|
2016-01-07 16:54:38 +00:00
|
|
|
}
|
|
|
|
|
2016-02-18 00:33:13 +00:00
|
|
|
func (m *monotonic) Unix() int64 {
|
|
|
|
return int64(m.Since(time.Time{}).Seconds())
|
|
|
|
}
|
|
|
|
|
2016-01-07 16:08:54 +00:00
|
|
|
func NewMonotonic() *monotonic {
|
2016-04-07 22:34:01 +00:00
|
|
|
t := &monotonic{Milliseconds: 0, Time: time.Time{}, ticker: time.NewTicker(10 * time.Millisecond)}
|
2016-01-07 16:08:54 +00:00
|
|
|
go t.Watcher()
|
|
|
|
return t
|
|
|
|
}
|