You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

76 lines
2.4 KiB

  1. // Copyright 2014 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package datastore
  15. import (
  16. "testing"
  17. "time"
  18. )
  19. func TestUnixMicro(t *testing.T) {
  20. // Test that all these time.Time values survive a round trip to unix micros.
  21. testCases := []time.Time{
  22. {},
  23. time.Date(2, 1, 1, 0, 0, 0, 0, time.UTC),
  24. time.Date(23, 1, 1, 0, 0, 0, 0, time.UTC),
  25. time.Date(234, 1, 1, 0, 0, 0, 0, time.UTC),
  26. time.Date(1000, 1, 1, 0, 0, 0, 0, time.UTC),
  27. time.Date(1600, 1, 1, 0, 0, 0, 0, time.UTC),
  28. time.Date(1700, 1, 1, 0, 0, 0, 0, time.UTC),
  29. time.Date(1800, 1, 1, 0, 0, 0, 0, time.UTC),
  30. time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC),
  31. time.Unix(-1e6, -1000),
  32. time.Unix(-1e6, 0),
  33. time.Unix(-1e6, +1000),
  34. time.Unix(-60, -1000),
  35. time.Unix(-60, 0),
  36. time.Unix(-60, +1000),
  37. time.Unix(-1, -1000),
  38. time.Unix(-1, 0),
  39. time.Unix(-1, +1000),
  40. time.Unix(0, -3000),
  41. time.Unix(0, -2000),
  42. time.Unix(0, -1000),
  43. time.Unix(0, 0),
  44. time.Unix(0, +1000),
  45. time.Unix(0, +2000),
  46. time.Unix(+60, -1000),
  47. time.Unix(+60, 0),
  48. time.Unix(+60, +1000),
  49. time.Unix(+1e6, -1000),
  50. time.Unix(+1e6, 0),
  51. time.Unix(+1e6, +1000),
  52. time.Date(1999, 12, 31, 23, 59, 59, 999000, time.UTC),
  53. time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
  54. time.Date(2006, 1, 2, 15, 4, 5, 678000, time.UTC),
  55. time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC),
  56. time.Date(3456, 1, 1, 0, 0, 0, 0, time.UTC),
  57. }
  58. for _, tc := range testCases {
  59. got := fromUnixMicro(toUnixMicro(tc))
  60. if !got.Equal(tc) {
  61. t.Errorf("got %q, want %q", got, tc)
  62. }
  63. }
  64. // Test that a time.Time that isn't an integral number of microseconds
  65. // is not perfectly reconstructed after a round trip.
  66. t0 := time.Unix(0, 123)
  67. t1 := fromUnixMicro(toUnixMicro(t0))
  68. if t1.Nanosecond()%1000 != 0 || t0.Nanosecond()%1000 == 0 {
  69. t.Errorf("quantization to µs: got %q with %d ns, started with %d ns", t1, t1.Nanosecond(), t0.Nanosecond())
  70. }
  71. }