github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekatime/date_test.go (about) 1 // Copyright © 2020. All rights reserved. 2 // Author: Ilya Stroy. 3 // Contacts: iyuryevich@pm.me, https://github.com/qioalice 4 // License: https://opensource.org/licenses/MIT 5 6 package ekatime_test 7 8 import ( 9 "encoding/json" 10 "fmt" 11 "testing" 12 13 "github.com/qioalice/ekago/v3/ekatime" 14 15 "github.com/stretchr/testify/require" 16 ) 17 18 func TestDate_String(t *testing.T) { 19 d1 := ekatime.NewDate(2020, 9, 1) 20 d2 := ekatime.NewDate(1812, 11, 24) 21 d3 := ekatime.NewDate(2100, 2, 15) 22 23 require.EqualValues(t, "2020/09/01", d1.String()) 24 require.EqualValues(t, "1812/11/24", d2.String()) 25 require.EqualValues(t, "2100/02/15", d3.String()) 26 } 27 28 func BenchmarkDate_String_CachedYear(b *testing.B) { 29 d := ekatime.NewDate(2020, 9, 1) 30 b.ResetTimer() 31 b.ReportAllocs() 32 for i := 0; i < b.N; i++ { 33 _ = d.String() 34 } 35 } 36 37 func BenchmarkDate_String_GeneratedYear(b *testing.B) { 38 d := ekatime.NewDate(2212, 8, 8) 39 b.ResetTimer() 40 b.ReportAllocs() 41 for i := 0; i < b.N; i++ { 42 _ = d.String() 43 } 44 } 45 46 func BenchmarkDate_FmtSprintf(b *testing.B) { 47 y, m, d := ekatime.NewDate(1914, 3, 9).Split() 48 b.ResetTimer() 49 b.ReportAllocs() 50 for i := 0; i < b.N; i++ { 51 _ = fmt.Sprintf("%04d/%02d/%02d", y, m, d) 52 } 53 } 54 55 func TestDate_ParseFrom(t *testing.T) { 56 bd1 := []byte("1814/05/12") // valid 57 bd2 := []byte("20200901") // valid 58 bd3 := []byte("2020-1120") // invalid 59 bd4 := []byte("202011-2") // invalid 60 bd5 := []byte("202011-20") // invalid 61 62 var ( 63 d1, d2, d3, d4, d5 ekatime.Date 64 ) 65 66 err1 := d1.ParseFrom(bd1) 67 err2 := d2.ParseFrom(bd2) 68 err3 := d3.ParseFrom(bd3) 69 err4 := d4.ParseFrom(bd4) 70 err5 := d5.ParseFrom(bd5) 71 72 require.Equal(t, ekatime.NewDate(1814, 5, 12).ToCmp(), d1.ToCmp()) 73 require.Equal(t, ekatime.NewDate(2020, 9, 1).ToCmp(), d2.ToCmp()) 74 75 require.NoError(t, err1) 76 require.NoError(t, err2) 77 require.Error(t, err3) 78 require.Error(t, err4) 79 require.Error(t, err5) 80 } 81 82 func BenchmarkDate_ParseFrom(b *testing.B) { 83 bd0 := []byte("2020/09/01") 84 var d ekatime.Date 85 b.ResetTimer() 86 b.ReportAllocs() 87 for i := 0; i < b.N; i++ { 88 _ = d.ParseFrom(bd0) 89 } 90 } 91 92 func TestDate_MarshalJSON(t *testing.T) { 93 var dt = struct { 94 D ekatime.Date `json:"d"` 95 }{ 96 D: ekatime.NewDate(2020, 9, 12), 97 } 98 d, err := json.Marshal(&dt) 99 100 require.NoError(t, err) 101 require.EqualValues(t, string(d), `{"d":"2020-09-12"}`) 102 } 103 104 func TestDate_Replace(t *testing.T) { 105 d := ekatime.NewDate(2021, ekatime.MONTH_FEBRUARY, 10) 106 107 d = d.Replace(2013, ekatime.MONTH_JANUARY, 2) 108 require.Equal(t, ekatime.NewDate(2013, ekatime.MONTH_JANUARY, 02).ToCmp(), d.ToCmp()) 109 110 d = d.Replace(2020, -2, 4) 111 require.Equal(t, ekatime.NewDate(2020, ekatime.MONTH_JANUARY, 4).ToCmp(), d.ToCmp()) 112 113 d = d.Replace(0, 0, -50) 114 require.Equal(t, ekatime.NewDate(2020, ekatime.MONTH_JANUARY, 4).ToCmp(), d.ToCmp()) 115 116 d = d.Replace(1899, 61, 31) 117 require.Equal(t, ekatime.NewDate(2020, ekatime.MONTH_JANUARY, 31).ToCmp(), d.ToCmp()) 118 119 d = d.Replace(2013, ekatime.MONTH_FEBRUARY, -2) 120 require.Equal(t, ekatime.NewDate(2013, ekatime.MONTH_JANUARY, 31).ToCmp(), d.ToCmp()) 121 122 d = d.Replace(2014, ekatime.MONTH_FEBRUARY, 30) 123 require.Equal(t, ekatime.NewDate(2014, ekatime.MONTH_JANUARY, 30).ToCmp(), d.ToCmp()) 124 125 d = d.Replace(2000, ekatime.MONTH_FEBRUARY, 29) 126 require.Equal(t, ekatime.NewDate(2000, ekatime.MONTH_FEBRUARY, 29).ToCmp(), d.ToCmp()) 127 128 d = d.Replace(2001, -1, -1) 129 require.Equal(t, ekatime.NewDate(2000, ekatime.MONTH_FEBRUARY, 29).ToCmp(), d.ToCmp()) 130 131 d = d.Replace(0, 0, 13) 132 require.Equal(t, ekatime.NewDate(2000, ekatime.MONTH_FEBRUARY, 13).ToCmp(), d.ToCmp()) 133 } 134 135 func TestDate_Add(t *testing.T) { 136 d := ekatime.NewDate(2021, ekatime.MONTH_FEBRUARY, 10) 137 138 d = d.Add(1, 2, 3) 139 require.Equal(t, ekatime.NewDate(2022, ekatime.MONTH_APRIL, 13).ToCmp(), d.ToCmp()) 140 141 d = d.Add(0, -1, -13) 142 require.Equal(t, ekatime.NewDate(2022, ekatime.MONTH_FEBRUARY, 28).ToCmp(), d.ToCmp()) 143 144 d = d.Add(0, 1, 3) 145 require.Equal(t, ekatime.NewDate(2022, ekatime.MONTH_MARCH, 31).ToCmp(), d.ToCmp()) 146 147 d = d.Add(0, 1, 0) 148 require.Equal(t, ekatime.NewDate(2022, ekatime.MONTH_MAY, 1).ToCmp(), d.ToCmp()) 149 150 d = d.Add(0, 127, 0) 151 require.Equal(t, ekatime.NewDate(2032, ekatime.MONTH_DECEMBER, 1).ToCmp(), d.ToCmp()) 152 } 153 154 func TestDate_Days(t *testing.T) { 155 var d ekatime.Date 156 157 d = ekatime.NewDate(2021, ekatime.MONTH_FEBRUARY, 11) 158 require.EqualValues(t, 42, d.DayOfYear()) 159 160 d = ekatime.NewDate(2021, ekatime.MONTH_DECEMBER, 31) 161 require.EqualValues(t, 365, d.DayOfYear()) 162 163 d = ekatime.NewDate(2021, ekatime.MONTH_JANUARY, 1) 164 require.EqualValues(t, 1, d.DayOfYear()) 165 166 d = ekatime.NewDate(2021, ekatime.MONTH_JANUARY, 0) 167 require.EqualValues(t, 366, d.DayOfYear()) 168 169 d = ekatime.NewDate(2021, ekatime.MONTH_SEPTEMBER, 10) 170 require.EqualValues(t, 253, d.DayOfYear()) 171 } 172 173 var TestDateWeekNumber = []struct { 174 y ekatime.Year 175 m ekatime.Month 176 d ekatime.Day 177 expectedWeekNumber ekatime.WeekNumber 178 }{ 179 {2004, 12, 27, 53}, 180 {2004, 12, 28, 53}, 181 {2004, 12, 29, 53}, 182 {2004, 12, 30, 53}, 183 {2004, 12, 31, 53}, 184 {2005, 1, 1, 53}, 185 {2005, 1, 2, 53}, 186 {2005, 1, 3, 1}, 187 {2005, 1, 4, 1}, 188 {2005, 1, 5, 1}, 189 {2005, 1, 6, 1}, 190 {2005, 12, 27, 52}, 191 {2005, 12, 28, 52}, 192 {2005, 12, 29, 52}, 193 {2005, 12, 30, 52}, 194 {2005, 12, 31, 52}, 195 {2006, 1, 1, 52}, 196 {2006, 1, 2, 1}, 197 {2006, 1, 3, 1}, 198 {2006, 1, 4, 1}, 199 {2006, 1, 5, 1}, 200 {2006, 1, 6, 1}, 201 {2006, 12, 27, 52}, 202 {2006, 12, 28, 52}, 203 {2006, 12, 29, 52}, 204 {2006, 12, 30, 52}, 205 {2006, 12, 31, 52}, 206 {2007, 1, 1, 1}, 207 {2007, 1, 2, 1}, 208 {2007, 1, 3, 1}, 209 {2007, 1, 4, 1}, 210 {2007, 1, 5, 1}, 211 {2007, 1, 6, 1}, 212 {2007, 12, 27, 52}, 213 {2007, 12, 28, 52}, 214 {2007, 12, 29, 52}, 215 {2007, 12, 30, 52}, 216 {2007, 12, 31, 1}, 217 {2008, 1, 1, 1}, 218 {2008, 1, 2, 1}, 219 {2008, 1, 3, 1}, 220 {2008, 1, 4, 1}, 221 {2008, 1, 5, 1}, 222 {2008, 1, 6, 1}, 223 {2008, 12, 26, 52}, 224 {2008, 12, 27, 52}, 225 {2008, 12, 28, 52}, 226 {2008, 12, 29, 1}, 227 {2008, 12, 30, 1}, 228 {2008, 12, 31, 1}, 229 {2009, 1, 1, 1}, 230 {2009, 1, 2, 1}, 231 {2009, 1, 3, 1}, 232 {2009, 1, 4, 1}, 233 {2009, 1, 5, 2}, 234 {2009, 1, 6, 2}, 235 {2009, 12, 27, 52}, 236 {2009, 12, 28, 53}, 237 {2009, 12, 29, 53}, 238 {2009, 12, 30, 53}, 239 {2009, 12, 31, 53}, 240 {2010, 1, 1, 53}, 241 {2010, 1, 2, 53}, 242 {2010, 1, 3, 53}, 243 {2010, 1, 4, 1}, 244 {2010, 1, 5, 1}, 245 {2010, 1, 6, 1}, 246 {2010, 12, 27, 52}, 247 {2010, 12, 28, 52}, 248 {2010, 12, 29, 52}, 249 {2010, 12, 30, 52}, 250 {2010, 12, 31, 52}, 251 {2011, 1, 1, 52}, 252 {2011, 1, 2, 52}, 253 {2011, 1, 3, 1}, 254 {2011, 1, 4, 1}, 255 {2011, 1, 5, 1}, 256 {2011, 1, 6, 1}, 257 {2011, 12, 27, 52}, 258 {2011, 12, 28, 52}, 259 {2011, 12, 29, 52}, 260 {2011, 12, 30, 52}, 261 {2011, 12, 31, 52}, 262 {2012, 1, 1, 52}, 263 {2012, 1, 2, 1}, 264 {2012, 1, 3, 1}, 265 {2012, 1, 4, 1}, 266 {2012, 1, 5, 1}, 267 {2012, 1, 6, 1}, 268 {2012, 12, 26, 52}, 269 {2012, 12, 27, 52}, 270 {2012, 12, 28, 52}, 271 {2012, 12, 29, 52}, 272 {2012, 12, 30, 52}, 273 {2012, 12, 31, 1}, 274 {2013, 1, 1, 1}, 275 {2013, 1, 2, 1}, 276 {2013, 1, 3, 1}, 277 {2013, 1, 4, 1}, 278 {2013, 1, 5, 1}, 279 {2013, 1, 6, 1}, 280 {2013, 12, 27, 52}, 281 {2013, 12, 28, 52}, 282 {2013, 12, 29, 52}, 283 {2013, 12, 30, 1}, 284 {2013, 12, 31, 1}, 285 {2014, 1, 1, 1}, 286 {2014, 1, 2, 1}, 287 {2014, 1, 3, 1}, 288 {2014, 1, 4, 1}, 289 {2014, 1, 5, 1}, 290 {2014, 1, 6, 2}, 291 {2014, 12, 27, 52}, 292 {2014, 12, 28, 52}, 293 {2014, 12, 29, 1}, 294 {2014, 12, 30, 1}, 295 {2014, 12, 31, 1}, 296 {2015, 1, 1, 1}, 297 {2015, 1, 2, 1}, 298 {2015, 1, 3, 1}, 299 {2015, 1, 4, 1}, 300 {2015, 1, 5, 2}, 301 {2015, 1, 6, 2}, 302 {2015, 12, 27, 52}, 303 {2015, 12, 28, 53}, 304 {2015, 12, 29, 53}, 305 {2015, 12, 30, 53}, 306 {2015, 12, 31, 53}, 307 {2016, 1, 1, 53}, 308 {2016, 1, 2, 53}, 309 {2016, 1, 3, 53}, 310 {2016, 1, 4, 1}, 311 {2016, 1, 5, 1}, 312 {2016, 1, 6, 1}, 313 {2016, 12, 26, 52}, 314 {2016, 12, 27, 52}, 315 {2016, 12, 28, 52}, 316 {2016, 12, 29, 52}, 317 {2016, 12, 30, 52}, 318 {2016, 12, 31, 52}, 319 {2017, 1, 1, 52}, 320 {2017, 1, 2, 1}, 321 {2017, 1, 3, 1}, 322 {2017, 1, 4, 1}, 323 {2017, 1, 5, 1}, 324 {2017, 1, 6, 1}, 325 {2017, 12, 27, 52}, 326 {2017, 12, 28, 52}, 327 {2017, 12, 29, 52}, 328 {2017, 12, 30, 52}, 329 {2017, 12, 31, 52}, 330 {2018, 1, 1, 1}, 331 {2018, 1, 2, 1}, 332 {2018, 1, 3, 1}, 333 {2018, 1, 4, 1}, 334 {2018, 1, 5, 1}, 335 {2018, 1, 6, 1}, 336 {2018, 12, 27, 52}, 337 {2018, 12, 28, 52}, 338 {2018, 12, 29, 52}, 339 {2018, 12, 30, 52}, 340 {2018, 12, 31, 1}, 341 {2019, 1, 1, 1}, 342 {2019, 1, 2, 1}, 343 {2019, 1, 3, 1}, 344 {2019, 1, 4, 1}, 345 {2019, 1, 5, 1}, 346 {2019, 1, 6, 1}, 347 {2019, 12, 27, 52}, 348 {2019, 12, 28, 52}, 349 {2019, 12, 29, 52}, 350 {2019, 12, 30, 1}, 351 {2019, 12, 31, 1}, 352 {2020, 1, 1, 1}, 353 {2020, 1, 2, 1}, 354 {2020, 1, 3, 1}, 355 {2020, 1, 4, 1}, 356 {2020, 1, 5, 1}, 357 {2020, 1, 6, 2}, 358 {2020, 12, 26, 52}, 359 {2020, 12, 27, 52}, 360 {2020, 12, 28, 53}, 361 {2020, 12, 29, 53}, 362 {2020, 12, 30, 53}, 363 {2020, 12, 31, 53}, 364 {2021, 1, 1, 53}, 365 {2021, 1, 2, 53}, 366 {2021, 1, 3, 53}, 367 {2021, 1, 4, 1}, 368 {2021, 1, 5, 1}, 369 {2021, 1, 6, 1}, 370 {2021, 12, 27, 52}, 371 {2021, 12, 28, 52}, 372 {2021, 12, 29, 52}, 373 {2021, 12, 30, 52}, 374 {2021, 12, 31, 52}, 375 {2022, 1, 1, 52}, 376 {2022, 1, 2, 52}, 377 {2022, 1, 3, 1}, 378 {2022, 1, 4, 1}, 379 {2022, 1, 5, 1}, 380 {2022, 1, 6, 1}, 381 {2022, 12, 27, 52}, 382 {2022, 12, 28, 52}, 383 {2022, 12, 29, 52}, 384 {2022, 12, 30, 52}, 385 {2022, 12, 31, 52}, 386 {2023, 1, 1, 52}, 387 {2023, 1, 2, 1}, 388 {2023, 1, 3, 1}, 389 {2023, 1, 4, 1}, 390 {2023, 1, 5, 1}, 391 {2023, 1, 6, 1}, 392 {2023, 12, 27, 52}, 393 {2023, 12, 28, 52}, 394 {2023, 12, 29, 52}, 395 {2023, 12, 30, 52}, 396 {2023, 12, 31, 52}, 397 {2024, 1, 1, 1}, 398 {2024, 1, 2, 1}, 399 {2024, 1, 3, 1}, 400 {2024, 1, 4, 1}, 401 {2024, 1, 5, 1}, 402 {2024, 1, 6, 1}, 403 {2024, 12, 26, 52}, 404 {2024, 12, 27, 52}, 405 {2024, 12, 28, 52}, 406 {2024, 12, 29, 52}, 407 {2024, 12, 30, 1}, 408 {2024, 12, 31, 1}, 409 {2025, 1, 1, 1}, 410 {2025, 1, 2, 1}, 411 {2025, 1, 3, 1}, 412 {2025, 1, 4, 1}, 413 {2025, 1, 5, 1}, 414 {2025, 1, 6, 2}, 415 {2025, 12, 27, 52}, 416 {2025, 12, 28, 52}, 417 {2025, 12, 29, 1}, 418 {2025, 12, 30, 1}, 419 {2025, 12, 31, 1}, 420 {2026, 1, 1, 1}, 421 {2026, 1, 2, 1}, 422 {2026, 1, 3, 1}, 423 {2026, 1, 4, 1}, 424 {2026, 1, 5, 2}, 425 {2026, 1, 6, 2}, 426 {2026, 12, 27, 52}, 427 {2026, 12, 28, 53}, 428 {2026, 12, 29, 53}, 429 {2026, 12, 30, 53}, 430 {2026, 12, 31, 53}, 431 {2027, 1, 1, 53}, 432 {2027, 1, 2, 53}, 433 {2027, 1, 3, 53}, 434 {2027, 1, 4, 1}, 435 {2027, 1, 5, 1}, 436 {2027, 1, 6, 1}, 437 {2027, 12, 27, 52}, 438 {2027, 12, 28, 52}, 439 {2027, 12, 29, 52}, 440 {2027, 12, 30, 52}, 441 {2027, 12, 31, 52}, 442 {2028, 1, 1, 52}, 443 {2028, 1, 2, 52}, 444 {2028, 1, 3, 1}, 445 {2028, 1, 4, 1}, 446 {2028, 1, 5, 1}, 447 {2028, 1, 6, 1}, 448 {2028, 12, 26, 52}, 449 {2028, 12, 27, 52}, 450 {2028, 12, 28, 52}, 451 {2028, 12, 29, 52}, 452 {2028, 12, 30, 52}, 453 {2028, 12, 31, 52}, 454 {2029, 1, 1, 1}, 455 {2029, 1, 2, 1}, 456 {2029, 1, 3, 1}, 457 {2029, 1, 4, 1}, 458 {2029, 1, 5, 1}, 459 {2029, 1, 6, 1}, 460 {2029, 12, 27, 52}, 461 {2029, 12, 28, 52}, 462 {2029, 12, 29, 52}, 463 {2029, 12, 30, 52}, 464 {2029, 12, 31, 1}, 465 {2030, 1, 1, 1}, 466 {2030, 1, 2, 1}, 467 {2030, 1, 3, 1}, 468 {2030, 1, 4, 1}, 469 {2030, 1, 5, 1}, 470 {2030, 1, 6, 1}, 471 {2030, 12, 27, 52}, 472 {2030, 12, 28, 52}, 473 {2030, 12, 29, 52}, 474 {2030, 12, 30, 1}, 475 {2030, 12, 31, 1}, 476 {2031, 1, 1, 1}, 477 {2031, 1, 2, 1}, 478 {2031, 1, 3, 1}, 479 {2031, 1, 4, 1}, 480 {2031, 1, 5, 1}, 481 {2031, 1, 6, 2}, 482 {2031, 12, 27, 52}, 483 {2031, 12, 28, 52}, 484 {2031, 12, 29, 1}, 485 {2031, 12, 30, 1}, 486 {2031, 12, 31, 1}, 487 {2032, 1, 1, 1}, 488 {2032, 1, 2, 1}, 489 {2032, 1, 3, 1}, 490 {2032, 1, 4, 1}, 491 {2032, 1, 5, 2}, 492 {2032, 1, 6, 2}, 493 {2032, 12, 26, 52}, 494 {2032, 12, 27, 53}, 495 {2032, 12, 28, 53}, 496 {2032, 12, 29, 53}, 497 {2032, 12, 30, 53}, 498 {2032, 12, 31, 53}, 499 {2033, 1, 1, 53}, 500 {2033, 1, 2, 53}, 501 {2033, 1, 3, 1}, 502 {2033, 1, 4, 1}, 503 {2033, 1, 5, 1}, 504 {2033, 1, 6, 1}, 505 {2033, 12, 27, 52}, 506 {2033, 12, 28, 52}, 507 {2033, 12, 29, 52}, 508 {2033, 12, 30, 52}, 509 {2033, 12, 31, 52}, 510 {2034, 1, 1, 52}, 511 {2034, 1, 2, 1}, 512 {2034, 1, 3, 1}, 513 {2034, 1, 4, 1}, 514 {2034, 1, 5, 1}, 515 {2034, 1, 6, 1}, 516 {2034, 12, 27, 52}, 517 {2034, 12, 28, 52}, 518 {2034, 12, 29, 52}, 519 {2034, 12, 30, 52}, 520 {2034, 12, 31, 52}, 521 {2035, 1, 1, 1}, 522 {2035, 1, 2, 1}, 523 {2035, 1, 3, 1}, 524 {2035, 1, 4, 1}, 525 {2035, 1, 5, 1}, 526 {2035, 1, 6, 1}, 527 } 528 529 func TestDate_ISOWeek(t *testing.T) { 530 for _, n := range TestDateWeekNumber { 531 require.Equal(t, n.expectedWeekNumber, ekatime.NewDate(n.y, n.m, n.d).ISOWeek()) 532 } 533 } 534 535 func TestNewDateFromDays(t *testing.T) { 536 require.Equal(t, 537 ekatime.NewDate(2021, ekatime.MONTH_SEPTEMBER, 10).ToCmp(), 538 ekatime.NewDateFromDayOfYear(2021, 253).ToCmp(), 539 ) 540 require.EqualValues(t, 541 253, 542 ekatime.NewDateFromDayOfYear(2021, 253).DayOfYear(), 543 ) 544 }