time_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2022 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. "reflect"
  17. "testing"
  18. "time"
  19. )
  20. func TestDateToAndFromMilli(t *testing.T) {
  21. var tests = []struct {
  22. m int64
  23. t time.Time
  24. }{
  25. {int64(1579140864913), time.Date(2020, time.January, 16, 2, 14, 24, 913000000, time.UTC)},
  26. {int64(4913), time.Date(1970, time.January, 1, 0, 0, 4, 913000000, time.UTC)},
  27. {int64(2579140864913), time.Date(2051, time.September, 24, 4, 1, 4, 913000000, time.UTC)},
  28. {int64(-1579140864913), time.Date(1919, time.December, 17, 21, 45, 35, 87000000, time.UTC)},
  29. }
  30. for i, tt := range tests {
  31. time := TimeFromUnixMilli(tt.m)
  32. if !time.Equal(tt.t) {
  33. t.Errorf("%d time from milli result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.t, time)
  34. }
  35. milli := TimeToUnixMilli(tt.t)
  36. if tt.m != milli {
  37. t.Errorf("%d time to milli result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.m, milli)
  38. }
  39. }
  40. }
  41. func TestFormatTime(t *testing.T) {
  42. type args struct {
  43. time time.Time
  44. f string
  45. }
  46. tests := []struct {
  47. name string
  48. args args
  49. want string
  50. wantErr bool
  51. }{
  52. {
  53. name: "test1",
  54. args: args{
  55. time: time.Date(2020, time.January, 16, 2, 14, 24, 913000000, time.UTC),
  56. f: "YYYY-MM-dd HH:mm:ssSSS",
  57. },
  58. want: "2020-01-16 02:14:24.913",
  59. wantErr: false,
  60. },
  61. {
  62. name: "test1",
  63. args: args{
  64. time: time.Date(2020, time.January, 16, 2, 14, 24, 913000000, time.UTC),
  65. f: "YYYY-MM-dd T HH:mm:ss",
  66. },
  67. want: "2020-01-16 T 02:14:24",
  68. wantErr: false,
  69. },
  70. }
  71. for _, tt := range tests {
  72. t.Run(tt.name, func(t *testing.T) {
  73. got, err := FormatTime(tt.args.time, tt.args.f)
  74. if (err != nil) != tt.wantErr {
  75. t.Errorf("FormatTime() error = %v, wantErr %v", err, tt.wantErr)
  76. return
  77. }
  78. if got != tt.want {
  79. t.Errorf("FormatTime() got = %v, want %v", got, tt.want)
  80. }
  81. })
  82. }
  83. }
  84. func TestInterfaceToTime(t *testing.T) {
  85. type args struct {
  86. i interface{}
  87. format string
  88. }
  89. tests := []struct {
  90. name string
  91. args args
  92. want time.Time
  93. wantErr bool
  94. }{
  95. {
  96. name: "test string",
  97. args: args{
  98. i: "2022-04-13 06:22:32.233",
  99. format: "YYYY-MM-dd HH:mm:ssSSS",
  100. },
  101. want: time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
  102. wantErr: false,
  103. },
  104. }
  105. for _, tt := range tests {
  106. t.Run(tt.name, func(t *testing.T) {
  107. got, err := InterfaceToTime(tt.args.i, tt.args.format)
  108. if (err != nil) != tt.wantErr {
  109. t.Errorf("InterfaceToTime() error = %v, wantErr %v", err, tt.wantErr)
  110. return
  111. }
  112. if !reflect.DeepEqual(got, tt.want) {
  113. t.Errorf("InterfaceToTime() got = %v, want %v", got, tt.want)
  114. }
  115. })
  116. }
  117. }