oa_confirmation_date.py 834 B

1234567891011121314151617181920212223242526272829
  1. """转正套件:入职日 + 试用期月数 → 建议转正日(YYYY-MM-DD)。"""
  2. from __future__ import annotations
  3. import calendar
  4. import re
  5. from datetime import date
  6. from typing import Optional
  7. _YMD = re.compile(r"^(\d{4})-(\d{2})-(\d{2})$")
  8. def add_months_ymd(hire_ymd: str, months: int) -> Optional[str]:
  9. s = (hire_ymd or "").strip()
  10. m = _YMD.match(s)
  11. if not m:
  12. return None
  13. if not isinstance(months, int) or isinstance(months, bool) or months < 0:
  14. return None
  15. y, mo, d = int(m.group(1)), int(m.group(2)), int(m.group(3))
  16. try:
  17. date(y, mo, d)
  18. except ValueError:
  19. return None
  20. total = y * 12 + (mo - 1) + months
  21. ny, nm = divmod(total, 12)
  22. nm += 1
  23. last = calendar.monthrange(ny, nm)[1]
  24. nd = min(d, last)
  25. return f"{ny:04d}-{nm:02d}-{nd:02d}"