Skip to content

Commit 27b6699

Browse files
committed
crtime: add (Mono).add and ClockReading.
This may make it a bit easier to adopt Mono in libraries without forcing us to immediately update all callers, by making the library generic over crtime.ClockReading, which is implemented by both time.Time and crtime.Mono.
1 parent 2a49e18 commit 27b6699

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

crtime/monotonic.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ import (
2727
// used.
2828
type Mono time.Duration
2929

30+
type ClockReading[T any] interface {
31+
// Add returns a ClockReading time.Duration in the future from a given
32+
// ClockReading.
33+
Add(time.Duration) T
34+
// Sub returns the duration elapsed between two clock readings.
35+
Sub(T) time.Duration
36+
}
37+
3038
// NowMono returns a moment in time in terms of a monotonic clock. It is faster
3139
// than time.Now which also consults the wall clock.
3240
func NowMono() Mono {
@@ -39,6 +47,11 @@ func (m Mono) Sub(other Mono) time.Duration {
3947
return time.Duration(m - other)
4048
}
4149

50+
// Add returns the time.Mono m+d.
51+
func (m Mono) Add(d time.Duration) Mono {
52+
return Mono(time.Duration(m) + d)
53+
}
54+
4255
// Elapsed returns the duration that elapsed since m.
4356
func (m Mono) Elapsed() time.Duration {
4457
return time.Duration(NowMono() - m)

0 commit comments

Comments
 (0)