time_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  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 cast
  15. import (
  16. "testing"
  17. "time"
  18. )
  19. func TestDateToAndFromMilli(t *testing.T) {
  20. var tests = []struct {
  21. m int64
  22. t time.Time
  23. }{
  24. {int64(1579140864913), time.Date(2020, time.January, 16, 2, 14, 24, 913000000, time.UTC)},
  25. {int64(4913), time.Date(1970, time.January, 1, 0, 0, 4, 913000000, time.UTC)},
  26. {int64(2579140864913), time.Date(2051, time.September, 24, 4, 1, 4, 913000000, time.UTC)},
  27. {int64(-1579140864913), time.Date(1919, time.December, 17, 21, 45, 35, 87000000, time.UTC)},
  28. }
  29. for i, tt := range tests {
  30. time := TimeFromUnixMilli(tt.m)
  31. if !time.Equal(tt.t) {
  32. t.Errorf("%d time from milli result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.t, time)
  33. }
  34. milli := TimeToUnixMilli(tt.t)
  35. if tt.m != milli {
  36. t.Errorf("%d time to milli result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.m, milli)
  37. }
  38. }
  39. }