github.com/rakyll/go@v0.0.0-20170216000551-64c02460d703/src/time/time_test.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package time_test 6 7 import ( 8 "bytes" 9 "encoding/gob" 10 "encoding/json" 11 "fmt" 12 "math/big" 13 "math/rand" 14 "os" 15 "runtime" 16 "strings" 17 "testing" 18 "testing/quick" 19 . "time" 20 ) 21 22 // We should be in PST/PDT, but if the time zone files are missing we 23 // won't be. The purpose of this test is to at least explain why some of 24 // the subsequent tests fail. 25 func TestZoneData(t *testing.T) { 26 lt := Now() 27 // PST is 8 hours west, PDT is 7 hours west. We could use the name but it's not unique. 28 if name, off := lt.Zone(); off != -8*60*60 && off != -7*60*60 { 29 t.Errorf("Unable to find US Pacific time zone data for testing; time zone is %q offset %d", name, off) 30 t.Error("Likely problem: the time zone files have not been installed.") 31 } 32 } 33 34 // parsedTime is the struct representing a parsed time value. 35 type parsedTime struct { 36 Year int 37 Month Month 38 Day int 39 Hour, Minute, Second int // 15:04:05 is 15, 4, 5. 40 Nanosecond int // Fractional second. 41 Weekday Weekday 42 ZoneOffset int // seconds east of UTC, e.g. -7*60*60 for -0700 43 Zone string // e.g., "MST" 44 } 45 46 type TimeTest struct { 47 seconds int64 48 golden parsedTime 49 } 50 51 var utctests = []TimeTest{ 52 {0, parsedTime{1970, January, 1, 0, 0, 0, 0, Thursday, 0, "UTC"}}, 53 {1221681866, parsedTime{2008, September, 17, 20, 4, 26, 0, Wednesday, 0, "UTC"}}, 54 {-1221681866, parsedTime{1931, April, 16, 3, 55, 34, 0, Thursday, 0, "UTC"}}, 55 {-11644473600, parsedTime{1601, January, 1, 0, 0, 0, 0, Monday, 0, "UTC"}}, 56 {599529660, parsedTime{1988, December, 31, 0, 1, 0, 0, Saturday, 0, "UTC"}}, 57 {978220860, parsedTime{2000, December, 31, 0, 1, 0, 0, Sunday, 0, "UTC"}}, 58 } 59 60 var nanoutctests = []TimeTest{ 61 {0, parsedTime{1970, January, 1, 0, 0, 0, 1e8, Thursday, 0, "UTC"}}, 62 {1221681866, parsedTime{2008, September, 17, 20, 4, 26, 2e8, Wednesday, 0, "UTC"}}, 63 } 64 65 var localtests = []TimeTest{ 66 {0, parsedTime{1969, December, 31, 16, 0, 0, 0, Wednesday, -8 * 60 * 60, "PST"}}, 67 {1221681866, parsedTime{2008, September, 17, 13, 4, 26, 0, Wednesday, -7 * 60 * 60, "PDT"}}, 68 } 69 70 var nanolocaltests = []TimeTest{ 71 {0, parsedTime{1969, December, 31, 16, 0, 0, 1e8, Wednesday, -8 * 60 * 60, "PST"}}, 72 {1221681866, parsedTime{2008, September, 17, 13, 4, 26, 3e8, Wednesday, -7 * 60 * 60, "PDT"}}, 73 } 74 75 func same(t Time, u *parsedTime) bool { 76 // Check aggregates. 77 year, month, day := t.Date() 78 hour, min, sec := t.Clock() 79 name, offset := t.Zone() 80 if year != u.Year || month != u.Month || day != u.Day || 81 hour != u.Hour || min != u.Minute || sec != u.Second || 82 name != u.Zone || offset != u.ZoneOffset { 83 return false 84 } 85 // Check individual entries. 86 return t.Year() == u.Year && 87 t.Month() == u.Month && 88 t.Day() == u.Day && 89 t.Hour() == u.Hour && 90 t.Minute() == u.Minute && 91 t.Second() == u.Second && 92 t.Nanosecond() == u.Nanosecond && 93 t.Weekday() == u.Weekday 94 } 95 96 func TestSecondsToUTC(t *testing.T) { 97 for _, test := range utctests { 98 sec := test.seconds 99 golden := &test.golden 100 tm := Unix(sec, 0).UTC() 101 newsec := tm.Unix() 102 if newsec != sec { 103 t.Errorf("SecondsToUTC(%d).Seconds() = %d", sec, newsec) 104 } 105 if !same(tm, golden) { 106 t.Errorf("SecondsToUTC(%d): // %#v", sec, tm) 107 t.Errorf(" want=%+v", *golden) 108 t.Errorf(" have=%v", tm.Format(RFC3339+" MST")) 109 } 110 } 111 } 112 113 func TestNanosecondsToUTC(t *testing.T) { 114 for _, test := range nanoutctests { 115 golden := &test.golden 116 nsec := test.seconds*1e9 + int64(golden.Nanosecond) 117 tm := Unix(0, nsec).UTC() 118 newnsec := tm.Unix()*1e9 + int64(tm.Nanosecond()) 119 if newnsec != nsec { 120 t.Errorf("NanosecondsToUTC(%d).Nanoseconds() = %d", nsec, newnsec) 121 } 122 if !same(tm, golden) { 123 t.Errorf("NanosecondsToUTC(%d):", nsec) 124 t.Errorf(" want=%+v", *golden) 125 t.Errorf(" have=%+v", tm.Format(RFC3339+" MST")) 126 } 127 } 128 } 129 130 func TestSecondsToLocalTime(t *testing.T) { 131 for _, test := range localtests { 132 sec := test.seconds 133 golden := &test.golden 134 tm := Unix(sec, 0) 135 newsec := tm.Unix() 136 if newsec != sec { 137 t.Errorf("SecondsToLocalTime(%d).Seconds() = %d", sec, newsec) 138 } 139 if !same(tm, golden) { 140 t.Errorf("SecondsToLocalTime(%d):", sec) 141 t.Errorf(" want=%+v", *golden) 142 t.Errorf(" have=%+v", tm.Format(RFC3339+" MST")) 143 } 144 } 145 } 146 147 func TestNanosecondsToLocalTime(t *testing.T) { 148 for _, test := range nanolocaltests { 149 golden := &test.golden 150 nsec := test.seconds*1e9 + int64(golden.Nanosecond) 151 tm := Unix(0, nsec) 152 newnsec := tm.Unix()*1e9 + int64(tm.Nanosecond()) 153 if newnsec != nsec { 154 t.Errorf("NanosecondsToLocalTime(%d).Seconds() = %d", nsec, newnsec) 155 } 156 if !same(tm, golden) { 157 t.Errorf("NanosecondsToLocalTime(%d):", nsec) 158 t.Errorf(" want=%+v", *golden) 159 t.Errorf(" have=%+v", tm.Format(RFC3339+" MST")) 160 } 161 } 162 } 163 164 func TestSecondsToUTCAndBack(t *testing.T) { 165 f := func(sec int64) bool { return Unix(sec, 0).UTC().Unix() == sec } 166 f32 := func(sec int32) bool { return f(int64(sec)) } 167 cfg := &quick.Config{MaxCount: 10000} 168 169 // Try a reasonable date first, then the huge ones. 170 if err := quick.Check(f32, cfg); err != nil { 171 t.Fatal(err) 172 } 173 if err := quick.Check(f, cfg); err != nil { 174 t.Fatal(err) 175 } 176 } 177 178 func TestNanosecondsToUTCAndBack(t *testing.T) { 179 f := func(nsec int64) bool { 180 t := Unix(0, nsec).UTC() 181 ns := t.Unix()*1e9 + int64(t.Nanosecond()) 182 return ns == nsec 183 } 184 f32 := func(nsec int32) bool { return f(int64(nsec)) } 185 cfg := &quick.Config{MaxCount: 10000} 186 187 // Try a small date first, then the large ones. (The span is only a few hundred years 188 // for nanoseconds in an int64.) 189 if err := quick.Check(f32, cfg); err != nil { 190 t.Fatal(err) 191 } 192 if err := quick.Check(f, cfg); err != nil { 193 t.Fatal(err) 194 } 195 } 196 197 // The time routines provide no way to get absolute time 198 // (seconds since zero), but we need it to compute the right 199 // answer for bizarre roundings like "to the nearest 3 ns". 200 // Compute as t - year1 = (t - 1970) + (1970 - 2001) + (2001 - 1). 201 // t - 1970 is returned by Unix and Nanosecond. 202 // 1970 - 2001 is -(31*365+8)*86400 = -978307200 seconds. 203 // 2001 - 1 is 2000*365.2425*86400 = 63113904000 seconds. 204 const unixToZero = -978307200 + 63113904000 205 206 // abs returns the absolute time stored in t, as seconds and nanoseconds. 207 func abs(t Time) (sec, nsec int64) { 208 unix := t.Unix() 209 nano := t.Nanosecond() 210 return unix + unixToZero, int64(nano) 211 } 212 213 // absString returns abs as a decimal string. 214 func absString(t Time) string { 215 sec, nsec := abs(t) 216 if sec < 0 { 217 sec = -sec 218 nsec = -nsec 219 if nsec < 0 { 220 nsec += 1e9 221 sec-- 222 } 223 return fmt.Sprintf("-%d%09d", sec, nsec) 224 } 225 return fmt.Sprintf("%d%09d", sec, nsec) 226 } 227 228 var truncateRoundTests = []struct { 229 t Time 230 d Duration 231 }{ 232 {Date(-1, January, 1, 12, 15, 30, 5e8, UTC), 3}, 233 {Date(-1, January, 1, 12, 15, 31, 5e8, UTC), 3}, 234 {Date(2012, January, 1, 12, 15, 30, 5e8, UTC), Second}, 235 {Date(2012, January, 1, 12, 15, 31, 5e8, UTC), Second}, 236 } 237 238 func TestTruncateRound(t *testing.T) { 239 var ( 240 bsec = new(big.Int) 241 bnsec = new(big.Int) 242 bd = new(big.Int) 243 bt = new(big.Int) 244 br = new(big.Int) 245 bq = new(big.Int) 246 b1e9 = new(big.Int) 247 ) 248 249 b1e9.SetInt64(1e9) 250 251 testOne := func(ti, tns, di int64) bool { 252 t0 := Unix(ti, int64(tns)).UTC() 253 d := Duration(di) 254 if d < 0 { 255 d = -d 256 } 257 if d <= 0 { 258 d = 1 259 } 260 261 // Compute bt = absolute nanoseconds. 262 sec, nsec := abs(t0) 263 bsec.SetInt64(sec) 264 bnsec.SetInt64(nsec) 265 bt.Mul(bsec, b1e9) 266 bt.Add(bt, bnsec) 267 268 // Compute quotient and remainder mod d. 269 bd.SetInt64(int64(d)) 270 bq.DivMod(bt, bd, br) 271 272 // To truncate, subtract remainder. 273 // br is < d, so it fits in an int64. 274 r := br.Int64() 275 t1 := t0.Add(-Duration(r)) 276 277 // Check that time.Truncate works. 278 if trunc := t0.Truncate(d); trunc != t1 { 279 t.Errorf("Time.Truncate(%s, %s) = %s, want %s\n"+ 280 "%v trunc %v =\n%v want\n%v", 281 t0.Format(RFC3339Nano), d, trunc, t1.Format(RFC3339Nano), 282 absString(t0), int64(d), absString(trunc), absString(t1)) 283 return false 284 } 285 286 // To round, add d back if remainder r > d/2 or r == exactly d/2. 287 // The commented out code would round half to even instead of up, 288 // but that makes it time-zone dependent, which is a bit strange. 289 if r > int64(d)/2 || r+r == int64(d) /*&& bq.Bit(0) == 1*/ { 290 t1 = t1.Add(Duration(d)) 291 } 292 293 // Check that time.Round works. 294 if rnd := t0.Round(d); rnd != t1 { 295 t.Errorf("Time.Round(%s, %s) = %s, want %s\n"+ 296 "%v round %v =\n%v want\n%v", 297 t0.Format(RFC3339Nano), d, rnd, t1.Format(RFC3339Nano), 298 absString(t0), int64(d), absString(rnd), absString(t1)) 299 return false 300 } 301 return true 302 } 303 304 // manual test cases 305 for _, tt := range truncateRoundTests { 306 testOne(tt.t.Unix(), int64(tt.t.Nanosecond()), int64(tt.d)) 307 } 308 309 // exhaustive near 0 310 for i := 0; i < 100; i++ { 311 for j := 1; j < 100; j++ { 312 testOne(unixToZero, int64(i), int64(j)) 313 testOne(unixToZero, -int64(i), int64(j)) 314 if t.Failed() { 315 return 316 } 317 } 318 } 319 320 if t.Failed() { 321 return 322 } 323 324 // randomly generated test cases 325 cfg := &quick.Config{MaxCount: 100000} 326 if testing.Short() { 327 cfg.MaxCount = 1000 328 } 329 330 // divisors of Second 331 f1 := func(ti int64, tns int32, logdi int32) bool { 332 d := Duration(1) 333 a, b := uint(logdi%9), (logdi>>16)%9 334 d <<= a 335 for i := 0; i < int(b); i++ { 336 d *= 5 337 } 338 return testOne(ti, int64(tns), int64(d)) 339 } 340 quick.Check(f1, cfg) 341 342 // multiples of Second 343 f2 := func(ti int64, tns int32, di int32) bool { 344 d := Duration(di) * Second 345 if d < 0 { 346 d = -d 347 } 348 return testOne(ti, int64(tns), int64(d)) 349 } 350 quick.Check(f2, cfg) 351 352 // halfway cases 353 f3 := func(tns, di int64) bool { 354 di &= 0xfffffffe 355 if di == 0 { 356 di = 2 357 } 358 tns -= tns % di 359 if tns < 0 { 360 tns += di / 2 361 } else { 362 tns -= di / 2 363 } 364 return testOne(0, tns, di) 365 } 366 quick.Check(f3, cfg) 367 368 // full generality 369 f4 := func(ti int64, tns int32, di int64) bool { 370 return testOne(ti, int64(tns), di) 371 } 372 quick.Check(f4, cfg) 373 } 374 375 type ISOWeekTest struct { 376 year int // year 377 month, day int // month and day 378 yex int // expected year 379 wex int // expected week 380 } 381 382 var isoWeekTests = []ISOWeekTest{ 383 {1981, 1, 1, 1981, 1}, {1982, 1, 1, 1981, 53}, {1983, 1, 1, 1982, 52}, 384 {1984, 1, 1, 1983, 52}, {1985, 1, 1, 1985, 1}, {1986, 1, 1, 1986, 1}, 385 {1987, 1, 1, 1987, 1}, {1988, 1, 1, 1987, 53}, {1989, 1, 1, 1988, 52}, 386 {1990, 1, 1, 1990, 1}, {1991, 1, 1, 1991, 1}, {1992, 1, 1, 1992, 1}, 387 {1993, 1, 1, 1992, 53}, {1994, 1, 1, 1993, 52}, {1995, 1, 2, 1995, 1}, 388 {1996, 1, 1, 1996, 1}, {1996, 1, 7, 1996, 1}, {1996, 1, 8, 1996, 2}, 389 {1997, 1, 1, 1997, 1}, {1998, 1, 1, 1998, 1}, {1999, 1, 1, 1998, 53}, 390 {2000, 1, 1, 1999, 52}, {2001, 1, 1, 2001, 1}, {2002, 1, 1, 2002, 1}, 391 {2003, 1, 1, 2003, 1}, {2004, 1, 1, 2004, 1}, {2005, 1, 1, 2004, 53}, 392 {2006, 1, 1, 2005, 52}, {2007, 1, 1, 2007, 1}, {2008, 1, 1, 2008, 1}, 393 {2009, 1, 1, 2009, 1}, {2010, 1, 1, 2009, 53}, {2010, 1, 1, 2009, 53}, 394 {2011, 1, 1, 2010, 52}, {2011, 1, 2, 2010, 52}, {2011, 1, 3, 2011, 1}, 395 {2011, 1, 4, 2011, 1}, {2011, 1, 5, 2011, 1}, {2011, 1, 6, 2011, 1}, 396 {2011, 1, 7, 2011, 1}, {2011, 1, 8, 2011, 1}, {2011, 1, 9, 2011, 1}, 397 {2011, 1, 10, 2011, 2}, {2011, 1, 11, 2011, 2}, {2011, 6, 12, 2011, 23}, 398 {2011, 6, 13, 2011, 24}, {2011, 12, 25, 2011, 51}, {2011, 12, 26, 2011, 52}, 399 {2011, 12, 27, 2011, 52}, {2011, 12, 28, 2011, 52}, {2011, 12, 29, 2011, 52}, 400 {2011, 12, 30, 2011, 52}, {2011, 12, 31, 2011, 52}, {1995, 1, 1, 1994, 52}, 401 {2012, 1, 1, 2011, 52}, {2012, 1, 2, 2012, 1}, {2012, 1, 8, 2012, 1}, 402 {2012, 1, 9, 2012, 2}, {2012, 12, 23, 2012, 51}, {2012, 12, 24, 2012, 52}, 403 {2012, 12, 30, 2012, 52}, {2012, 12, 31, 2013, 1}, {2013, 1, 1, 2013, 1}, 404 {2013, 1, 6, 2013, 1}, {2013, 1, 7, 2013, 2}, {2013, 12, 22, 2013, 51}, 405 {2013, 12, 23, 2013, 52}, {2013, 12, 29, 2013, 52}, {2013, 12, 30, 2014, 1}, 406 {2014, 1, 1, 2014, 1}, {2014, 1, 5, 2014, 1}, {2014, 1, 6, 2014, 2}, 407 {2015, 1, 1, 2015, 1}, {2016, 1, 1, 2015, 53}, {2017, 1, 1, 2016, 52}, 408 {2018, 1, 1, 2018, 1}, {2019, 1, 1, 2019, 1}, {2020, 1, 1, 2020, 1}, 409 {2021, 1, 1, 2020, 53}, {2022, 1, 1, 2021, 52}, {2023, 1, 1, 2022, 52}, 410 {2024, 1, 1, 2024, 1}, {2025, 1, 1, 2025, 1}, {2026, 1, 1, 2026, 1}, 411 {2027, 1, 1, 2026, 53}, {2028, 1, 1, 2027, 52}, {2029, 1, 1, 2029, 1}, 412 {2030, 1, 1, 2030, 1}, {2031, 1, 1, 2031, 1}, {2032, 1, 1, 2032, 1}, 413 {2033, 1, 1, 2032, 53}, {2034, 1, 1, 2033, 52}, {2035, 1, 1, 2035, 1}, 414 {2036, 1, 1, 2036, 1}, {2037, 1, 1, 2037, 1}, {2038, 1, 1, 2037, 53}, 415 {2039, 1, 1, 2038, 52}, {2040, 1, 1, 2039, 52}, 416 } 417 418 func TestISOWeek(t *testing.T) { 419 // Selected dates and corner cases 420 for _, wt := range isoWeekTests { 421 dt := Date(wt.year, Month(wt.month), wt.day, 0, 0, 0, 0, UTC) 422 y, w := dt.ISOWeek() 423 if w != wt.wex || y != wt.yex { 424 t.Errorf("got %d/%d; expected %d/%d for %d-%02d-%02d", 425 y, w, wt.yex, wt.wex, wt.year, wt.month, wt.day) 426 } 427 } 428 429 // The only real invariant: Jan 04 is in week 1 430 for year := 1950; year < 2100; year++ { 431 if y, w := Date(year, January, 4, 0, 0, 0, 0, UTC).ISOWeek(); y != year || w != 1 { 432 t.Errorf("got %d/%d; expected %d/1 for Jan 04", y, w, year) 433 } 434 } 435 } 436 437 type YearDayTest struct { 438 year, month, day int 439 yday int 440 } 441 442 // Test YearDay in several different scenarios 443 // and corner cases 444 var yearDayTests = []YearDayTest{ 445 // Non-leap-year tests 446 {2007, 1, 1, 1}, 447 {2007, 1, 15, 15}, 448 {2007, 2, 1, 32}, 449 {2007, 2, 15, 46}, 450 {2007, 3, 1, 60}, 451 {2007, 3, 15, 74}, 452 {2007, 4, 1, 91}, 453 {2007, 12, 31, 365}, 454 455 // Leap-year tests 456 {2008, 1, 1, 1}, 457 {2008, 1, 15, 15}, 458 {2008, 2, 1, 32}, 459 {2008, 2, 15, 46}, 460 {2008, 3, 1, 61}, 461 {2008, 3, 15, 75}, 462 {2008, 4, 1, 92}, 463 {2008, 12, 31, 366}, 464 465 // Looks like leap-year (but isn't) tests 466 {1900, 1, 1, 1}, 467 {1900, 1, 15, 15}, 468 {1900, 2, 1, 32}, 469 {1900, 2, 15, 46}, 470 {1900, 3, 1, 60}, 471 {1900, 3, 15, 74}, 472 {1900, 4, 1, 91}, 473 {1900, 12, 31, 365}, 474 475 // Year one tests (non-leap) 476 {1, 1, 1, 1}, 477 {1, 1, 15, 15}, 478 {1, 2, 1, 32}, 479 {1, 2, 15, 46}, 480 {1, 3, 1, 60}, 481 {1, 3, 15, 74}, 482 {1, 4, 1, 91}, 483 {1, 12, 31, 365}, 484 485 // Year minus one tests (non-leap) 486 {-1, 1, 1, 1}, 487 {-1, 1, 15, 15}, 488 {-1, 2, 1, 32}, 489 {-1, 2, 15, 46}, 490 {-1, 3, 1, 60}, 491 {-1, 3, 15, 74}, 492 {-1, 4, 1, 91}, 493 {-1, 12, 31, 365}, 494 495 // 400 BC tests (leap-year) 496 {-400, 1, 1, 1}, 497 {-400, 1, 15, 15}, 498 {-400, 2, 1, 32}, 499 {-400, 2, 15, 46}, 500 {-400, 3, 1, 61}, 501 {-400, 3, 15, 75}, 502 {-400, 4, 1, 92}, 503 {-400, 12, 31, 366}, 504 505 // Special Cases 506 507 // Gregorian calendar change (no effect) 508 {1582, 10, 4, 277}, 509 {1582, 10, 15, 288}, 510 } 511 512 // Check to see if YearDay is location sensitive 513 var yearDayLocations = []*Location{ 514 FixedZone("UTC-8", -8*60*60), 515 FixedZone("UTC-4", -4*60*60), 516 UTC, 517 FixedZone("UTC+4", 4*60*60), 518 FixedZone("UTC+8", 8*60*60), 519 } 520 521 func TestYearDay(t *testing.T) { 522 for _, loc := range yearDayLocations { 523 for _, ydt := range yearDayTests { 524 dt := Date(ydt.year, Month(ydt.month), ydt.day, 0, 0, 0, 0, loc) 525 yday := dt.YearDay() 526 if yday != ydt.yday { 527 t.Errorf("got %d, expected %d for %d-%02d-%02d in %v", 528 yday, ydt.yday, ydt.year, ydt.month, ydt.day, loc) 529 } 530 } 531 } 532 } 533 534 var durationTests = []struct { 535 str string 536 d Duration 537 }{ 538 {"0s", 0}, 539 {"1ns", 1 * Nanosecond}, 540 {"1.1µs", 1100 * Nanosecond}, 541 {"2.2ms", 2200 * Microsecond}, 542 {"3.3s", 3300 * Millisecond}, 543 {"4m5s", 4*Minute + 5*Second}, 544 {"4m5.001s", 4*Minute + 5001*Millisecond}, 545 {"5h6m7.001s", 5*Hour + 6*Minute + 7001*Millisecond}, 546 {"8m0.000000001s", 8*Minute + 1*Nanosecond}, 547 {"2562047h47m16.854775807s", 1<<63 - 1}, 548 {"-2562047h47m16.854775808s", -1 << 63}, 549 } 550 551 func TestDurationString(t *testing.T) { 552 for _, tt := range durationTests { 553 if str := tt.d.String(); str != tt.str { 554 t.Errorf("Duration(%d).String() = %s, want %s", int64(tt.d), str, tt.str) 555 } 556 if tt.d > 0 { 557 if str := (-tt.d).String(); str != "-"+tt.str { 558 t.Errorf("Duration(%d).String() = %s, want %s", int64(-tt.d), str, "-"+tt.str) 559 } 560 } 561 } 562 } 563 564 var dateTests = []struct { 565 year, month, day, hour, min, sec, nsec int 566 z *Location 567 unix int64 568 }{ 569 {2011, 11, 6, 1, 0, 0, 0, Local, 1320566400}, // 1:00:00 PDT 570 {2011, 11, 6, 1, 59, 59, 0, Local, 1320569999}, // 1:59:59 PDT 571 {2011, 11, 6, 2, 0, 0, 0, Local, 1320573600}, // 2:00:00 PST 572 573 {2011, 3, 13, 1, 0, 0, 0, Local, 1300006800}, // 1:00:00 PST 574 {2011, 3, 13, 1, 59, 59, 0, Local, 1300010399}, // 1:59:59 PST 575 {2011, 3, 13, 3, 0, 0, 0, Local, 1300010400}, // 3:00:00 PDT 576 {2011, 3, 13, 2, 30, 0, 0, Local, 1300008600}, // 2:30:00 PDT ≡ 1:30 PST 577 578 // Many names for Fri Nov 18 7:56:35 PST 2011 579 {2011, 11, 18, 7, 56, 35, 0, Local, 1321631795}, // Nov 18 7:56:35 580 {2011, 11, 19, -17, 56, 35, 0, Local, 1321631795}, // Nov 19 -17:56:35 581 {2011, 11, 17, 31, 56, 35, 0, Local, 1321631795}, // Nov 17 31:56:35 582 {2011, 11, 18, 6, 116, 35, 0, Local, 1321631795}, // Nov 18 6:116:35 583 {2011, 10, 49, 7, 56, 35, 0, Local, 1321631795}, // Oct 49 7:56:35 584 {2011, 11, 18, 7, 55, 95, 0, Local, 1321631795}, // Nov 18 7:55:95 585 {2011, 11, 18, 7, 56, 34, 1e9, Local, 1321631795}, // Nov 18 7:56:34 + 10⁹ns 586 {2011, 12, -12, 7, 56, 35, 0, Local, 1321631795}, // Dec -21 7:56:35 587 {2012, 1, -43, 7, 56, 35, 0, Local, 1321631795}, // Jan -52 7:56:35 2012 588 {2012, int(January - 2), 18, 7, 56, 35, 0, Local, 1321631795}, // (Jan-2) 18 7:56:35 2012 589 {2010, int(December + 11), 18, 7, 56, 35, 0, Local, 1321631795}, // (Dec+11) 18 7:56:35 2010 590 } 591 592 func TestDate(t *testing.T) { 593 for _, tt := range dateTests { 594 time := Date(tt.year, Month(tt.month), tt.day, tt.hour, tt.min, tt.sec, tt.nsec, tt.z) 595 want := Unix(tt.unix, 0) 596 if !time.Equal(want) { 597 t.Errorf("Date(%d, %d, %d, %d, %d, %d, %d, %s) = %v, want %v", 598 tt.year, tt.month, tt.day, tt.hour, tt.min, tt.sec, tt.nsec, tt.z, 599 time, want) 600 } 601 } 602 } 603 604 // Several ways of getting from 605 // Fri Nov 18 7:56:35 PST 2011 606 // to 607 // Thu Mar 19 7:56:35 PST 2016 608 var addDateTests = []struct { 609 years, months, days int 610 }{ 611 {4, 4, 1}, 612 {3, 16, 1}, 613 {3, 15, 30}, 614 {5, -6, -18 - 30 - 12}, 615 } 616 617 func TestAddDate(t *testing.T) { 618 t0 := Date(2011, 11, 18, 7, 56, 35, 0, UTC) 619 t1 := Date(2016, 3, 19, 7, 56, 35, 0, UTC) 620 for _, at := range addDateTests { 621 time := t0.AddDate(at.years, at.months, at.days) 622 if !time.Equal(t1) { 623 t.Errorf("AddDate(%d, %d, %d) = %v, want %v", 624 at.years, at.months, at.days, 625 time, t1) 626 } 627 } 628 } 629 630 var daysInTests = []struct { 631 year, month, di int 632 }{ 633 {2011, 1, 31}, // January, first month, 31 days 634 {2011, 2, 28}, // February, non-leap year, 28 days 635 {2012, 2, 29}, // February, leap year, 29 days 636 {2011, 6, 30}, // June, 30 days 637 {2011, 12, 31}, // December, last month, 31 days 638 } 639 640 func TestDaysIn(t *testing.T) { 641 // The daysIn function is not exported. 642 // Test the daysIn function via the `var DaysIn = daysIn` 643 // statement in the internal_test.go file. 644 for _, tt := range daysInTests { 645 di := DaysIn(Month(tt.month), tt.year) 646 if di != tt.di { 647 t.Errorf("got %d; expected %d for %d-%02d", 648 di, tt.di, tt.year, tt.month) 649 } 650 } 651 } 652 653 func TestAddToExactSecond(t *testing.T) { 654 // Add an amount to the current time to round it up to the next exact second. 655 // This test checks that the nsec field still lies within the range [0, 999999999]. 656 t1 := Now() 657 t2 := t1.Add(Second - Duration(t1.Nanosecond())) 658 sec := (t1.Second() + 1) % 60 659 if t2.Second() != sec || t2.Nanosecond() != 0 { 660 t.Errorf("sec = %d, nsec = %d, want sec = %d, nsec = 0", t2.Second(), t2.Nanosecond(), sec) 661 } 662 } 663 664 func equalTimeAndZone(a, b Time) bool { 665 aname, aoffset := a.Zone() 666 bname, boffset := b.Zone() 667 return a.Equal(b) && aoffset == boffset && aname == bname 668 } 669 670 var gobTests = []Time{ 671 Date(0, 1, 2, 3, 4, 5, 6, UTC), 672 Date(7, 8, 9, 10, 11, 12, 13, FixedZone("", 0)), 673 Unix(81985467080890095, 0x76543210), // Time.sec: 0x0123456789ABCDEF 674 {}, // nil location 675 Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", 32767*60)), 676 Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", -32768*60)), 677 } 678 679 func TestTimeGob(t *testing.T) { 680 var b bytes.Buffer 681 enc := gob.NewEncoder(&b) 682 dec := gob.NewDecoder(&b) 683 for _, tt := range gobTests { 684 var gobtt Time 685 if err := enc.Encode(&tt); err != nil { 686 t.Errorf("%v gob Encode error = %q, want nil", tt, err) 687 } else if err := dec.Decode(&gobtt); err != nil { 688 t.Errorf("%v gob Decode error = %q, want nil", tt, err) 689 } else if !equalTimeAndZone(gobtt, tt) { 690 t.Errorf("Decoded time = %v, want %v", gobtt, tt) 691 } 692 b.Reset() 693 } 694 } 695 696 var invalidEncodingTests = []struct { 697 bytes []byte 698 want string 699 }{ 700 {[]byte{}, "Time.UnmarshalBinary: no data"}, 701 {[]byte{0, 2, 3}, "Time.UnmarshalBinary: unsupported version"}, 702 {[]byte{1, 2, 3}, "Time.UnmarshalBinary: invalid length"}, 703 } 704 705 func TestInvalidTimeGob(t *testing.T) { 706 for _, tt := range invalidEncodingTests { 707 var ignored Time 708 err := ignored.GobDecode(tt.bytes) 709 if err == nil || err.Error() != tt.want { 710 t.Errorf("time.GobDecode(%#v) error = %v, want %v", tt.bytes, err, tt.want) 711 } 712 err = ignored.UnmarshalBinary(tt.bytes) 713 if err == nil || err.Error() != tt.want { 714 t.Errorf("time.UnmarshalBinary(%#v) error = %v, want %v", tt.bytes, err, tt.want) 715 } 716 } 717 } 718 719 var notEncodableTimes = []struct { 720 time Time 721 want string 722 }{ 723 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 1)), "Time.MarshalBinary: zone offset has fractional minute"}, 724 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -1*60)), "Time.MarshalBinary: unexpected zone offset"}, 725 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -32769*60)), "Time.MarshalBinary: unexpected zone offset"}, 726 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 32768*60)), "Time.MarshalBinary: unexpected zone offset"}, 727 } 728 729 func TestNotGobEncodableTime(t *testing.T) { 730 for _, tt := range notEncodableTimes { 731 _, err := tt.time.GobEncode() 732 if err == nil || err.Error() != tt.want { 733 t.Errorf("%v GobEncode error = %v, want %v", tt.time, err, tt.want) 734 } 735 _, err = tt.time.MarshalBinary() 736 if err == nil || err.Error() != tt.want { 737 t.Errorf("%v MarshalBinary error = %v, want %v", tt.time, err, tt.want) 738 } 739 } 740 } 741 742 var jsonTests = []struct { 743 time Time 744 json string 745 }{ 746 {Date(9999, 4, 12, 23, 20, 50, 520*1e6, UTC), `"9999-04-12T23:20:50.52Z"`}, 747 {Date(1996, 12, 19, 16, 39, 57, 0, Local), `"1996-12-19T16:39:57-08:00"`}, 748 {Date(0, 1, 1, 0, 0, 0, 1, FixedZone("", 1*60)), `"0000-01-01T00:00:00.000000001+00:01"`}, 749 } 750 751 func TestTimeJSON(t *testing.T) { 752 for _, tt := range jsonTests { 753 var jsonTime Time 754 755 if jsonBytes, err := json.Marshal(tt.time); err != nil { 756 t.Errorf("%v json.Marshal error = %v, want nil", tt.time, err) 757 } else if string(jsonBytes) != tt.json { 758 t.Errorf("%v JSON = %#q, want %#q", tt.time, string(jsonBytes), tt.json) 759 } else if err = json.Unmarshal(jsonBytes, &jsonTime); err != nil { 760 t.Errorf("%v json.Unmarshal error = %v, want nil", tt.time, err) 761 } else if !equalTimeAndZone(jsonTime, tt.time) { 762 t.Errorf("Unmarshaled time = %v, want %v", jsonTime, tt.time) 763 } 764 } 765 } 766 767 func TestInvalidTimeJSON(t *testing.T) { 768 var tt Time 769 err := json.Unmarshal([]byte(`{"now is the time":"buddy"}`), &tt) 770 _, isParseErr := err.(*ParseError) 771 if !isParseErr { 772 t.Errorf("expected *time.ParseError unmarshaling JSON, got %v", err) 773 } 774 } 775 776 var notJSONEncodableTimes = []struct { 777 time Time 778 want string 779 }{ 780 {Date(10000, 1, 1, 0, 0, 0, 0, UTC), "Time.MarshalJSON: year outside of range [0,9999]"}, 781 {Date(-1, 1, 1, 0, 0, 0, 0, UTC), "Time.MarshalJSON: year outside of range [0,9999]"}, 782 } 783 784 func TestNotJSONEncodableTime(t *testing.T) { 785 for _, tt := range notJSONEncodableTimes { 786 _, err := tt.time.MarshalJSON() 787 if err == nil || err.Error() != tt.want { 788 t.Errorf("%v MarshalJSON error = %v, want %v", tt.time, err, tt.want) 789 } 790 } 791 } 792 793 var parseDurationTests = []struct { 794 in string 795 ok bool 796 want Duration 797 }{ 798 // simple 799 {"0", true, 0}, 800 {"5s", true, 5 * Second}, 801 {"30s", true, 30 * Second}, 802 {"1478s", true, 1478 * Second}, 803 // sign 804 {"-5s", true, -5 * Second}, 805 {"+5s", true, 5 * Second}, 806 {"-0", true, 0}, 807 {"+0", true, 0}, 808 // decimal 809 {"5.0s", true, 5 * Second}, 810 {"5.6s", true, 5*Second + 600*Millisecond}, 811 {"5.s", true, 5 * Second}, 812 {".5s", true, 500 * Millisecond}, 813 {"1.0s", true, 1 * Second}, 814 {"1.00s", true, 1 * Second}, 815 {"1.004s", true, 1*Second + 4*Millisecond}, 816 {"1.0040s", true, 1*Second + 4*Millisecond}, 817 {"100.00100s", true, 100*Second + 1*Millisecond}, 818 // different units 819 {"10ns", true, 10 * Nanosecond}, 820 {"11us", true, 11 * Microsecond}, 821 {"12µs", true, 12 * Microsecond}, // U+00B5 822 {"12μs", true, 12 * Microsecond}, // U+03BC 823 {"13ms", true, 13 * Millisecond}, 824 {"14s", true, 14 * Second}, 825 {"15m", true, 15 * Minute}, 826 {"16h", true, 16 * Hour}, 827 // composite durations 828 {"3h30m", true, 3*Hour + 30*Minute}, 829 {"10.5s4m", true, 4*Minute + 10*Second + 500*Millisecond}, 830 {"-2m3.4s", true, -(2*Minute + 3*Second + 400*Millisecond)}, 831 {"1h2m3s4ms5us6ns", true, 1*Hour + 2*Minute + 3*Second + 4*Millisecond + 5*Microsecond + 6*Nanosecond}, 832 {"39h9m14.425s", true, 39*Hour + 9*Minute + 14*Second + 425*Millisecond}, 833 // large value 834 {"52763797000ns", true, 52763797000 * Nanosecond}, 835 // more than 9 digits after decimal point, see https://golang.org/issue/6617 836 {"0.3333333333333333333h", true, 20 * Minute}, 837 // 9007199254740993 = 1<<53+1 cannot be stored precisely in a float64 838 {"9007199254740993ns", true, (1<<53 + 1) * Nanosecond}, 839 // largest duration that can be represented by int64 in nanoseconds 840 {"9223372036854775807ns", true, (1<<63 - 1) * Nanosecond}, 841 {"9223372036854775.807us", true, (1<<63 - 1) * Nanosecond}, 842 {"9223372036s854ms775us807ns", true, (1<<63 - 1) * Nanosecond}, 843 // large negative value 844 {"-9223372036854775807ns", true, -1<<63 + 1*Nanosecond}, 845 // huge string; issue 15011. 846 {"0.100000000000000000000h", true, 6 * Minute}, 847 // This value tests the first overflow check in leadingFraction. 848 {"0.830103483285477580700h", true, 49*Minute + 48*Second + 372539827*Nanosecond}, 849 850 // errors 851 {"", false, 0}, 852 {"3", false, 0}, 853 {"-", false, 0}, 854 {"s", false, 0}, 855 {".", false, 0}, 856 {"-.", false, 0}, 857 {".s", false, 0}, 858 {"+.s", false, 0}, 859 {"3000000h", false, 0}, // overflow 860 {"9223372036854775808ns", false, 0}, // overflow 861 {"9223372036854775.808us", false, 0}, // overflow 862 {"9223372036854ms775us808ns", false, 0}, // overflow 863 // largest negative value of type int64 in nanoseconds should fail 864 // see https://go-review.googlesource.com/#/c/2461/ 865 {"-9223372036854775808ns", false, 0}, 866 } 867 868 func TestParseDuration(t *testing.T) { 869 for _, tc := range parseDurationTests { 870 d, err := ParseDuration(tc.in) 871 if tc.ok && (err != nil || d != tc.want) { 872 t.Errorf("ParseDuration(%q) = %v, %v, want %v, nil", tc.in, d, err, tc.want) 873 } else if !tc.ok && err == nil { 874 t.Errorf("ParseDuration(%q) = _, nil, want _, non-nil", tc.in) 875 } 876 } 877 } 878 879 func TestParseDurationRoundTrip(t *testing.T) { 880 for i := 0; i < 100; i++ { 881 // Resolutions finer than milliseconds will result in 882 // imprecise round-trips. 883 d0 := Duration(rand.Int31()) * Millisecond 884 s := d0.String() 885 d1, err := ParseDuration(s) 886 if err != nil || d0 != d1 { 887 t.Errorf("round-trip failed: %d => %q => %d, %v", d0, s, d1, err) 888 } 889 } 890 } 891 892 // golang.org/issue/4622 893 func TestLocationRace(t *testing.T) { 894 ResetLocalOnceForTest() // reset the Once to trigger the race 895 896 c := make(chan string, 1) 897 go func() { 898 c <- Now().String() 899 }() 900 _ = Now().String() 901 <-c 902 Sleep(100 * Millisecond) 903 904 // Back to Los Angeles for subsequent tests: 905 ForceUSPacificForTesting() 906 } 907 908 var ( 909 t Time 910 u int64 911 ) 912 913 var mallocTest = []struct { 914 count int 915 desc string 916 fn func() 917 }{ 918 {0, `time.Now()`, func() { t = Now() }}, 919 {0, `time.Now().UnixNano()`, func() { u = Now().UnixNano() }}, 920 } 921 922 func TestCountMallocs(t *testing.T) { 923 if testing.Short() { 924 t.Skip("skipping malloc count in short mode") 925 } 926 if runtime.GOMAXPROCS(0) > 1 { 927 t.Skip("skipping; GOMAXPROCS>1") 928 } 929 for _, mt := range mallocTest { 930 allocs := int(testing.AllocsPerRun(100, mt.fn)) 931 if allocs > mt.count { 932 t.Errorf("%s: %d allocs, want %d", mt.desc, allocs, mt.count) 933 } 934 } 935 } 936 937 func TestLoadFixed(t *testing.T) { 938 // Issue 4064: handle locations without any zone transitions. 939 loc, err := LoadLocation("Etc/GMT+1") 940 if err != nil { 941 t.Fatal(err) 942 } 943 944 // The tzdata name Etc/GMT+1 uses "east is negative", 945 // but Go and most other systems use "east is positive". 946 // So GMT+1 corresponds to -3600 in the Go zone, not +3600. 947 name, offset := Now().In(loc).Zone() 948 // The zone abbreviation is "-01" since tzdata-2016g, and "GMT+1" 949 // on earlier versions; we accept both. (Issue #17276). 950 if !(name == "GMT+1" || name == "-01") || offset != -1*60*60 { 951 t.Errorf("Now().In(loc).Zone() = %q, %d, want %q or %q, %d", 952 name, offset, "GMT+1", "-01", -1*60*60) 953 } 954 } 955 956 const ( 957 minDuration Duration = -1 << 63 958 maxDuration Duration = 1<<63 - 1 959 ) 960 961 var subTests = []struct { 962 t Time 963 u Time 964 d Duration 965 }{ 966 {Time{}, Time{}, Duration(0)}, 967 {Date(2009, 11, 23, 0, 0, 0, 1, UTC), Date(2009, 11, 23, 0, 0, 0, 0, UTC), Duration(1)}, 968 {Date(2009, 11, 23, 0, 0, 0, 0, UTC), Date(2009, 11, 24, 0, 0, 0, 0, UTC), -24 * Hour}, 969 {Date(2009, 11, 24, 0, 0, 0, 0, UTC), Date(2009, 11, 23, 0, 0, 0, 0, UTC), 24 * Hour}, 970 {Date(-2009, 11, 24, 0, 0, 0, 0, UTC), Date(-2009, 11, 23, 0, 0, 0, 0, UTC), 24 * Hour}, 971 {Time{}, Date(2109, 11, 23, 0, 0, 0, 0, UTC), Duration(minDuration)}, 972 {Date(2109, 11, 23, 0, 0, 0, 0, UTC), Time{}, Duration(maxDuration)}, 973 {Time{}, Date(-2109, 11, 23, 0, 0, 0, 0, UTC), Duration(maxDuration)}, 974 {Date(-2109, 11, 23, 0, 0, 0, 0, UTC), Time{}, Duration(minDuration)}, 975 {Date(2290, 1, 1, 0, 0, 0, 0, UTC), Date(2000, 1, 1, 0, 0, 0, 0, UTC), 290*365*24*Hour + 71*24*Hour}, 976 {Date(2300, 1, 1, 0, 0, 0, 0, UTC), Date(2000, 1, 1, 0, 0, 0, 0, UTC), Duration(maxDuration)}, 977 {Date(2000, 1, 1, 0, 0, 0, 0, UTC), Date(2290, 1, 1, 0, 0, 0, 0, UTC), -290*365*24*Hour - 71*24*Hour}, 978 {Date(2000, 1, 1, 0, 0, 0, 0, UTC), Date(2300, 1, 1, 0, 0, 0, 0, UTC), Duration(minDuration)}, 979 } 980 981 func TestSub(t *testing.T) { 982 for i, st := range subTests { 983 got := st.t.Sub(st.u) 984 if got != st.d { 985 t.Errorf("#%d: Sub(%v, %v): got %v; want %v", i, st.t, st.u, got, st.d) 986 } 987 } 988 } 989 990 var nsDurationTests = []struct { 991 d Duration 992 want int64 993 }{ 994 {Duration(-1000), -1000}, 995 {Duration(-1), -1}, 996 {Duration(1), 1}, 997 {Duration(1000), 1000}, 998 } 999 1000 func TestDurationNanoseconds(t *testing.T) { 1001 for _, tt := range nsDurationTests { 1002 if got := tt.d.Nanoseconds(); got != tt.want { 1003 t.Errorf("d.Nanoseconds() = %d; want: %d", got, tt.want) 1004 } 1005 } 1006 } 1007 1008 var secDurationTests = []struct { 1009 d Duration 1010 want float64 1011 }{ 1012 {Duration(300000000), 0.3}, 1013 } 1014 1015 func TestDurationSeconds(t *testing.T) { 1016 for _, tt := range secDurationTests { 1017 if got := tt.d.Seconds(); got != tt.want { 1018 t.Errorf("d.Seconds() = %g; want: %g", got, tt.want) 1019 } 1020 } 1021 } 1022 1023 var minDurationTests = []struct { 1024 d Duration 1025 want float64 1026 }{ 1027 {Duration(-60000000000), -1}, 1028 {Duration(-1), -1 / 60e9}, 1029 {Duration(1), 1 / 60e9}, 1030 {Duration(60000000000), 1}, 1031 {Duration(3000), 5e-8}, 1032 } 1033 1034 func TestDurationMinutes(t *testing.T) { 1035 for _, tt := range minDurationTests { 1036 if got := tt.d.Minutes(); got != tt.want { 1037 t.Errorf("d.Minutes() = %g; want: %g", got, tt.want) 1038 } 1039 } 1040 } 1041 1042 var hourDurationTests = []struct { 1043 d Duration 1044 want float64 1045 }{ 1046 {Duration(-3600000000000), -1}, 1047 {Duration(-1), -1 / 3600e9}, 1048 {Duration(1), 1 / 3600e9}, 1049 {Duration(3600000000000), 1}, 1050 {Duration(36), 1e-11}, 1051 } 1052 1053 func TestDurationHours(t *testing.T) { 1054 for _, tt := range hourDurationTests { 1055 if got := tt.d.Hours(); got != tt.want { 1056 t.Errorf("d.Hours() = %g; want: %g", got, tt.want) 1057 } 1058 } 1059 } 1060 1061 var durationTruncateTests = []struct { 1062 d Duration 1063 m Duration 1064 want Duration 1065 }{ 1066 {0, Second, 0}, 1067 {Minute, -7 * Second, Minute}, 1068 {Minute, 0, Minute}, 1069 {Minute, 1, Minute}, 1070 {Minute + 10*Second, 10 * Second, Minute + 10*Second}, 1071 {2*Minute + 10*Second, Minute, 2 * Minute}, 1072 {10*Minute + 10*Second, 3 * Minute, 9 * Minute}, 1073 {Minute + 10*Second, Minute + 10*Second + 1, 0}, 1074 {Minute + 10*Second, Hour, 0}, 1075 {-Minute, Second, -Minute}, 1076 {-10 * Minute, 3 * Minute, -9 * Minute}, 1077 {-10 * Minute, Hour, 0}, 1078 } 1079 1080 func TestDurationTruncate(t *testing.T) { 1081 for _, tt := range durationTruncateTests { 1082 if got := tt.d.Truncate(tt.m); got != tt.want { 1083 t.Errorf("Duration(%s).Truncate(%s) = %s; want: %s", tt.d, tt.m, got, tt.want) 1084 } 1085 } 1086 } 1087 1088 var durationRoundTests = []struct { 1089 d Duration 1090 m Duration 1091 want Duration 1092 }{ 1093 {0, Second, 0}, 1094 {Minute, -11 * Second, Minute}, 1095 {Minute, 0, Minute}, 1096 {Minute, 1, Minute}, 1097 {2 * Minute, Minute, 2 * Minute}, 1098 {2*Minute + 10*Second, Minute, 2 * Minute}, 1099 {2*Minute + 30*Second, Minute, 3 * Minute}, 1100 {2*Minute + 50*Second, Minute, 3 * Minute}, 1101 {-Minute, 1, -Minute}, 1102 {-2 * Minute, Minute, -2 * Minute}, 1103 {-2*Minute - 10*Second, Minute, -2 * Minute}, 1104 {-2*Minute - 30*Second, Minute, -3 * Minute}, 1105 {-2*Minute - 50*Second, Minute, -3 * Minute}, 1106 {8e18, 3e18, 9e18}, 1107 {9e18, 5e18, 1<<63 - 1}, 1108 {-8e18, 3e18, -9e18}, 1109 {-9e18, 5e18, -1 << 63}, 1110 } 1111 1112 func TestDurationRound(t *testing.T) { 1113 for _, tt := range durationRoundTests { 1114 if got := tt.d.Round(tt.m); got != tt.want { 1115 t.Errorf("Duration(%s).Round(%s) = %s; want: %s", tt.d, tt.m, got, tt.want) 1116 } 1117 } 1118 } 1119 1120 var defaultLocTests = []struct { 1121 name string 1122 f func(t1, t2 Time) bool 1123 }{ 1124 {"After", func(t1, t2 Time) bool { return t1.After(t2) == t2.After(t1) }}, 1125 {"Before", func(t1, t2 Time) bool { return t1.Before(t2) == t2.Before(t1) }}, 1126 {"Equal", func(t1, t2 Time) bool { return t1.Equal(t2) == t2.Equal(t1) }}, 1127 1128 {"IsZero", func(t1, t2 Time) bool { return t1.IsZero() == t2.IsZero() }}, 1129 {"Date", func(t1, t2 Time) bool { 1130 a1, b1, c1 := t1.Date() 1131 a2, b2, c2 := t2.Date() 1132 return a1 == a2 && b1 == b2 && c1 == c2 1133 }}, 1134 {"Year", func(t1, t2 Time) bool { return t1.Year() == t2.Year() }}, 1135 {"Month", func(t1, t2 Time) bool { return t1.Month() == t2.Month() }}, 1136 {"Day", func(t1, t2 Time) bool { return t1.Day() == t2.Day() }}, 1137 {"Weekday", func(t1, t2 Time) bool { return t1.Weekday() == t2.Weekday() }}, 1138 {"ISOWeek", func(t1, t2 Time) bool { 1139 a1, b1 := t1.ISOWeek() 1140 a2, b2 := t2.ISOWeek() 1141 return a1 == a2 && b1 == b2 1142 }}, 1143 {"Clock", func(t1, t2 Time) bool { 1144 a1, b1, c1 := t1.Clock() 1145 a2, b2, c2 := t2.Clock() 1146 return a1 == a2 && b1 == b2 && c1 == c2 1147 }}, 1148 {"Hour", func(t1, t2 Time) bool { return t1.Hour() == t2.Hour() }}, 1149 {"Minute", func(t1, t2 Time) bool { return t1.Minute() == t2.Minute() }}, 1150 {"Second", func(t1, t2 Time) bool { return t1.Second() == t2.Second() }}, 1151 {"Nanosecond", func(t1, t2 Time) bool { return t1.Hour() == t2.Hour() }}, 1152 {"YearDay", func(t1, t2 Time) bool { return t1.YearDay() == t2.YearDay() }}, 1153 1154 // Using Equal since Add don't modify loc using "==" will cause a fail 1155 {"Add", func(t1, t2 Time) bool { return t1.Add(Hour).Equal(t2.Add(Hour)) }}, 1156 {"Sub", func(t1, t2 Time) bool { return t1.Sub(t2) == t2.Sub(t1) }}, 1157 1158 //Original caus for this test case bug 15852 1159 {"AddDate", func(t1, t2 Time) bool { return t1.AddDate(1991, 9, 3) == t2.AddDate(1991, 9, 3) }}, 1160 1161 {"UTC", func(t1, t2 Time) bool { return t1.UTC() == t2.UTC() }}, 1162 {"Local", func(t1, t2 Time) bool { return t1.Local() == t2.Local() }}, 1163 {"In", func(t1, t2 Time) bool { return t1.In(UTC) == t2.In(UTC) }}, 1164 1165 {"Local", func(t1, t2 Time) bool { return t1.Local() == t2.Local() }}, 1166 {"Zone", func(t1, t2 Time) bool { 1167 a1, b1 := t1.Zone() 1168 a2, b2 := t2.Zone() 1169 return a1 == a2 && b1 == b2 1170 }}, 1171 1172 {"Unix", func(t1, t2 Time) bool { return t1.Unix() == t2.Unix() }}, 1173 {"UnixNano", func(t1, t2 Time) bool { return t1.UnixNano() == t2.UnixNano() }}, 1174 1175 {"MarshalBinary", func(t1, t2 Time) bool { 1176 a1, b1 := t1.MarshalBinary() 1177 a2, b2 := t2.MarshalBinary() 1178 return bytes.Equal(a1, a2) && b1 == b2 1179 }}, 1180 {"GobEncode", func(t1, t2 Time) bool { 1181 a1, b1 := t1.GobEncode() 1182 a2, b2 := t2.GobEncode() 1183 return bytes.Equal(a1, a2) && b1 == b2 1184 }}, 1185 {"MarshalJSON", func(t1, t2 Time) bool { 1186 a1, b1 := t1.MarshalJSON() 1187 a2, b2 := t2.MarshalJSON() 1188 return bytes.Equal(a1, a2) && b1 == b2 1189 }}, 1190 {"MarshalText", func(t1, t2 Time) bool { 1191 a1, b1 := t1.MarshalText() 1192 a2, b2 := t2.MarshalText() 1193 return bytes.Equal(a1, a2) && b1 == b2 1194 }}, 1195 1196 {"Truncate", func(t1, t2 Time) bool { return t1.Truncate(Hour).Equal(t2.Truncate(Hour)) }}, 1197 {"Round", func(t1, t2 Time) bool { return t1.Round(Hour).Equal(t2.Round(Hour)) }}, 1198 1199 {"== Time{}", func(t1, t2 Time) bool { return (t1 == Time{}) == (t2 == Time{}) }}, 1200 } 1201 1202 func TestDefaultLoc(t *testing.T) { 1203 //This test verifyes that all Time's methods behaves identical if loc is set 1204 //as nil or UTC 1205 for _, tt := range defaultLocTests { 1206 t1 := Time{} 1207 t2 := Time{}.UTC() 1208 if !tt.f(t1, t2) { 1209 t.Errorf("Time{} and Time{}.UTC() behave differently for %s", tt.name) 1210 } 1211 } 1212 } 1213 1214 func BenchmarkNow(b *testing.B) { 1215 for i := 0; i < b.N; i++ { 1216 t = Now() 1217 } 1218 } 1219 1220 func BenchmarkNowUnixNano(b *testing.B) { 1221 for i := 0; i < b.N; i++ { 1222 u = Now().UnixNano() 1223 } 1224 } 1225 1226 func BenchmarkFormat(b *testing.B) { 1227 t := Unix(1265346057, 0) 1228 for i := 0; i < b.N; i++ { 1229 t.Format("Mon Jan 2 15:04:05 2006") 1230 } 1231 } 1232 1233 func BenchmarkFormatNow(b *testing.B) { 1234 // Like BenchmarkFormat, but easier, because the time zone 1235 // lookup cache is optimized for the present. 1236 t := Now() 1237 for i := 0; i < b.N; i++ { 1238 t.Format("Mon Jan 2 15:04:05 2006") 1239 } 1240 } 1241 1242 func BenchmarkMarshalJSON(b *testing.B) { 1243 t := Now() 1244 for i := 0; i < b.N; i++ { 1245 t.MarshalJSON() 1246 } 1247 } 1248 1249 func BenchmarkMarshalText(b *testing.B) { 1250 t := Now() 1251 for i := 0; i < b.N; i++ { 1252 t.MarshalText() 1253 } 1254 } 1255 1256 func BenchmarkParse(b *testing.B) { 1257 for i := 0; i < b.N; i++ { 1258 Parse(ANSIC, "Mon Jan 2 15:04:05 2006") 1259 } 1260 } 1261 1262 func BenchmarkParseDuration(b *testing.B) { 1263 for i := 0; i < b.N; i++ { 1264 ParseDuration("9007199254.740993ms") 1265 ParseDuration("9007199254740993ns") 1266 } 1267 } 1268 1269 func BenchmarkHour(b *testing.B) { 1270 t := Now() 1271 for i := 0; i < b.N; i++ { 1272 _ = t.Hour() 1273 } 1274 } 1275 1276 func BenchmarkSecond(b *testing.B) { 1277 t := Now() 1278 for i := 0; i < b.N; i++ { 1279 _ = t.Second() 1280 } 1281 } 1282 1283 func BenchmarkYear(b *testing.B) { 1284 t := Now() 1285 for i := 0; i < b.N; i++ { 1286 _ = t.Year() 1287 } 1288 } 1289 1290 func BenchmarkDay(b *testing.B) { 1291 t := Now() 1292 for i := 0; i < b.N; i++ { 1293 _ = t.Day() 1294 } 1295 } 1296 1297 func TestMarshalBinaryZeroTime(t *testing.T) { 1298 t0 := Time{} 1299 enc, err := t0.MarshalBinary() 1300 if err != nil { 1301 t.Fatal(err) 1302 } 1303 t1 := Now() // not zero 1304 if err := t1.UnmarshalBinary(enc); err != nil { 1305 t.Fatal(err) 1306 } 1307 if t1 != t0 { 1308 t.Errorf("t0=%#v\nt1=%#v\nwant identical structures", t0, t1) 1309 } 1310 } 1311 1312 // Issue 17720: Zero value of time.Month fails to print 1313 func TestZeroMonthString(t *testing.T) { 1314 if got, want := Month(0).String(), "%!Month(0)"; got != want { 1315 t.Errorf("zero month = %q; want %q", got, want) 1316 } 1317 } 1318 1319 func TestReadFileLimit(t *testing.T) { 1320 const zero = "/dev/zero" 1321 if _, err := os.Stat(zero); err != nil { 1322 t.Skip("skipping test without a /dev/zero") 1323 } 1324 _, err := ReadFile(zero) 1325 if err == nil || !strings.Contains(err.Error(), "is too large") { 1326 t.Errorf("readFile(%q) error = %v; want error containing 'is too large'", zero, err) 1327 } 1328 }