github.com/PandaGoAdmin/utils@v0.0.0-20211208134815-d5461603a00f/time_test.go (about) 1 package kgo 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "testing" 6 "time" 7 ) 8 9 func TestTime_UnixTime(t *testing.T) { 10 var res int64 11 12 res = KTime.UnixTime() 13 assert.Equal(t, 10, len(toStr(res))) 14 } 15 16 func BenchmarkTime_UnixTime(b *testing.B) { 17 b.ResetTimer() 18 for i := 0; i < b.N; i++ { 19 KTime.UnixTime() 20 } 21 } 22 23 func TestTime_MilliTime(t *testing.T) { 24 var res int64 25 26 res = KTime.MilliTime() 27 assert.Equal(t, 13, len(toStr(res))) 28 } 29 30 func BenchmarkTime_MilliTime(b *testing.B) { 31 b.ResetTimer() 32 for i := 0; i < b.N; i++ { 33 KTime.MilliTime() 34 } 35 } 36 37 func TestTime_MicroTime(t *testing.T) { 38 var res int64 39 40 res = KTime.MicroTime() 41 assert.Equal(t, 16, len(toStr(res))) 42 } 43 func BenchmarkTime_MicroTime(b *testing.B) { 44 b.ResetTimer() 45 for i := 0; i < b.N; i++ { 46 KTime.MicroTime() 47 } 48 } 49 50 func TestTime_Str2Timestruct(t *testing.T) { 51 var res time.Time 52 var err error 53 54 res, err = KTime.Str2Timestruct(strTime1) 55 assert.Nil(t, err) 56 assert.Equal(t, res.Year(), 2019) 57 assert.Equal(t, int(res.Month()), 7) 58 assert.Equal(t, res.Day(), 11) 59 } 60 61 func BenchmarkTime_Str2Timestruct(b *testing.B) { 62 b.ResetTimer() 63 for i := 0; i < b.N; i++ { 64 _, _ = KTime.Str2Timestruct(strTime1) 65 } 66 } 67 68 func TestTime_Str2Timestamp(t *testing.T) { 69 var res int64 70 var err error 71 72 res, err = KTime.Str2Timestamp(strTime1) 73 assert.Nil(t, err) 74 assert.Greater(t, res, int64(1)) 75 76 res, err = KTime.Str2Timestamp(strTime3, "01/02/2006 15:04:05") 77 assert.Nil(t, err) 78 assert.Greater(t, res, int64(1)) 79 80 //时间格式错误 81 res, err = KTime.Str2Timestamp(strTime2, "2006-01-02") 82 assert.NotNil(t, err) 83 } 84 85 func BenchmarkTime_Str2Timestamp(b *testing.B) { 86 b.ResetTimer() 87 for i := 0; i < b.N; i++ { 88 _, _ = KTime.Str2Timestamp(strTime3, "01/02/2006 15:04:05") 89 } 90 } 91 92 func TestTime_Date(t *testing.T) { 93 var res string 94 95 res = KTime.Date("Y-m-d H:i:s") 96 assert.NotEmpty(t, res) 97 98 res = KTime.Date("Y-m-d H:i:s", intTime1) 99 assert.NotEmpty(t, res) 100 101 res = KTime.Date("y-n-j H:i:s", int64(intTime1)) 102 assert.NotEmpty(t, res) 103 104 res = KTime.Date("m/d/y h-i-s", Kuptime) 105 assert.NotEmpty(t, res) 106 107 res = KTime.Date("Y-m-d H:i:s") 108 assert.NotEmpty(t, res) 109 110 res = KTime.Date("Y-m-d H:i:s", strHello) 111 assert.Empty(t, res) 112 113 //时间戳为0的时间点 114 res = KTime.Date("Y-m-d H:i:s", 0) 115 assert.NotEmpty(t, res) 116 //东八区 117 //assert.Equal(t, res, "1970-01-01 08:00:00") 118 } 119 120 func BenchmarkTime_Date(b *testing.B) { 121 b.ResetTimer() 122 for i := 0; i < b.N; i++ { 123 KTime.Date("Y-m-d H:i:s") 124 } 125 } 126 127 func TestTime_CheckDate(t *testing.T) { 128 var res bool 129 130 res = KTime.CheckDate(2019, 7, 31) 131 assert.True(t, res) 132 133 res = KTime.CheckDate(2019, 2, 31) 134 assert.False(t, res) 135 136 res = KTime.CheckDate(2019, 0, 31) 137 assert.False(t, res) 138 139 res = KTime.CheckDate(2019, 4, 31) 140 assert.False(t, res) 141 142 res = KTime.CheckDate(2008, 2, 30) 143 assert.False(t, res) 144 } 145 146 func BenchmarkTime_CheckDate(b *testing.B) { 147 b.ResetTimer() 148 for i := 0; i < b.N; i++ { 149 KTime.CheckDate(2019, 7, 31) 150 } 151 } 152 153 func TestTime_Sleep(t *testing.T) { 154 var t0, t1, t2, res int64 155 t0 = 1 156 t1 = KTime.UnixTime() 157 KTime.Sleep(t0) 158 t2 = KTime.UnixTime() 159 res = t2 - t1 160 assert.GreaterOrEqual(t, res, t0) 161 } 162 163 func TestTime_Usleep(t *testing.T) { 164 var t0, t1, t2, res int64 165 t0 = 100 166 t1 = KTime.MicroTime() 167 KTime.Usleep(t0) 168 t2 = KTime.MicroTime() 169 res = t2 - t1 170 assert.GreaterOrEqual(t, res, t0) 171 } 172 173 func TestTime_ServiceStartime(t *testing.T) { 174 var res int64 175 res = KTime.ServiceStartime() 176 assert.Greater(t, res, int64(1)) 177 } 178 179 func BenchmarkTime_ServiceStartime(b *testing.B) { 180 b.ResetTimer() 181 for i := 0; i < b.N; i++ { 182 KTime.ServiceStartime() 183 } 184 } 185 186 func TestTime_ServiceUptime(t *testing.T) { 187 var res time.Duration 188 189 res = KTime.ServiceUptime() 190 assert.Greater(t, int64(res), int64(1)) 191 } 192 193 func BenchmarkTime_ServiceUptime(b *testing.B) { 194 b.ResetTimer() 195 for i := 0; i < b.N; i++ { 196 KTime.ServiceUptime() 197 } 198 } 199 200 func TestTime_GetMonthDays(t *testing.T) { 201 var res int 202 var tests = []struct { 203 month int 204 year int 205 expected int 206 }{ 207 {3, 1970, 31}, 208 {1, 2009, 31}, 209 {0, 2009, 0}, 210 {2, 2009, 28}, 211 {2, 2016, 29}, 212 {2, 1900, 28}, 213 {4, 2020, 30}, 214 {2, 1600, 29}, 215 } 216 for _, test := range tests { 217 actual := KTime.GetMonthDays(test.month, test.year) 218 assert.Equal(t, actual, test.expected) 219 } 220 221 res = KTime.GetMonthDays(2) 222 assert.Greater(t, res, 0) 223 } 224 225 func BenchmarkTime_GetMonthDays(b *testing.B) { 226 b.ResetTimer() 227 for i := 0; i < b.N; i++ { 228 KTime.GetMonthDays(3, 1970) 229 } 230 } 231 232 func TestTime_YearMonthDay(t *testing.T) { 233 var y1, m1, d1, y2, m2, d2 int 234 235 y1 = KTime.Year() 236 m1 = KTime.Month() 237 d1 = KTime.Day() 238 239 y2 = KTime.Year(Kuptime) 240 m2 = KTime.Month(Kuptime) 241 d2 = KTime.Day(Kuptime) 242 243 assert.Equal(t, y1, y2) 244 assert.Equal(t, m1, m2) 245 assert.Equal(t, d1, d2) 246 } 247 248 func BenchmarkTime_Year(b *testing.B) { 249 b.ResetTimer() 250 for i := 0; i < b.N; i++ { 251 KTime.Year(Kuptime) 252 } 253 } 254 255 func BenchmarkTime_Month(b *testing.B) { 256 b.ResetTimer() 257 for i := 0; i < b.N; i++ { 258 KTime.Month(Kuptime) 259 } 260 } 261 262 func BenchmarkTime_Day(b *testing.B) { 263 b.ResetTimer() 264 for i := 0; i < b.N; i++ { 265 KTime.Year(Kuptime) 266 } 267 } 268 269 func TestTime_HourMinuteSecond(t *testing.T) { 270 var h1, m1, s1, h2, m2, s2 int 271 272 h1 = KTime.Hour() 273 m1 = KTime.Minute() 274 s1 = KTime.Second() 275 assert.GreaterOrEqual(t, h1, 0) 276 assert.GreaterOrEqual(t, m1, 0) 277 assert.GreaterOrEqual(t, s1, 0) 278 279 h2 = KTime.Hour(myDate1) 280 m2 = KTime.Minute(myDate1) 281 s2 = KTime.Second(myDate1) 282 assert.Equal(t, h2, 23) 283 assert.Equal(t, m2, 4) 284 assert.Equal(t, s2, 35) 285 } 286 287 func BenchmarkTime_Hour(b *testing.B) { 288 b.ResetTimer() 289 for i := 0; i < b.N; i++ { 290 KTime.Hour(Kuptime) 291 } 292 } 293 294 func BenchmarkTime_Minute(b *testing.B) { 295 b.ResetTimer() 296 for i := 0; i < b.N; i++ { 297 KTime.Minute(Kuptime) 298 } 299 } 300 301 func BenchmarkTime_Second(b *testing.B) { 302 b.ResetTimer() 303 for i := 0; i < b.N; i++ { 304 KTime.Second(Kuptime) 305 } 306 } 307 308 func TestTime_StartOfDay(t *testing.T) { 309 var res time.Time 310 311 res = KTime.StartOfDay(myDate1) 312 str := KTime.Date("Y-m-d H:i:s", res) 313 assert.Equal(t, str, "2020-03-10 00:00:00") 314 } 315 316 func BenchmarkTime_StartOfDay(b *testing.B) { 317 b.ResetTimer() 318 for i := 0; i < b.N; i++ { 319 KTime.StartOfDay(myDate1) 320 } 321 } 322 323 func TestTime_EndOfDay(t *testing.T) { 324 var res time.Time 325 326 res = KTime.EndOfDay(myDate1) 327 str := KTime.Date("Y-m-d H:i:s", res) 328 assert.Equal(t, str, "2020-03-10 23:59:59") 329 } 330 331 func BenchmarkTime_EndOfDay(b *testing.B) { 332 b.ResetTimer() 333 for i := 0; i < b.N; i++ { 334 KTime.EndOfDay(myDate1) 335 } 336 } 337 338 func TestTime_StartOfMonth(t *testing.T) { 339 var res time.Time 340 341 res = KTime.StartOfMonth(myDate1) 342 str := KTime.Date("Y-m-d H:i:s", res) 343 assert.Equal(t, str, "2020-03-01 00:00:00") 344 } 345 346 func BenchmarkTime_StartOfMonth(b *testing.B) { 347 b.ResetTimer() 348 for i := 0; i < b.N; i++ { 349 KTime.StartOfMonth(myDate1) 350 } 351 } 352 353 func TestTime_EndOfMonth(t *testing.T) { 354 var res time.Time 355 356 res = KTime.EndOfMonth(myDate1) 357 str := KTime.Date("Y-m-d H:i:s", res) 358 assert.Equal(t, str, "2020-03-31 23:59:59") 359 } 360 361 func BenchmarkTime_EndOfMonth(b *testing.B) { 362 b.ResetTimer() 363 for i := 0; i < b.N; i++ { 364 KTime.EndOfMonth(myDate1) 365 } 366 } 367 368 func TestTime_StartOfYear(t *testing.T) { 369 var res time.Time 370 371 res = KTime.StartOfYear(myDate1) 372 str := KTime.Date("Y-m-d H:i:s", res) 373 assert.Equal(t, str, "2020-01-01 00:00:00") 374 } 375 376 func BenchmarkTime_StartOfYear(b *testing.B) { 377 b.ResetTimer() 378 for i := 0; i < b.N; i++ { 379 KTime.StartOfYear(myDate1) 380 } 381 } 382 383 func TestTime_EndOfYear(t *testing.T) { 384 var res time.Time 385 386 res = KTime.EndOfYear(myDate1) 387 str := KTime.Date("Y-m-d H:i:s", res) 388 assert.Equal(t, str, "2020-12-31 23:59:59") 389 } 390 391 func BenchmarkTime_EndOfYear(b *testing.B) { 392 b.ResetTimer() 393 for i := 0; i < b.N; i++ { 394 KTime.EndOfYear(myDate1) 395 } 396 } 397 398 func TestTime_StartOfWeek(t *testing.T) { 399 var res time.Time 400 401 res = KTime.StartOfWeek(myDate1) 402 str := KTime.Date("Y-m-d H:i:s", res) 403 assert.Equal(t, str, "2020-03-09 00:00:00") 404 405 res = KTime.StartOfWeek(myDate2, time.Tuesday) 406 str = KTime.Date("Y-m-d H:i:s", res) 407 assert.Equal(t, str, "2020-03-03 00:00:00") 408 } 409 410 func BenchmarkTime_StartOfWeek(b *testing.B) { 411 b.ResetTimer() 412 for i := 0; i < b.N; i++ { 413 KTime.StartOfWeek(myDate1) 414 } 415 } 416 417 func TestTime_EndOfWeek(t *testing.T) { 418 var res time.Time 419 420 res = KTime.EndOfWeek(myDate1) 421 str := KTime.Date("Y-m-d H:i:s", res) 422 assert.Equal(t, str, "2020-03-15 23:59:59") 423 424 res = KTime.EndOfWeek(myDate2, time.Tuesday) 425 str = KTime.Date("Y-m-d H:i:s", res) 426 assert.Equal(t, str, "2020-03-09 23:59:59") 427 } 428 429 func BenchmarkTime_EndOfWeek(b *testing.B) { 430 b.ResetTimer() 431 for i := 0; i < b.N; i++ { 432 KTime.EndOfWeek(myDate1) 433 } 434 } 435 436 func TestTime_DaysBetween(t *testing.T) { 437 days := KTime.DaysBetween(myDate1, myDate3) 438 assert.Equal(t, days, 107) 439 440 days = KTime.DaysBetween(myDate3, myDate1) 441 assert.Equal(t, days, -107) 442 } 443 444 func BenchmarkTime_DaysBetween(b *testing.B) { 445 b.ResetTimer() 446 for i := 0; i < b.N; i++ { 447 KTime.DaysBetween(myDate1, myDate3) 448 } 449 } 450 451 func TestTime_IsDate2time(t *testing.T) { 452 var tests = []struct { 453 param string 454 expected bool 455 }{ 456 {"", false}, 457 {"hello", false}, 458 {"0000", true}, 459 {"1970", true}, 460 {"1970-01-01", true}, 461 {"1970-01-01 00:00:01", true}, 462 {"1971-01-01 00:00:01", true}, 463 {"1990-01", true}, 464 {"1990/01", true}, 465 {"1990-01-02", true}, 466 {"1990/01/02", true}, 467 {"1990-01-02 03", true}, 468 {"1990/01/02 03", true}, 469 {"1990-01-02 03:14", true}, 470 {"1990/01/02 03:14", true}, 471 {"1990-01-02 03:14:59", true}, 472 {"1990/01/02 03:14:59", true}, 473 {"2990-00-00 03:14:59", false}, 474 } 475 for _, test := range tests { 476 actual, tim := KTime.IsDate2time(test.param) 477 assert.Equal(t, actual, test.expected) 478 if actual { 479 if toInt(test.param) > 1970 { 480 assert.Greater(t, tim, int64(0)) 481 } 482 } 483 } 484 } 485 486 func BenchmarkTime_IsDate2time(b *testing.B) { 487 b.ResetTimer() 488 for i := 0; i < b.N; i++ { 489 _, _ = KTime.IsDate2time(strTime7) 490 } 491 }