News for hackers. Stuff that matters.
Re:Ask HN: Did you encounter any leap year bugs today? by rlaager on Thursday February 29, 2024 @03:34PM (#39554674)
One cleanup script broke because Python doesn’t have a clean way to subtract a year, and if you do now.replace(year=now.year-1), you get a ValueError when now is 2/29.

It’s easy enough to address. There are various StackOverflow posts on such things. Here is one: https://stackoverflow.com/questions/54394327/using-datetime-...

Re:Ask HN: Did you encounter any leap year bugs today? by cmgriffing on Thursday February 29, 2024 @04:27PM (#39555346)
I get it if you don't want to bring in another dependency, but Arrow has a lot of nice utility methods: https://arrow.readthedocs.io/en/latest/guide.html#replace-sh...
Re:Ask HN: Did you encounter any leap year bugs today? by natrius on Thursday February 29, 2024 @03:58PM (#39554976)
It's not that python doesn't have a clean way to subtract a year, it's that "subtract a year" is imprecise. There's a clean way to subtract 365 days, and there's a clean way to set the year one year earlier. But if you're doing the second thing, is python supposed to silently change to March 1 when you change the year from a leap day? There's no way around handling edge cases.
Re:Ask HN: Did you encounter any leap year bugs today? by Swenrekcah on Thursday February 29, 2024 @04:06PM (#39555103)
APy should just figure out what I would have wanted to happen
Re:Ask HN: Did you encounter any leap year bugs today? by samatman on Thursday February 29, 2024 @06:42PM (#39556721)
This has been a factor in contract law for longer than computers have existed. "Subtract a year" or "Add a year" aren't really imprecise, it's just that there are two ways you can interpret it: 365 days, or 12 months. When adding, you get the same result: end of February, the 28th. Subtracting, you get one of March 1st for 365 days and February 28th for 12 months.

Most jurisdictions use the 12 month standard, fwiw. I believe that's the ISO ruling as well, but I wasn't able to confirm that.

Re:Ask HN: Did you encounter any leap year bugs today? by emmelaich on Thursday February 29, 2024 @07:39PM (#39557141)
Which type of year? Sidereal or solar? Did you account for axial precession?
Re:Ask HN: Did you encounter any leap year bugs today? by samatman on Friday March 01, 2024 @09:14AM (#39561949)
To be a bit blunt, these questions are entirely irrelevant, since you know the answer as well as I do: the calendar year, the one Western law and international business uses uniformly. It's very clever of you to mention that other concepts of the year exist. Kind of. Would have been more clever if you'd realized that they aren't germane, any more than the Islamic, Jewish, Indian, or Chinese years are.
Re:Ask HN: Did you encounter any leap year bugs today? by bhaney on Thursday February 29, 2024 @07:57PM (#39557252)
> There's a clean way to subtract 365 days

What is it? Does it attempt to return a datetime that's the same time as the input but 365 calendar days earlier (ignoring leap seconds? compensating for time zone changes?), or does it subtract 31536000 seconds from the current datetime? Because those aren't necessarily the same and it's ambiguous which one you mean by "subtract 365 days"

Re:Ask HN: Did you encounter any leap year bugs today? by aiiane on Thursday February 29, 2024 @03:49PM (#39554853)
What is your definition of "subtracting a year"? Seems like that's a relatively ambiguous operation without more specification.
Re:Ask HN: Did you encounter any leap year bugs today? by Ensorceled on Thursday February 29, 2024 @03:57PM (#39554964)
Can you think of any situation where subtracting a year from today's date is ambiguous when today isn't, well, today?
Re:Ask HN: Did you encounter any leap year bugs today? by aiiane on Thursday February 29, 2024 @04:27PM (#39555345)
Yes - on any day, subtracting a year might mean subtracting the average length of a year (which is a bit more than 365 days), or wanting the same day and month number in the previous calendar year, or wanting the same semantic difference ("last Monday of the month in January"), to name a few possible meanings.
Re:Ask HN: Did you encounter any leap year bugs today? by JohnFen on Thursday February 29, 2024 @04:50PM (#39555652)
Worth a mention... Falsehoods Programmers Believe About Time: https://news.ycombinator.com/item?id=4128208
Re:Ask HN: Did you encounter any leap year bugs today? by OJFord on Thursday February 29, 2024 @05:38PM (#39556179)
Moving bank/festive holidays, first Monday of the year(, first work day of the year not Monday if that's NYD and bank holiday), lunar occasions.

'subtract a year' is imprecise and has many meanings, if what you want is 'same day, same month, previous year' then say that and do that, that's conceptually `date.year -= 1` not `date -= 1 year`, and will have this bug.

Re:Ask HN: Did you encounter any leap year bugs today? by chatmasta on Thursday February 29, 2024 @06:15PM (#39556470)
You could subtract 365.25 days, but then you're left with a new problem: just because you can amortize a leap day over four years doesn't mean that you get an extra 6 hours each year.
Re:Ask HN: Did you encounter any leap year bugs today? by OJFord on Thursday February 29, 2024 @06:50PM (#39556802)
And as I just learnt elsewhere in this thread, it would actually be three four-hundredths less than that anyway, i.e. 365.2425, since only one in four centenaries is a leap year.
Re:Ask HN: Did you encounter any leap year bugs today? by kccqzy on Thursday February 29, 2024 @03:57PM (#39554958)
Since they mentioned a clean up script, I assume they could easily just use 365 days for that use case.
Re:Ask HN: Did you encounter any leap year bugs today? by hinkley on Thursday February 29, 2024 @04:04PM (#39555068)
But then it'll be off by one day for the rest of this year. And someone will notice that they no longer have March 1 2023-March 1 2024 in their chart, but March 2 2023
Re:Ask HN: Did you encounter any leap year bugs today? by kccqzy on Thursday February 29, 2024 @05:50PM (#39556284)
It's a cleanup script. I bet nobody cares it's off by one day. Also I doubt a cleanup script has a charting function.

Everything is use case dependent. Sometimes the use case is unimportant enough that mistakes are okay.

Re:Ask HN: Did you encounter any leap year bugs today? by hinkley on Thursday February 29, 2024 @06:13PM (#39556454)
While that's true, I'm sharing scar tissue here not hypotheticals.

There's often someone out there who interprets such things as a 3 month, or 1 year retention policy and that it means they're entitled to look at the entire range whenever they want.

Re:Ask HN: Did you encounter any leap year bugs today? by adrianmonk on Thursday February 29, 2024 @06:41PM (#39556716)
It is ambiguous, but one part that's pretty unambiguous is that the result should be a date, not a crash.
Re:Ask HN: Did you encounter any leap year bugs today? by ddgflorida on Thursday February 29, 2024 @06:52PM (#39556818)
Plenty of thought has gone into this. Look at what database date functions do when you ask it to subtract 1 year. I will agree that there is not one answer.
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)
Re:Ask HN: Did you encounter any leap year bugs today? by VirusModulePtr on Thursday February 29, 2024 @03:59PM (#39554992)
Okay I am having really odd undefined behavior in Python in UART communications that were working just fine yesterday... My boss joked it could be a leap year thing but at this point it wouldn't surprise me. Switch over to using Rust and no issues at all
Re:Ask HN: Did you encounter any leap year bugs today? by iooi on Thursday February 29, 2024 @04:16PM (#39555220)
which is why you should use arrow