time_test.go 6.3 KB

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