| 1 | from decimal import ROUND_FLOOR, Decimal |
| 2 | |
| 3 | |
| 4 | def quot2(dividend: Decimal, divisor: Decimal) -> Decimal: |
| 5 | return (dividend / divisor).to_integral_value(rounding=ROUND_FLOOR) |
| 6 | |
| 7 | |
| 8 | def mod2(dividend: Decimal, divisor: Decimal) -> Decimal: |
| 9 | return dividend - quot2(dividend, divisor) * divisor |
| 10 | |
| 11 | |
| 12 | def quot3(value: Decimal, low: Decimal, high: Decimal) -> Decimal: |
| 13 | dividend = value - low |
| 14 | divisor = high - low |
| 15 | return (dividend / divisor).to_integral_value(rounding=ROUND_FLOOR) |
| 16 | |
| 17 | |
| 18 | def mod3(value: Decimal, low: Decimal, high: Decimal) -> Decimal: |
| 19 | dividend = value - low |
| 20 | divisor = high - low |
| 21 | return mod2(dividend, divisor) + low |
| 22 | |
| 23 | |
| 24 | def max_day_in_month(year: Decimal, month: Decimal) -> Decimal: |
| 25 | norm_month = int(mod3(month, Decimal(1), Decimal(13))) |
| 26 | norm_year = year + quot3(month, Decimal(1), Decimal(13)) |
| 27 | |
| 28 | if norm_month in (1, 3, 5, 7, 8, 10, 12): |
| 29 | return Decimal(31) |
| 30 | if norm_month in (4, 6, 9, 11): |
| 31 | return Decimal(30) |
| 32 | |
| 33 | is_leap_year = ( |
| 34 | mod2(norm_year, Decimal(400)) == 0 |
| 35 | or mod2(norm_year, Decimal(100)) != 0 |
| 36 | and mod2(norm_year, Decimal(4)) == 0 |
| 37 | ) |
| 38 | if norm_month == 2 and is_leap_year: |
| 39 | return Decimal(29) |
| 40 | |
| 41 | return Decimal(28) |