| 1 |
//go:build go1.7 |
| 2 |
// +build go1.7 |
| 3 |
|
| 4 |
package date |
| 5 |
|
| 6 |
// Copyright 2017 Microsoft Corporation |
| 7 |
// |
| 8 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
// you may not use this file except in compliance with the License. |
| 10 |
// You may obtain a copy of the License at |
| 11 |
// |
| 12 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
// |
| 14 |
// Unless required by applicable law or agreed to in writing, software |
| 15 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
// See the License for the specific language governing permissions and |
| 18 |
// limitations under the License. |
| 19 |
|
| 20 |
import ( |
| 21 |
"bytes" |
| 22 |
"encoding/binary" |
| 23 |
"encoding/json" |
| 24 |
"fmt" |
| 25 |
"math" |
| 26 |
"testing" |
| 27 |
"time" |
| 28 |
) |
| 29 |
|
| 30 |
func ExampleUnixTime_MarshalJSON() { |
| 31 |
epoch := UnixTime(UnixEpoch()) |
| 32 |
text, _ := json.Marshal(epoch) |
| 33 |
fmt.Print(string(text)) |
| 34 |
// Output: 0 |
| 35 |
} |
| 36 |
|
| 37 |
func ExampleUnixTime_UnmarshalJSON() { |
| 38 |
var myTime UnixTime |
| 39 |
json.Unmarshal([]byte("1.3e2"), &myTime) |
| 40 |
fmt.Printf("%v", time.Time(myTime)) |
| 41 |
// Output: 1970-01-01 00:02:10 +0000 UTC |
| 42 |
} |
| 43 |
|
| 44 |
func TestUnixTime_MarshalJSON(t *testing.T) { |
| 45 |
testCases := []time.Time{ |
| 46 |
UnixEpoch().Add(-1 * time.Second), // One second befote the Unix Epoch |
| 47 |
time.Date(2017, time.April, 14, 20, 27, 47, 0, time.UTC), // The time this test was written |
| 48 |
UnixEpoch(), |
| 49 |
time.Date(1800, 01, 01, 0, 0, 0, 0, time.UTC), |
| 50 |
time.Date(2200, 12, 29, 00, 01, 37, 82, time.UTC), |
| 51 |
} |
| 52 |
|
| 53 |
for _, tc := range testCases { |
| 54 |
t.Run(tc.String(), func(subT *testing.T) { |
| 55 |
var actual, expected float64 |
| 56 |
var marshaled []byte |
| 57 |
|
| 58 |
target := UnixTime(tc) |
| 59 |
expected = float64(target.Duration().Nanoseconds()) / 1e9 |
| 60 |
|
| 61 |
if temp, err := json.Marshal(target); err == nil { |
| 62 |
marshaled = temp |
| 63 |
} else { |
| 64 |
subT.Error(err) |
| 65 |
return |
| 66 |
} |
| 67 |
|
| 68 |
dec := json.NewDecoder(bytes.NewReader(marshaled)) |
| 69 |
if err := dec.Decode(&actual); err != nil { |
| 70 |
subT.Error(err) |
| 71 |
return |
| 72 |
} |
| 73 |
|
| 74 |
diff := math.Abs(actual - expected) |
| 75 |
subT.Logf("\ngot :\t%g\nwant:\t%g\ndiff:\t%g", actual, expected, diff) |
| 76 |
if diff > 1e-9 { //Must be within 1 nanosecond of one another |
| 77 |
subT.Fail() |
| 78 |
} |
| 79 |
}) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func TestUnixTime_UnmarshalJSON(t *testing.T) { |
| 84 |
testCases := []struct { |
| 85 |
text string |
| 86 |
expected time.Time |
| 87 |
}{ |
| 88 |
{"1", UnixEpoch().Add(time.Second)}, |
| 89 |
{"0", UnixEpoch()}, |
| 90 |
{"1492203742", time.Date(2017, time.April, 14, 21, 02, 22, 0, time.UTC)}, // The time this test was written |
| 91 |
{"-1", time.Date(1969, time.December, 31, 23, 59, 59, 0, time.UTC)}, |
| 92 |
{"1.5", UnixEpoch().Add(1500 * time.Millisecond)}, |
| 93 |
{"0e1", UnixEpoch()}, // See http://json.org for 'number' format definition. |
| 94 |
{"1.3e+2", UnixEpoch().Add(130 * time.Second)}, |
| 95 |
{"1.6E-10", UnixEpoch()}, // This is so small, it should get truncated into the UnixEpoch |
| 96 |
{"2E-6", UnixEpoch().Add(2 * time.Microsecond)}, |
| 97 |
{"1.289345e9", UnixEpoch().Add(1289345000 * time.Second)}, |
| 98 |
{"1e-9", UnixEpoch().Add(time.Nanosecond)}, |
| 99 |
} |
| 100 |
|
| 101 |
for _, tc := range testCases { |
| 102 |
t.Run(tc.text, func(subT *testing.T) { |
| 103 |
var rehydrated UnixTime |
| 104 |
if err := json.Unmarshal([]byte(tc.text), &rehydrated); err != nil { |
| 105 |
subT.Error(err) |
| 106 |
return |
| 107 |
} |
| 108 |
|
| 109 |
if time.Time(rehydrated) != tc.expected { |
| 110 |
subT.Logf("\ngot: \t%v\nwant:\t%v\ndiff:\t%v", time.Time(rehydrated), tc.expected, time.Time(rehydrated).Sub(tc.expected)) |
| 111 |
subT.Fail() |
| 112 |
} |
| 113 |
}) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
func TestUnixTime_JSONRoundTrip(t *testing.T) { |
| 118 |
testCases := []time.Time{ |
| 119 |
UnixEpoch(), |
| 120 |
time.Date(2005, time.November, 5, 0, 0, 0, 0, time.UTC), // The day V for Vendetta (film) was released. |
| 121 |
UnixEpoch().Add(-6 * time.Second), |
| 122 |
UnixEpoch().Add(800 * time.Hour), |
| 123 |
UnixEpoch().Add(time.Nanosecond), |
| 124 |
time.Date(2015, time.September, 05, 4, 30, 12, 9992, time.UTC), |
| 125 |
} |
| 126 |
|
| 127 |
for _, tc := range testCases { |
| 128 |
t.Run(tc.String(), func(subT *testing.T) { |
| 129 |
subject := UnixTime(tc) |
| 130 |
var marshaled []byte |
| 131 |
if temp, err := json.Marshal(subject); err == nil { |
| 132 |
marshaled = temp |
| 133 |
} else { |
| 134 |
subT.Error(err) |
| 135 |
return |
| 136 |
} |
| 137 |
|
| 138 |
var unmarshaled UnixTime |
| 139 |
if err := json.Unmarshal(marshaled, &unmarshaled); err != nil { |
| 140 |
subT.Error(err) |
| 141 |
} |
| 142 |
|
| 143 |
actual := time.Time(unmarshaled) |
| 144 |
diff := actual.Sub(tc) |
| 145 |
subT.Logf("\ngot :\t%s\nwant:\t%s\ndiff:\t%s", actual.String(), tc.String(), diff.String()) |
| 146 |
|
| 147 |
if diff > time.Duration(100) { // We lose some precision be working in floats. We shouldn't lose more than 100 nanoseconds. |
| 148 |
subT.Fail() |
| 149 |
} |
| 150 |
}) |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
func TestUnixTime_MarshalBinary(t *testing.T) { |
| 155 |
testCases := []struct { |
| 156 |
expected int64 |
| 157 |
subject time.Time |
| 158 |
}{ |
| 159 |
{0, UnixEpoch()}, |
| 160 |
{-15 * int64(time.Second), UnixEpoch().Add(-15 * time.Second)}, |
| 161 |
{54, UnixEpoch().Add(54 * time.Nanosecond)}, |
| 162 |
} |
| 163 |
|
| 164 |
for _, tc := range testCases { |
| 165 |
t.Run("", func(subT *testing.T) { |
| 166 |
var marshaled []byte |
| 167 |
|
| 168 |
if temp, err := UnixTime(tc.subject).MarshalBinary(); err == nil { |
| 169 |
marshaled = temp |
| 170 |
} else { |
| 171 |
subT.Error(err) |
| 172 |
return |
| 173 |
} |
| 174 |
|
| 175 |
var unmarshaled int64 |
| 176 |
if err := binary.Read(bytes.NewReader(marshaled), binary.LittleEndian, &unmarshaled); err != nil { |
| 177 |
subT.Error(err) |
| 178 |
return |
| 179 |
} |
| 180 |
|
| 181 |
if unmarshaled != tc.expected { |
| 182 |
subT.Logf("\ngot: \t%d\nwant:\t%d", unmarshaled, tc.expected) |
| 183 |
subT.Fail() |
| 184 |
} |
| 185 |
}) |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
func TestUnixTime_BinaryRoundTrip(t *testing.T) { |
| 190 |
testCases := []time.Time{ |
| 191 |
UnixEpoch(), |
| 192 |
UnixEpoch().Add(800 * time.Minute), |
| 193 |
UnixEpoch().Add(7 * time.Hour), |
| 194 |
UnixEpoch().Add(-1 * time.Nanosecond), |
| 195 |
} |
| 196 |
|
| 197 |
for _, tc := range testCases { |
| 198 |
t.Run(tc.String(), func(subT *testing.T) { |
| 199 |
original := UnixTime(tc) |
| 200 |
var marshaled []byte |
| 201 |
|
| 202 |
if temp, err := original.MarshalBinary(); err == nil { |
| 203 |
marshaled = temp |
| 204 |
} else { |
| 205 |
subT.Error(err) |
| 206 |
return |
| 207 |
} |
| 208 |
|
| 209 |
var traveled UnixTime |
| 210 |
if err := traveled.UnmarshalBinary(marshaled); err != nil { |
| 211 |
subT.Error(err) |
| 212 |
return |
| 213 |
} |
| 214 |
|
| 215 |
if traveled != original { |
| 216 |
subT.Logf("\ngot: \t%s\nwant:\t%s", time.Time(original).String(), time.Time(traveled).String()) |
| 217 |
subT.Fail() |
| 218 |
} |
| 219 |
}) |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
func TestUnixTime_MarshalText(t *testing.T) { |
| 224 |
testCases := []time.Time{ |
| 225 |
UnixEpoch(), |
| 226 |
UnixEpoch().Add(45 * time.Second), |
| 227 |
UnixEpoch().Add(time.Nanosecond), |
| 228 |
UnixEpoch().Add(-100000 * time.Second), |
| 229 |
} |
| 230 |
|
| 231 |
for _, tc := range testCases { |
| 232 |
expected, _ := tc.MarshalText() |
| 233 |
t.Run("", func(subT *testing.T) { |
| 234 |
var marshaled []byte |
| 235 |
|
| 236 |
if temp, err := UnixTime(tc).MarshalText(); err == nil { |
| 237 |
marshaled = temp |
| 238 |
} else { |
| 239 |
subT.Error(err) |
| 240 |
return |
| 241 |
} |
| 242 |
|
| 243 |
if string(marshaled) != string(expected) { |
| 244 |
subT.Logf("\ngot: \t%s\nwant:\t%s", string(marshaled), string(expected)) |
| 245 |
subT.Fail() |
| 246 |
} |
| 247 |
}) |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
func TestUnixTime_TextRoundTrip(t *testing.T) { |
| 252 |
testCases := []time.Time{ |
| 253 |
UnixEpoch(), |
| 254 |
UnixEpoch().Add(-1 * time.Nanosecond), |
| 255 |
UnixEpoch().Add(1 * time.Nanosecond), |
| 256 |
time.Date(2017, time.April, 17, 21, 00, 00, 00, time.UTC), |
| 257 |
} |
| 258 |
|
| 259 |
for _, tc := range testCases { |
| 260 |
t.Run(tc.String(), func(subT *testing.T) { |
| 261 |
unixTC := UnixTime(tc) |
| 262 |
|
| 263 |
var marshaled []byte |
| 264 |
|
| 265 |
if temp, err := unixTC.MarshalText(); err == nil { |
| 266 |
marshaled = temp |
| 267 |
} else { |
| 268 |
subT.Error(err) |
| 269 |
return |
| 270 |
} |
| 271 |
|
| 272 |
var unmarshaled UnixTime |
| 273 |
if err := unmarshaled.UnmarshalText(marshaled); err != nil { |
| 274 |
subT.Error(err) |
| 275 |
return |
| 276 |
} |
| 277 |
|
| 278 |
if unmarshaled != unixTC { |
| 279 |
t.Logf("\ngot: \t%s\nwant:\t%s", time.Time(unmarshaled).String(), tc.String()) |
| 280 |
t.Fail() |
| 281 |
} |
| 282 |
}) |
| 283 |
} |
| 284 |
} |