Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

What is your definition of "subtracting a year"? Seems like that's a relatively ambiguous operation without more specification.


Can you think of any situation where subtracting a year from today's date is ambiguous when today isn't, well, today?


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.


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.


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.


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.


Worth a mention... Falsehoods Programmers Believe About Time: https://news.ycombinator.com/item?id=4128208


Since they mentioned a clean up script, I assume they could easily just use 365 days for that use case.


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


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.


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.


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.


It is ambiguous, but one part that's pretty unambiguous is that the result should be a date, not a crash.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: