| 553 | ra(TypeError, lambda: td(microseconds='1')) |
| 554 | |
| 555 | def test_computations(self): |
| 556 | eq = self.assertEqual |
| 557 | td = timedelta |
| 558 | |
| 559 | a = td(7) # One week |
| 560 | b = td(0, 60) # One minute |
| 561 | c = td(0, 0, 1000) # One millisecond |
| 562 | eq(a+b+c, td(7, 60, 1000)) |
| 563 | eq(a-b, td(6, 24*3600 - 60)) |
| 564 | eq(b.__rsub__(a), td(6, 24*3600 - 60)) |
| 565 | eq(-a, td(-7)) |
| 566 | eq(+a, td(7)) |
| 567 | eq(-b, td(-1, 24*3600 - 60)) |
| 568 | eq(-c, td(-1, 24*3600 - 1, 999000)) |
| 569 | eq(abs(a), a) |
| 570 | eq(abs(-a), a) |
| 571 | eq(td(6, 24*3600), a) |
| 572 | eq(td(0, 0, 60*1000000), b) |
| 573 | eq(a*10, td(70)) |
| 574 | eq(a*10, 10*a) |
| 575 | eq(a*10, 10*a) |
| 576 | eq(b*10, td(0, 600)) |
| 577 | eq(10*b, td(0, 600)) |
| 578 | eq(b*10, td(0, 600)) |
| 579 | eq(c*10, td(0, 0, 10000)) |
| 580 | eq(10*c, td(0, 0, 10000)) |
| 581 | eq(c*10, td(0, 0, 10000)) |
| 582 | eq(a*-1, -a) |
| 583 | eq(b*-2, -b-b) |
| 584 | eq(c*-2, -c+-c) |
| 585 | eq(b*(60*24), (b*60)*24) |
| 586 | eq(b*(60*24), (60*b)*24) |
| 587 | eq(c*1000, td(0, 1)) |
| 588 | eq(1000*c, td(0, 1)) |
| 589 | eq(a//7, td(1)) |
| 590 | eq(b//10, td(0, 6)) |
| 591 | eq(c//1000, td(0, 0, 1)) |
| 592 | eq(a//10, td(0, 7*24*360)) |
| 593 | eq(a//3600000, td(0, 0, 7*24*1000)) |
| 594 | eq(a/0.5, td(14)) |
| 595 | eq(b/0.5, td(0, 120)) |
| 596 | eq(a/7, td(1)) |
| 597 | eq(b/10, td(0, 6)) |
| 598 | eq(c/1000, td(0, 0, 1)) |
| 599 | eq(a/10, td(0, 7*24*360)) |
| 600 | eq(a/3600000, td(0, 0, 7*24*1000)) |
| 601 | |
| 602 | # Multiplication by float |
| 603 | us = td(microseconds=1) |
| 604 | eq((3*us) * 0.5, 2*us) |
| 605 | eq((5*us) * 0.5, 2*us) |
| 606 | eq(0.5 * (3*us), 2*us) |
| 607 | eq(0.5 * (5*us), 2*us) |
| 608 | eq((-3*us) * 0.5, -2*us) |
| 609 | eq((-5*us) * 0.5, -2*us) |
| 610 | |
| 611 | # Issue #23521 |
| 612 | eq(td(seconds=1) * 0.123456, td(microseconds=123456)) |