-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
0internal.go
130 lines (106 loc) · 4.03 KB
/
0internal.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package calendars
import (
"encoding/hex"
"math"
"math/big"
)
// TAI64NAXURTime stores a TAI64NAXUR instant in a reliable, easy-converted
// format.
type TAI64NAXURTime struct {
Seconds int64 // Seconds since 1970-01-01 00:00:00 TAI
Nano uint32 // Nanoseconds since the given second
Atto uint32 // Attoseconds since the given nanosecond
Xicto uint32 // Xictoseconds since the given attosecond
Ucto uint32 // Uctoseconds since the given xictosecond
Rocto uint32 // Roctoseconds since the given uctosecond
}
// Add calculates the sum of two TAI64NAXURTime values.
func (t TAI64NAXURTime) Add(z TAI64NAXURTime) TAI64NAXURTime {
var o TAI64NAXURTime
var roll int32
roll, o.Rocto = rollOverAt9(int32(t.Rocto + z.Rocto))
roll, o.Ucto = rollOverAt9(int32(t.Ucto+z.Ucto) + roll)
roll, o.Xicto = rollOverAt9(int32(t.Xicto+z.Xicto) + roll)
roll, o.Atto = rollOverAt9(int32(t.Atto+z.Atto) + roll)
roll, o.Nano = rollOverAt9(int32(t.Nano+z.Nano) + roll)
o.Seconds = t.Seconds + z.Seconds + int64(roll)
return o
}
// Sub calculates the difference of two TAI64NAXURTime values.
func (t TAI64NAXURTime) Sub(z TAI64NAXURTime) TAI64NAXURTime {
var o TAI64NAXURTime
var roll int32
roll, o.Rocto = rollOverAt9(int32(t.Rocto) - int32(z.Rocto))
roll, o.Ucto = rollOverAt9(int32(t.Ucto) - int32(z.Ucto) - roll)
roll, o.Xicto = rollOverAt9(int32(t.Xicto) - int32(z.Xicto) - roll)
roll, o.Atto = rollOverAt9(int32(t.Atto) - int32(z.Atto) - roll)
roll, o.Nano = rollOverAt9(int32(t.Nano) - int32(z.Nano) - roll)
o.Seconds = t.Seconds - z.Seconds - int64(roll)
return o
}
// String returns the decimal string representation of the TAI64NAXURTime value.
func (t TAI64NAXURTime) String() string {
out, _ := FromInternal("tai64", t, "decimal")
return out
}
// HexString returns the hex string representation of the TAI64NAXURTime value.
func (t TAI64NAXURTime) HexString() string {
out, _ := FromInternal("tai64", t, "tai64naxur")
return out
}
// Float returns the math/big.Float representation of the TAI64NAXURTime value.
func (t TAI64NAXURTime) Float() *big.Float {
out, _, _ := big.ParseFloat(t.String(), 10, 176, big.ToNearestAway)
return out
}
// MarshalText implements the encoding.TextMarshaler interface.
func (t TAI64NAXURTime) MarshalText() ([]byte, error) {
out, err := FromInternal("tai64", t, "tai64naxur")
return []byte(out), err
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (t *TAI64NAXURTime) UnmarshalText(in []byte) error {
tmp, err := ToInternal("tai64", in, "tai64naxur")
*t = tmp
return err
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (t *TAI64NAXURTime) MarshalBinary() (out []byte, err error) {
in, err := t.MarshalText()
if err != nil {
return
}
out, err = hex.DecodeString(string(in))
return
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (t *TAI64NAXURTime) UnmarshalBinary(in []byte) error {
out := hex.EncodeToString(in)
return t.UnmarshalText([]byte(out))
}
// TAI64NAXURTimeFromDecimalString calculates a TAI64NAXURTime from its decimal
// string representation.
func TAI64NAXURTimeFromDecimalString(in string) TAI64NAXURTime {
out, _ := ToInternal("tai64", in, "decimal")
// fmt.Printf("TAI64NAXURTimeFromDecimalString: %#v → %#v [%#v]\n", in, out, err)
return out
}
// TAI64NAXURTimeFromHexString calculates a TAI64NAXURTime from its hexadecimal
// string representation.
func TAI64NAXURTimeFromHexString(in string) TAI64NAXURTime {
out, _ := ToInternal("tai64", in, "tai64naxur")
// fmt.Printf("TAI64NAXURTimeFromHexString: %#v → %#v [%#v]\n", in, out, err)
return out
}
// TAI64NAXURTimeFromFloat calculates a TAI64NAXURTime from its math/big.Float
// representation.
func TAI64NAXURTimeFromFloat(in big.Float) TAI64NAXURTime {
// fmt.Printf("TAI64NAXURTimeFromFloat: %#v\n", in)
return TAI64NAXURTimeFromDecimalString(in.Text('f', 45))
}
func rollOverAt9(value int32) (roll int32, remain uint32) {
working := math.Abs(float64(value))
roll = int32(working) / 1000000000
remain = uint32(math.Mod(working, 1000000000))
return
}