-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tai64.go
138 lines (125 loc) · 4.35 KB
/
tai64.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
131
132
133
134
135
136
137
138
// TAI64 CALENDAR
/*
Supports times that are seconds since CE 1970-01-01 00:00:00 TAI (plus 2**62,
when in hexadecimal), as defined at https://cr.yp.to/libtai/tai64.html (though
this library includes extensions to the formats described there). These values
are also used internally, so this calendar system can be used to directly expose
the underlying internal values in a manner that allows them to be used
elsewhere.
Supported Input Types:
- string
- []byte
- TAI64NAXURTime
- math/big.Float for Offset
Supported Format Strings:
- decimal - decimal; full resolution
- tai64 - hexadecimal; just seconds
- tai64n - hexadecimal; with nanoseconds
- tai64na - hexadecimal; with attoseconds
- tai64nax - hexadecimal; with xictoseconds
- tai64naxu - hexadecimal; with uctoseconds
- tai64naxur - hexadecimal; with roctoseconds
*/
package calendars
import (
"fmt"
"math/big"
"strings"
)
func init() {
RegisterElements(
// name
"TAI64",
// toInternal
func(date interface{}, format string) (stamp TAI64NAXURTime, err error) {
var dateString string
switch date.(type) {
// TODO - other types
case []byte:
dateString = string(date.([]byte))
case string:
dateString = date.(string)
case TAI64NAXURTime:
stamp = date.(TAI64NAXURTime)
return
default:
err = ErrUnsupportedInput
return
}
switch format {
case "decimal":
tmp := strings.Split(dateString, ".")
if len(tmp) < 2 {
tmp = append(tmp, "0")
}
_, err = fmt.Sscanf(fmt.Sprintf("%s %-045s", tmp[0], tmp[1]), "%d %09d%09d%09d%09d%09d", &stamp.Seconds, &stamp.Nano, &stamp.Atto, &stamp.Xicto, &stamp.Ucto, &stamp.Rocto)
case "tai64naxur":
_, err = fmt.Sscanf(dateString, "%016X%08X%08X%08X%08X%08X", &stamp.Seconds, &stamp.Nano, &stamp.Atto, &stamp.Xicto, &stamp.Ucto, &stamp.Rocto)
case "tai64naxu":
_, err = fmt.Sscanf(dateString, "%016X%08X%08X%08X%08X", &stamp.Seconds, &stamp.Nano, &stamp.Atto, &stamp.Xicto, &stamp.Ucto)
case "tai64nax":
_, err = fmt.Sscanf(dateString, "%016X%08X%08X%08X", &stamp.Seconds, &stamp.Nano, &stamp.Atto, &stamp.Xicto)
case "tai64na":
_, err = fmt.Sscanf(dateString, "%016X%08X%08X", &stamp.Seconds, &stamp.Nano, &stamp.Atto)
case "tai64n":
_, err = fmt.Sscanf(dateString, "%016X%08X", &stamp.Seconds, &stamp.Nano)
case "tai64":
_, err = fmt.Sscanf(dateString, "%016X", &stamp.Seconds)
default:
err = ErrInvalidFormat
}
if err != nil && err.Error() == "EOF" {
err = nil
}
if format != "decimal" && err == nil {
stamp.Seconds -= 0x4000000000000000
}
return
},
// fromInternal
func(stamp TAI64NAXURTime, format string) (date string, err error) {
switch format {
case "decimal":
date = strings.TrimRight(strings.TrimRight(fmt.Sprintf("%0d.%09d%09d%09d%09d%09d", stamp.Seconds, stamp.Nano, stamp.Atto, stamp.Xicto, stamp.Ucto, stamp.Rocto), "0"), ".")
case "tai64naxur":
date = fmt.Sprintf("%016X%08X%08X%08X%08X%08X", stamp.Seconds+0x4000000000000000, stamp.Nano, stamp.Atto, stamp.Xicto, stamp.Ucto, stamp.Rocto)
case "tai64naxu":
date = fmt.Sprintf("%016X%08X%08X%08X%08X", stamp.Seconds+0x4000000000000000, stamp.Nano, stamp.Atto, stamp.Xicto, stamp.Ucto)
case "tai64nax":
date = fmt.Sprintf("%016X%08X%08X%08X", stamp.Seconds+0x4000000000000000, stamp.Nano, stamp.Atto, stamp.Xicto)
case "tai64na":
date = fmt.Sprintf("%016X%08X%08X", stamp.Seconds+0x4000000000000000, stamp.Nano, stamp.Atto)
case "tai64n":
date = fmt.Sprintf("%016X%08X", stamp.Seconds+0x4000000000000000, stamp.Nano)
case "tai64":
date = fmt.Sprintf("%016X", stamp.Seconds+0x4000000000000000)
default:
err = ErrInvalidFormat
}
return
},
// offset
func(in TAI64NAXURTime, offset interface{}) (out TAI64NAXURTime, err error) {
var adjust TAI64NAXURTime
switch offset.(type) {
// TODO - other types
case big.Float:
adjust = TAI64NAXURTimeFromFloat(offset.(big.Float))
case *big.Float:
adjust = TAI64NAXURTimeFromFloat(*offset.(*big.Float))
case []byte:
adjust = TAI64NAXURTimeFromDecimalString(string(offset.([]byte)))
case string:
adjust = TAI64NAXURTimeFromDecimalString(offset.(string))
case TAI64NAXURTime:
adjust = offset.(TAI64NAXURTime)
default:
err = ErrUnsupportedInput
}
out = in.Add(adjust)
return
},
// defaultFormat
"tai64naxur",
)
}