| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from datetime import datetime |
| 4 |
from decimal import ROUND_DOWN, ROUND_HALF_UP, Decimal |
| 5 |
from typing import TYPE_CHECKING |
| 6 |
|
| 7 |
from isoduration.operations.util import max_day_in_month, mod2, mod3, quot2, quot3 |
| 8 |
|
| 9 |
if TYPE_CHECKING: |
| 10 |
from isoduration.types import Duration |
| 11 |
|
| 12 |
|
| 13 |
def add(start: datetime, duration: Duration) -> datetime: |
| 14 |
""" |
| 15 |
https://www.w3.org/TR/xmlschema-2/#adding-durations-to-dateTimes |
| 16 |
""" |
| 17 |
|
| 18 |
|
| 19 |
temp = Decimal(start.month) + duration.date.months |
| 20 |
end_month = mod3(temp, Decimal(1), Decimal(13)) |
| 21 |
carry = quot3(temp, Decimal(1), Decimal(13)) |
| 22 |
|
| 23 |
|
| 24 |
end_year = Decimal(start.year) + duration.date.years + carry |
| 25 |
|
| 26 |
|
| 27 |
end_tzinfo = start.tzinfo |
| 28 |
|
| 29 |
|
| 30 |
seconds = duration.time.seconds.to_integral_value(rounding=ROUND_DOWN) |
| 31 |
microseconds = (duration.time.seconds - seconds) * Decimal("1e6") |
| 32 |
|
| 33 |
temp = Decimal(start.microsecond) + microseconds |
| 34 |
end_microsecond = mod2(temp, Decimal("1e6")) |
| 35 |
carry = quot2(temp, Decimal("1e6")) |
| 36 |
|
| 37 |
|
| 38 |
temp = Decimal(start.second) + seconds + carry |
| 39 |
end_second = mod2(temp, Decimal("60")) |
| 40 |
carry = quot2(temp, Decimal("60")) |
| 41 |
|
| 42 |
|
| 43 |
temp = Decimal(start.minute) + duration.time.minutes + carry |
| 44 |
end_minute = mod2(temp, Decimal("60")) |
| 45 |
carry = quot2(temp, Decimal("60")) |
| 46 |
|
| 47 |
|
| 48 |
temp = Decimal(start.hour) + duration.time.hours + carry |
| 49 |
end_hour = mod2(temp, Decimal("24")) |
| 50 |
carry = quot2(temp, Decimal("24")) |
| 51 |
|
| 52 |
|
| 53 |
end_max_day_in_month = max_day_in_month(end_year, end_month) |
| 54 |
|
| 55 |
if start.day > end_max_day_in_month: |
| 56 |
temp = end_max_day_in_month |
| 57 |
else: |
| 58 |
temp = Decimal(start.day) |
| 59 |
|
| 60 |
end_day = temp + duration.date.days + (7 * duration.date.weeks) + carry |
| 61 |
|
| 62 |
while True: |
| 63 |
if end_day < 1: |
| 64 |
end_day += max_day_in_month(end_year, end_month - 1) |
| 65 |
carry = Decimal(-1) |
| 66 |
elif end_day > max_day_in_month(end_year, end_month): |
| 67 |
end_day -= max_day_in_month(end_year, end_month) |
| 68 |
carry = Decimal(1) |
| 69 |
else: |
| 70 |
break |
| 71 |
|
| 72 |
temp = end_month + carry |
| 73 |
end_month = mod3(temp, Decimal(1), Decimal(13)) |
| 74 |
end_year = end_year + quot3(temp, Decimal(1), Decimal(13)) |
| 75 |
|
| 76 |
return datetime( |
| 77 |
year=int(end_year.to_integral_value(ROUND_HALF_UP)), |
| 78 |
month=int(end_month.to_integral_value(ROUND_HALF_UP)), |
| 79 |
day=int(end_day.to_integral_value(ROUND_HALF_UP)), |
| 80 |
hour=int(end_hour.to_integral_value(ROUND_HALF_UP)), |
| 81 |
minute=int(end_minute.to_integral_value(ROUND_HALF_UP)), |
| 82 |
second=int(end_second.to_integral_value(ROUND_HALF_UP)), |
| 83 |
microsecond=int(end_microsecond.to_integral_value(ROUND_HALF_UP)), |
| 84 |
tzinfo=end_tzinfo, |
| 85 |
) |