forked from ZebraDevs/rosgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
temporal.go
62 lines (50 loc) · 1.2 KB
/
temporal.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
package rosgo
const maxUint32 = int64(^uint32(0))
func normalizeTemporal(sec int64, nsec int64) (uint32, uint32) {
const SecondInNanosecond = 1000000000
if nsec > SecondInNanosecond {
sec += nsec / SecondInNanosecond
nsec = nsec % SecondInNanosecond
} else if nsec < 0 {
sec += nsec/SecondInNanosecond - 1
nsec = nsec%SecondInNanosecond + SecondInNanosecond
}
if sec < 0 || sec > maxUint32 {
panic("Time is out of range")
}
return uint32(sec), uint32(nsec)
}
func cmpUint64(lhs, rhs uint64) int {
var result int
if lhs > rhs {
result = 1
} else if lhs < rhs {
result = -1
} else {
result = 0
}
return result
}
type temporal struct {
Sec uint32
NSec uint32
}
func (t *temporal) IsZero() bool {
return t.Sec == 0 && t.NSec == 0
}
func (t *temporal) ToSec() float64 {
return float64(t.Sec) + float64(t.NSec)*1e-9
}
func (t *temporal) ToNSec() uint64 {
return uint64(t.Sec)*1000000000 + uint64(t.NSec)
}
func (t *temporal) FromSec(sec float64) {
nsec := uint64(sec * 1e9)
t.FromNSec(nsec)
}
func (t *temporal) FromNSec(nsec uint64) {
t.Sec, t.NSec = normalizeTemporal(0, int64(nsec))
}
func (t *temporal) Normalize() {
t.Sec, t.NSec = normalizeTemporal(int64(t.Sec), int64(t.NSec))
}