_utils.py 621 B

123456789101112131415161718
  1. from datetime import date, timedelta
  2. def month_bounds(month: int, year: int) -> tuple[str, str]:
  3. start = date(year, month, 1)
  4. end = date(year + month // 12, month % 12 + 1, 1) - timedelta(days=1)
  5. return start.isoformat(), end.isoformat()
  6. def months_in_range(date_from: str, date_to: str) -> list[tuple[int, int]]:
  7. start = date.fromisoformat(date_from)
  8. end = date.fromisoformat(date_to)
  9. months: list[tuple[int, int]] = []
  10. cur = date(start.year, start.month, 1)
  11. while cur <= end:
  12. months.append((cur.month, cur.year))
  13. cur = date(cur.year + cur.month // 12, cur.month % 12 + 1, 1)
  14. return months