-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstant.go
67 lines (57 loc) · 1.3 KB
/
instant.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package chrono
import "strconv"
// Instant represents an instantaneous point in time with nanosecond resolution.
type Instant struct {
v *int64
}
// Now returns the Instant that represents the current point in time.
func Now() Instant {
now := monotime()
return Instant{
v: &now,
}
}
// Compare compares i with i2. If i is before i2, it returns -1;
// if i is after i2, it returns 1; if they're the same, it returns 0.
func (i Instant) Compare(i2 Instant) int {
switch {
case i.v == nil:
panic("i is not initialized")
case i2.v == nil:
panic("i2 is not initialized")
case *i.v < *i2.v:
return -1
case *i.v > *i2.v:
return 1
default:
return 0
}
}
// Elapsed is shorthand for i.Until(chrono.Now()).
func (i Instant) Elapsed() Duration {
return i.Until(Now())
}
func (i Instant) String() string {
return strconv.FormatInt(*i.v, 10)
}
// Until returns the Duration that represents the elapsed time from i to v.
func (i Instant) Until(i2 Instant) Duration {
switch {
case i.v == nil:
panic("i is not initialized")
case i2.v == nil:
panic("i2 is not initialized")
}
iv, vv := *i.v, *i2.v
if vv < iv {
panic("v is smaller than i")
}
d := vv - iv
return DurationOf(Extent(d))
}
// instant is used by the chronotest package.
func instant(t int64) Instant {
return Instant{
v: &t,
}
}