Add a duration to the instance. If we're adding units of variable length (i.e., years, months), move forward from current time, otherwise move forward from utc, for accuracy when moving across DST boundaries.
(
self,
years: int = 0,
months: int = 0,
weeks: int = 0,
days: int = 0,
hours: int = 0,
minutes: int = 0,
seconds: float = 0,
microseconds: int = 0,
)
| 557 | # ADDITIONS AND SUBSTRACTIONS |
| 558 | |
| 559 | def add( |
| 560 | self, |
| 561 | years: int = 0, |
| 562 | months: int = 0, |
| 563 | weeks: int = 0, |
| 564 | days: int = 0, |
| 565 | hours: int = 0, |
| 566 | minutes: int = 0, |
| 567 | seconds: float = 0, |
| 568 | microseconds: int = 0, |
| 569 | ) -> Self: |
| 570 | """ |
| 571 | Add a duration to the instance. |
| 572 | |
| 573 | If we're adding units of variable length (i.e., years, months), |
| 574 | move forward from current time, otherwise move forward from utc, for accuracy |
| 575 | when moving across DST boundaries. |
| 576 | """ |
| 577 | units_of_variable_length = any([years, months, weeks, days]) |
| 578 | |
| 579 | current_dt = datetime.datetime( |
| 580 | self.year, |
| 581 | self.month, |
| 582 | self.day, |
| 583 | self.hour, |
| 584 | self.minute, |
| 585 | self.second, |
| 586 | self.microsecond, |
| 587 | ) |
| 588 | if not units_of_variable_length: |
| 589 | offset = self.utcoffset() |
| 590 | if offset: |
| 591 | current_dt = current_dt - offset |
| 592 | |
| 593 | dt = add_duration( |
| 594 | current_dt, |
| 595 | years=years, |
| 596 | months=months, |
| 597 | weeks=weeks, |
| 598 | days=days, |
| 599 | hours=hours, |
| 600 | minutes=minutes, |
| 601 | seconds=seconds, |
| 602 | microseconds=microseconds, |
| 603 | ) |
| 604 | |
| 605 | if units_of_variable_length or self.tz is None: |
| 606 | return self.__class__.create( |
| 607 | dt.year, |
| 608 | dt.month, |
| 609 | dt.day, |
| 610 | dt.hour, |
| 611 | dt.minute, |
| 612 | dt.second, |
| 613 | dt.microsecond, |
| 614 | tz=self.tz, |
| 615 | ) |
| 616 |