@@ -9,18 +9,25 @@ import (
99var epoch = time .Now ()
1010
1111// FromDuration converts a time.Duration into units of 1/hz.
12- func FromDuration (d time.Duration , hz uint32 ) uint64 {
13- return uint64 (d ) * uint64 (hz ) / uint64 (time .Second )
12+ func FromDuration (d time.Duration , hz uint32 ) int64 {
13+ return int64 (d ) * int64 (hz ) / int64 (time .Second )
1414}
1515
1616// ToDuration converts units of 1/hz into a time.Duration.
17- func ToDuration (tm uint64 , hz uint32 ) time.Duration {
18- return time .Duration (tm * uint64 (time .Second ) / uint64 (hz ))
17+ func ToDuration (tm int64 , hz uint32 ) time.Duration {
18+ return time .Duration (tm * int64 (time .Second ) / int64 (hz ))
19+ }
20+
21+ func sat (a int64 ) uint64 {
22+ if a < 0 {
23+ return 0
24+ }
25+ return uint64 (a )
1926}
2027
2128// Now returns the current time in units of 1/hz from an arbitrary origin.
2229func Now (hz uint32 ) uint64 {
23- return FromDuration (time .Since (epoch ), hz )
30+ return sat ( FromDuration (time .Since (epoch ), hz ) )
2431}
2532
2633// Microseconds is like Now, but uses microseconds.
@@ -39,7 +46,7 @@ func Jiffies() uint64 {
3946
4047// TimeToJiffies converts a time.Time into jiffies.
4148func TimeToJiffies (tm time.Time ) uint64 {
42- return FromDuration (tm .Sub (epoch ), JiffiesPerSec )
49+ return sat ( FromDuration (tm .Sub (epoch ), JiffiesPerSec ) )
4350}
4451
4552// The origin of NTP time.
0 commit comments