time_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Copyright 2022-2023 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. "github.com/stretchr/testify/assert"
  19. )
  20. func TestTimeToAndFromMilli(t *testing.T) {
  21. 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. assert.Equal(t, tt.t, time, "%d time from milli result mismatch:\n\nexp=%#v\n\ngot=%#v", i, tt.t, time)
  33. milli := TimeToUnixMilli(tt.t)
  34. assert.Equal(t, tt.m, milli, "%d time to milli result mismatch:\n\nexp=%#v\n\ngot=%#v", i, tt.m, milli)
  35. }
  36. }
  37. func TestFormatTime(t *testing.T) {
  38. date := time.Date(2020, time.January, 16, 2, 14, 24, 913000000, time.UTC)
  39. tests := []struct {
  40. format string
  41. want string
  42. wantErr bool
  43. }{
  44. {
  45. format: "YYYY-MM-dd HH:mm:ssSSS",
  46. want: "2020-01-16 02:14:24.913",
  47. wantErr: false,
  48. },
  49. {
  50. format: "YYYY-MM-dd T HH:mm:ss",
  51. want: "2020-01-16 T 02:14:24",
  52. wantErr: false,
  53. },
  54. {
  55. format: "YYY",
  56. want: "2020",
  57. wantErr: true,
  58. },
  59. }
  60. for _, tt := range tests {
  61. got, err := FormatTime(date, tt.format)
  62. if tt.wantErr {
  63. assert.Error(t, err)
  64. continue
  65. }
  66. assert.NoError(t, err)
  67. assert.Equal(t, tt.want, got)
  68. }
  69. }
  70. func TestParseTime(t *testing.T) {
  71. tests := []struct {
  72. d time.Time
  73. t string
  74. f string
  75. wantErr bool
  76. }{
  77. {
  78. time.Date(2020, time.January, 16, 2, 14, 24, 913000000, time.UTC),
  79. "2020-01-16 02:14:24.913",
  80. "YYYY-MM-dd HH:mm:ssSSS",
  81. false,
  82. },
  83. {
  84. time.Date(2020, time.January, 16, 2, 14, 24, 0, time.UTC),
  85. "2020-01-16 02:14:24",
  86. "YYYY-MM-dd HH:mm:ss",
  87. false,
  88. },
  89. {
  90. time.Date(2020, time.January, 16, 2, 14, 24, 0, time.UTC),
  91. "2020-01-16 02:14:24",
  92. "",
  93. false,
  94. },
  95. {
  96. time.Time{},
  97. "2020",
  98. "YYY",
  99. true,
  100. },
  101. }
  102. for _, tt := range tests {
  103. date, err := ParseTime(tt.t, tt.f)
  104. if tt.wantErr {
  105. assert.Error(t, err)
  106. continue
  107. }
  108. assert.NoError(t, err)
  109. assert.Equal(t, tt.d, date)
  110. }
  111. }
  112. func TestInterfaceToTime(t *testing.T) {
  113. tests := []struct {
  114. i interface{}
  115. f string
  116. want time.Time
  117. wantErr bool
  118. }{
  119. {
  120. "2022-04-13 06:22:32.233",
  121. "YYYY-MM-dd HH:mm:ssSSS",
  122. time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
  123. false,
  124. },
  125. {
  126. "2022-04-13 6:22:32.2",
  127. "YYYY-MM-dd h:m:sS",
  128. time.Date(2022, time.April, 13, 6, 22, 32, 200000000, time.UTC),
  129. false,
  130. },
  131. {
  132. "2022-04-13 6:22:32.23",
  133. "YYYY-MM-dd h:m:sSS",
  134. time.Date(2022, time.April, 13, 6, 22, 32, 230000000, time.UTC),
  135. false,
  136. },
  137. {
  138. "2022-04-13 Wed 06:22:32.233",
  139. "YYYY-MM-dd EEE HH:m:ssSSS",
  140. time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
  141. false,
  142. },
  143. {
  144. "2022-04-13 Wednesday 06:22:32.233",
  145. "YYYY-MM-dd EEEE HH:m:ssSSS",
  146. time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
  147. false,
  148. },
  149. {
  150. 1649830952233,
  151. "YYYY-MM-dd HH:mm:ssSSS",
  152. time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
  153. false,
  154. },
  155. {
  156. int64(1649830952233),
  157. "YYYY-MM-dd HH:mm:ssSSS",
  158. time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
  159. false,
  160. },
  161. {
  162. float64(1649830952233),
  163. "YYYY-MM-dd HH:mm:ssSSS",
  164. time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
  165. false,
  166. },
  167. {
  168. time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
  169. "YYYY-MM-dd HH:mm:ssSSS",
  170. time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
  171. false,
  172. },
  173. {
  174. "2022-04-13 06:22:32.233",
  175. "YYYy-MM-dd HH:mm:ssSSS",
  176. time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
  177. true,
  178. },
  179. {
  180. struct{}{},
  181. "YYYY-MM-dd HH:mm:ssSSS",
  182. time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
  183. true,
  184. },
  185. }
  186. for _, tt := range tests {
  187. got, err := InterfaceToTime(tt.i, tt.f)
  188. if tt.wantErr {
  189. assert.Error(t, err)
  190. continue
  191. }
  192. assert.NoError(t, err)
  193. assert.Equal(t, tt.want, got)
  194. }
  195. }
  196. func TestInterfaceToUnixMilli(t *testing.T) {
  197. tests := []struct {
  198. i interface{}
  199. f string
  200. want int64
  201. wantErr bool
  202. }{
  203. {
  204. "2022-04-13 06:22:32.233",
  205. "YYYY-MM-dd HH:mm:ssSSS",
  206. 1649830952233,
  207. false,
  208. },
  209. {
  210. 1649830952233,
  211. "YYYY-MM-dd HH:mm:ssSSS",
  212. 1649830952233,
  213. false,
  214. },
  215. {
  216. int64(1649830952233),
  217. "YYYY-MM-dd HH:mm:ssSSS",
  218. 1649830952233,
  219. false,
  220. },
  221. {
  222. float64(1649830952233),
  223. "YYYY-MM-dd HH:mm:ssSSS",
  224. 1649830952233,
  225. false,
  226. },
  227. {
  228. time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
  229. "YYYY-MM-dd HH:mm:ssSSS",
  230. 1649830952233,
  231. false,
  232. },
  233. {
  234. "2022-04-13 06:22:32.233",
  235. "YYYy-MM-dd HH:mm:ssSSS",
  236. 1649830952233,
  237. true,
  238. },
  239. {
  240. struct{}{},
  241. "YYYY-MM-dd HH:mm:ssSSS",
  242. 1649830952233,
  243. true,
  244. },
  245. }
  246. for _, tt := range tests {
  247. got, err := InterfaceToUnixMilli(tt.i, tt.f)
  248. if tt.wantErr {
  249. assert.Error(t, err)
  250. continue
  251. }
  252. assert.NoError(t, err)
  253. assert.Equal(t, tt.want, got)
  254. }
  255. }