github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/time/monotonic/time.go (about) 1 /* For license and copyright information please see the LEGAL file in the code repository */ 2 3 package monotonic 4 5 import ( 6 "github.com/GeniusesGroup/libgo/protocol" 7 ) 8 9 // Now returns runtime monotonic clock in nanoseconds. 10 func Now() Time { 11 return Time(now()) 12 } 13 14 // A Time monotonic clock is for measuring time. 15 // time-measuring operations, specifically comparisons and subtractions, use the monotonic clock. 16 type Time int64 17 18 //libgo:impl protocol.Time 19 func (t *Time) Epoch() protocol.TimeEpoch { return protocol.TimeEpoch_Monotonic } 20 func (t *Time) SecondElapsed() int64 { return int64(*t) / int64(Second) } 21 func (t *Time) NanoSecondElapsed() int32 { return int32(int64(*t) % t.SecondElapsed()) } 22 func (t *Time) ToString() string { 23 // TODO::: 24 return "" 25 } 26 27 func (t *Time) Now() { *t = Now() } 28 func (t *Time) Add(d protocol.Duration) { *t += Time(d) } 29 30 // Equal reports whether t and other represent the same time instant. 31 func (t Time) Equal(other Time) bool { return t == other } 32 33 // Pass reports whether the time instant t is after from. 34 func (t Time) Pass(from Time) bool { return t > from } 35 36 // PassNow reports whether the time instant t is after now. 37 func (t Time) PassNow() bool { return t > Now() } 38 39 // Since returns the time elapsed since t. 40 func (t Time) Since(from Time) (d protocol.Duration) { return protocol.Duration(from - t) } 41 42 // SinceNow returns the time elapsed since now. 43 func (t Time) SinceNow() (d protocol.Duration) { return protocol.Duration(Now() - t) } 44 45 // Until returns the duration until to. 46 func (t Time) Until(to Time) (d protocol.Duration) { return protocol.Duration(t - to) } 47 48 // UntilNow returns the duration until now. 49 func (t Time) UntilNow() (d protocol.Duration) { return protocol.Duration(t - Now()) }