time_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2012 Google Inc. All Rights Reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package datastore
  5. import (
  6. "testing"
  7. "time"
  8. )
  9. func TestUnixMicro(t *testing.T) {
  10. // Test that all these time.Time values survive a round trip to unix micros.
  11. testCases := []time.Time{
  12. {},
  13. time.Date(2, 1, 1, 0, 0, 0, 0, time.UTC),
  14. time.Date(23, 1, 1, 0, 0, 0, 0, time.UTC),
  15. time.Date(234, 1, 1, 0, 0, 0, 0, time.UTC),
  16. time.Date(1000, 1, 1, 0, 0, 0, 0, time.UTC),
  17. time.Date(1600, 1, 1, 0, 0, 0, 0, time.UTC),
  18. time.Date(1700, 1, 1, 0, 0, 0, 0, time.UTC),
  19. time.Date(1800, 1, 1, 0, 0, 0, 0, time.UTC),
  20. time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC),
  21. time.Unix(-1e6, -1000),
  22. time.Unix(-1e6, 0),
  23. time.Unix(-1e6, +1000),
  24. time.Unix(-60, -1000),
  25. time.Unix(-60, 0),
  26. time.Unix(-60, +1000),
  27. time.Unix(-1, -1000),
  28. time.Unix(-1, 0),
  29. time.Unix(-1, +1000),
  30. time.Unix(0, -3000),
  31. time.Unix(0, -2000),
  32. time.Unix(0, -1000),
  33. time.Unix(0, 0),
  34. time.Unix(0, +1000),
  35. time.Unix(0, +2000),
  36. time.Unix(+60, -1000),
  37. time.Unix(+60, 0),
  38. time.Unix(+60, +1000),
  39. time.Unix(+1e6, -1000),
  40. time.Unix(+1e6, 0),
  41. time.Unix(+1e6, +1000),
  42. time.Date(1999, 12, 31, 23, 59, 59, 999000, time.UTC),
  43. time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
  44. time.Date(2006, 1, 2, 15, 4, 5, 678000, time.UTC),
  45. time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC),
  46. time.Date(3456, 1, 1, 0, 0, 0, 0, time.UTC),
  47. }
  48. for _, tc := range testCases {
  49. got := fromUnixMicro(toUnixMicro(tc))
  50. if !got.Equal(tc) {
  51. t.Errorf("got %q, want %q", got, tc)
  52. }
  53. }
  54. // Test that a time.Time that isn't an integral number of microseconds
  55. // is not perfectly reconstructed after a round trip.
  56. t0 := time.Unix(0, 123)
  57. t1 := fromUnixMicro(toUnixMicro(t0))
  58. if t1.Nanosecond()%1000 != 0 || t0.Nanosecond()%1000 == 0 {
  59. t.Errorf("quantization to µs: got %q with %d ns, started with %d ns", t1, t1.Nanosecond(), t0.Nanosecond())
  60. }
  61. }