Skip to content

Instantly share code, notes, and snippets.

View canchesi's full-sized avatar

Claudio Anchesi canchesi

  • Italy
View GitHub Profile
@canchesi
canchesi / days.py
Last active April 29, 2025 15:23
Arithmetic function to get days of the month, being careful about leap years.
def days_of_month(m: int, y: int) -> int:
"""
Return 30 or 31 if it's not February, otherwise 28 or 29 if leap year.
"""
return 30 + ((m // 8 + m) % 8) % 2 - (((2-(2%m)) * (m - 1))//2) * (m - 1 + (((((y % 4) + 3 ) // 4)) + ((1 - (((y % 100) + 99) // 100)) * (((y % 400) + 399) // 400))))