News for hackers. Stuff that matters.
Re:Ask HN: Did you encounter any leap year bugs today? by LeoPanthera on Thursday February 29, 2024 @04:13PM (#39555178)
> now.replace(year=now.year-1)

Yeah but this is bad code. Python certainly does have a "clean" way to subtract a year, you subtract a datetime.timedelta object.

Re:Ask HN: Did you encounter any leap year bugs today? by yen223 on Thursday February 29, 2024 @07:22PM (#39557024)
timedelta doesn't take "years" as a parameter, for the reasons others have listed here. It's ambiguous what subtracting by a year means, and there's no real sensible default either.
Re:Ask HN: Did you encounter any leap year bugs today? by LeoPanthera on Thursday February 29, 2024 @08:49PM (#39557583)
relativedelta will do it:

  from dateutil.relativedelta import relativedelta
  one_year = relativedelta(years=1)