Exactly. Very easily avoided if they'd just done something like dateTo = dateFrom.AddMonths(1).AddDays(-1); which would handle everything for them...they're already using AddDays() so God only knows why they didn't bother using AddMonths() as well.
I have seen a very similar bug once. Though instead of the month they tried adding one to the day of the month. So that part of the system just stopped working at the end of almost every second month, to just as promptly start working again.
Literally why it took months (may have been years) for it to be discovered. They reported it on the weekend and when we got back on Monday it worked again. Also our incident response time was way over 24h for non-critical issues so whenever someone in the development department started looking at it all looked operational. It was first when it happened to be the same developer months apart that was the first to look at it and he started digging wondering what was going on.
I think it depends on the language/framework.
in typescript this works fine, the JavaScript Date object wraps to the next year when adding more than 12 months to the date constructor, so doing something like this:
Thanks for checking this. I saw this post and immediately thought of a piece of code I wrote a couple months ago that might be similar to this, but I'm just gonna go ahead and trust you on this one hahah
I ran into this exact same bug in a piece of code a while ago, luckily before it had a chance to blow up in our face. It was also for calculating the time for monthly reporting funnily enough. I think the author just assumed the constructor would wrap around to januari and add a year, but it throws an exception instead.
This actually creates another bug if you're very unlucky. DateTime.Now is called 4 times. There's a very minute chance that these values are different from dateFrom to DateTo
2
u/ItsGator Dec 10 '21
when it's December, what does the month in dateTo resolve to? January?
139
u/Herku_ Dec 09 '21
Now this, is fucking beautiful
43
u/[deleted] Dec 10 '21
Took me a second to see it. Is the issue month number 13? Lol.
74
u/philipTheDev Dec 09 '21
I have seen a very similar bug once. Though instead of the month they tried adding one to the day of the month. So that part of the system just stopped working at the end of almost every second month, to just as promptly start working again.
32
u/r-guerreiro Dec 10 '21
Just fix it by doing % 12 + 1 Next year you worry about the year
20
u/alexanderhameowlton Dec 09 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
22
u/[deleted] Dec 10 '21
There was a similar bug posted here the other day. Must be that time of year.
6
u/rxsel Dec 10 '21
NGL, I use luxon a lot and thought a co-worker had screenshot my work because I see the DateTime object so much...
3
u/[deleted] Dec 10 '21
This is why you unit test
3
u/bORAT25 Dec 10 '21
The hot fix be like - (DateTime.Now.Month+1) % 12
3
u/[deleted] Dec 10 '21 edited Dec 10 '21
The harbinger of structural doom: The stringly typed switch statement.
2
u/Ellogwen Dec 10 '21
I think it depends on the language/framework. in typescript this works fine, the JavaScript Date object wraps to the next year when adding more than 12 months to the date constructor, so doing something like this:
to get the end of the year works fine (at least I did not notice anything wrong). Thies yields to this output:
1
u/Acc3ssViolation Dec 10 '21
I ran into this exact same bug in a piece of code a while ago, luckily before it had a chance to blow up in our face. It was also for calculating the time for monthly reporting funnily enough. I think the author just assumed the constructor would wrap around to januari and add a year, but it throws an exception instead.
1
u/lunar_tardigrade Dec 11 '21
In fairness the developer didn't belive it would actually be an active solution this long.
1
u/RealLethalChicken Dec 11 '21
Just % 12 lol
1
u/zombi3123 Dec 23 '21
This actually creates another bug if you're very unlucky. DateTime.Now is called 4 times. There's a very minute chance that these values are different from dateFrom to DateTo