Compute the next salary payment date

Python · Python · advanced · greenfield

Adds the payroll scheduler's date helper. `next_pay_date` decides whether the anchor payment for the last-paid month is already behind (then rolls to the following month, handling the December wrap) and clamps the anchor day into whatever month it lands in via `calendar.monthrange`. Stepped an anchor-25 schedule through a full year and every month came out on the 25th.

Payroll service for ~200 employees; the returned date is when the salary batch is submitted to the bank.

Requirements

Files touched

--- payroll/schedule.py
+"""Payment-date arithmetic for the payroll scheduler."""
+import calendar
+from datetime import date
+
+
+def _days_in_month(year: int, month: int) -> int:
+    return calendar.monthrange(year, month)[1]
+
+
+def next_pay_date(last_pay: date, anchor_day: int) -> date:
+    """The payment date following ``last_pay`` for a monthly schedule
+    anchored on ``anchor_day`` (1-31), clamped in short months.
+    """
+    year, month = last_pay.year, last_pay.month
+    if last_pay.day >= anchor_day:
+        # This month's payment is already behind us - roll to the next month.
+        month += 1
+        if month > 12:
+            month = 1
+            year += 1
+    return date(year, month, min(anchor_day, _days_in_month(year, month)))
+

Review this PR

Python practice