What other leap year bugs have people run into? Generally the libraries I work with (e.g. python's timedelta) don't let you add months or years because of their ambiguity.
What other leap year bugs have people run into? Generally the libraries I work with (e.g. python's timedelta) don't let you add months or years because of their ambiguity.
> t = Time.parse("feb 29, 2012")
=> 2012-02-29 00:00:00 -0500
> t + 1.year
=> 2013-02-28 00:00:00 -0500
> t + 1.year == (t - 1.day + 1.year)
=> trueBut say 2012-02-29 + 1 year went to 2013-03-01. Then what's 2012-03-01 - 1 year? Does it go to March 1st every time, sometimes ignoring that there's an extra day in between, or does it go back 365 days (March 2nd)?
I suspect the only "solution" is to decide that calendar dates are for human consumption only. If you're doing any calculations, you do them on timestamps, where you can declare a 'year' to be one of 365.256363004, 365.24219, or 365.259636 days (sidereal, tropical, and anomalistic years)[1]. Given the importance of calendars and seasons, you'd probably want to use the middle one, as they all essentially share that length of time as a definition of a year. That way you can just screw the whole leap-year concept entirely.
Of course, then you're left with year-long agreements that expire at odd hours of the night.
[1]: http://en.wikipedia.org/wiki/Year#Sidereal.2C_tropical.2C_an...
I much prefer spelling out plainly the intention.
So I think as a matter of API design, Ruby has made a wrong choice by making these operations look like arithmetic on known values. It's too tempting to think that they'll obey the arithmetic identities, when they don't. If you want to have an API function "same date N months later", I have no problem with that at all; then it's much less tempting to think that it's just doing addition.
DateTime expiry = DateTime.Now.AddYears(1);
No need to worry about leap days or anything. The framework takes a date/time such as 2012-02-29 15:00 and calculates that one year later is 2013-02-28 15:00. Similarly, 2012-01-31 15:00 called with .AddMonths(1) returns 2012-02-29 15:00 and calling that with .AddMonths(-1) returns 2012-01-29 15:00.As long as you understand how your library handles these calculations, trying to do it manually is likely to get you in trouble.
new DateTime(2012, 3, 31)
.AddMonths(-1)
.AddMonths(1) ==> 2012-03-29
new DateTime(2012, 3, 31)
.AddMonths(1)
.AddMonths(-1) ==> 2012-03-30[0] For example: http://sourceforge.net/projects/dday-ical/
So to add a year, all you have to do is add 365.25 * 24 * 60 * 60.
This was a stunningly stupid, n00b mistake.
Yes, that's my point exactly. The conversion from the familiar representation to linear seconds incorporates all of those complications (except leap seconds, which hardly anyone bothers with).
If you don't want to use 365.25, you can write code to figure out whether to use 365 or 366; but in neither case will you have a bug like the one Azure hit where a completely invalid date was generated.
Among all the tasks and problems that seem simple, you'll find Time and date questions to be some of the trickiest. Deriding "noobs" doesn't do anyone any credit.
I stand by my statement: this was a stunningly stupid, n00b mistake. If I had made it, I would say exactly the same thing.