| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package tai64 |
| 6 |
|
| 7 |
import ( |
| 8 |
"sort" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
) |
| 12 |
|
| 13 |
var tests = map[string]struct { |
| 14 |
t string |
| 15 |
o string |
| 16 |
}{ |
| 17 |
"a while ago": {"1920-01-01T00:00:00Z", "@3fffffffa1f2cd8a00000000"}, |
| 18 |
"before tai swap": {"1969-12-31T23:59:49Z", "@3fffffffffffffff00000000"}, |
| 19 |
"at tai swap": {"1969-12-31T23:59:50Z", "@400000000000000000000000"}, |
| 20 |
"after tai swap": {"1969-12-31T23:59:51Z", "@400000000000000100000000"}, |
| 21 |
"before unix epoch": {"1969-12-31T23:59:59Z", "@400000000000000900000000"}, |
| 22 |
"at unix epoch": {"1970-01-01T00:00:00Z", "@400000000000000a00000000"}, |
| 23 |
"after unix epoch": {"1970-01-01T00:00:01Z", "@400000000000000b00000000"}, |
| 24 |
"before tai-utc epoch": {"1970-01-01T00:00:09Z", "@400000000000001300000000"}, |
| 25 |
"at tai-utc epoch": {"1970-01-01T00:00:10Z", "@400000000000001400000000"}, |
| 26 |
"after tai-utc epoch": {"1970-01-01T00:00:11Z", "@400000000000001500000000"}, |
| 27 |
"right before adjust 1": {"1972-06-30T23:59:59Z", "@4000000004b2580900000000"}, |
| 28 |
"right at adjust 1": {"1972-07-01T00:00:00Z", "@4000000004b2580b00000000"}, |
| 29 |
"right after adjust 1": {"1972-07-01T00:00:01Z", "@4000000004b2580c00000000"}, |
| 30 |
"right before adjust 2": {"2016-12-31T23:59:59Z", "@40000000586846a300000000"}, |
| 31 |
"right at adjust 2": {"2017-01-01T00:00:00Z", "@40000000586846a500000000"}, |
| 32 |
"right after adjust 2": {"2017-01-01T00:00:01Z", "@40000000586846a600000000"}, |
| 33 |
"nanoseconds": {"2015-06-30T23:59:59.908626131Z", "@4000000055932da2362888d3"}, |
| 34 |
} |
| 35 |
|
| 36 |
func getOrderedTestNames() []string { |
| 37 |
var keys []string |
| 38 |
for k := range tests { |
| 39 |
keys = append(keys, k) |
| 40 |
} |
| 41 |
sort.Strings(keys) |
| 42 |
return keys |
| 43 |
} |
| 44 |
|
| 45 |
func TestRoundTripNano(t *testing.T) { |
| 46 |
for _, name := range getOrderedTestNames() { |
| 47 |
tt := tests[name] |
| 48 |
tm, err := time.Parse(time.RFC3339Nano, tt.t) |
| 49 |
if err != nil { |
| 50 |
t.Fatalf("%s: test failed parsing time.Time", name) |
| 51 |
} |
| 52 |
|
| 53 |
o := FormatNano(tm) |
| 54 |
p, err := Parse(o) |
| 55 |
if err != nil { |
| 56 |
t.Fatalf("%s: test failed parsing", name) |
| 57 |
} |
| 58 |
|
| 59 |
if tm != p { |
| 60 |
t.Fatalf("%s: test failed date compare:\n %s != %s", name, tm, p) |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func TestRoundTrip(t *testing.T) { |
| 66 |
for _, name := range getOrderedTestNames() { |
| 67 |
tt := tests[name] |
| 68 |
tm, err := time.Parse(time.RFC3339Nano, tt.t) |
| 69 |
if err != nil { |
| 70 |
t.Fatalf("%s: test failed parsing time.Time", name) |
| 71 |
} |
| 72 |
|
| 73 |
o := Format(tm) |
| 74 |
p, err := Parse(o[:17]) |
| 75 |
if err != nil { |
| 76 |
t.Fatalf("%s: test failed parsing", name) |
| 77 |
} |
| 78 |
|
| 79 |
ts := tm.Truncate(time.Second) |
| 80 |
if ts != p { |
| 81 |
t.Fatalf("%s: test failed date compare:\n %s != %s", name, ts, p) |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
func TestFormat(t *testing.T) { |
| 87 |
for _, name := range getOrderedTestNames() { |
| 88 |
tt := tests[name] |
| 89 |
tm, err := time.Parse(time.RFC3339Nano, tt.t) |
| 90 |
if err != nil { |
| 91 |
t.Fatalf("%s: test failed parsing time.Time", name) |
| 92 |
} |
| 93 |
|
| 94 |
o := FormatNano(tm) |
| 95 |
if tt.o != o { |
| 96 |
t.Fatalf("%s: test failed date compare:\n %s != %s", name, tt.o, o) |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func TestParse(t *testing.T) { |
| 102 |
for _, name := range getOrderedTestNames() { |
| 103 |
tt := tests[name] |
| 104 |
tm, err := time.Parse(time.RFC3339Nano, tt.t) |
| 105 |
if err != nil { |
| 106 |
t.Fatalf("%s: test failed parsing time.Time", name) |
| 107 |
} |
| 108 |
|
| 109 |
p, err := Parse(tt.o) |
| 110 |
if err != nil { |
| 111 |
t.Fatalf("%s: test failed parsing", name) |
| 112 |
} |
| 113 |
|
| 114 |
if tm != p { |
| 115 |
t.Fatalf("%s: test failed date compare:\n %s != %s", name, tm, p) |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
func BenchmarkFormat(b *testing.B) { |
| 121 |
t := time.Date(2016, 12, 31, 23, 59, 59, 00, time.UTC) |
| 122 |
b.ResetTimer() |
| 123 |
for i := 0; i < b.N; i++ { |
| 124 |
Format(t) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
func BenchmarkParse(b *testing.B) { |
| 129 |
s := "@40000000586846a300000000" |
| 130 |
b.ResetTimer() |
| 131 |
for i := 0; i < b.N; i++ { |
| 132 |
Parse(s) |
| 133 |
} |
| 134 |
} |