I am writing a class for holding dates in c++, and I found the following problem:
I have a number of days N
since a reference date (in
Let me simplify the question, I won't consider exceptions for explanation. Every 4 years, a leap occur, if you have 365*5 days, there must be a leap-year (unless if the exception 2 is applied). You could just use division for having the number of leap-years (if ignoring the exceptions).
Then you can easily use division and remainder for having the non-leap years/months/days.
Use the same basic intuition for resolving the Exception 1 (if the number of years is a multiple of 100, then also check the Exception 2)
- Years which are divisible by 4 are leap
- Exception to rule 1: years that are divisible with 100 are not leap
- Exception to rule 2: years that are divisible with 400 are leap
I have a number of days N since a reference date (in my case that would be Jan 1, 0001 AD)...
In that case, "efficiency" in applying the 4-100-400 rule and looking up month lengths is not your main problem. Please be also aware of the multiple problems inherent in applying today's Gregorian calendar to dates predating its inception, and the fact that Gregorian was not introduced uniformly. (*)
Wikipedia is a good starting point to a very involved subject.
(*): Depending on country, anywhere between 15 October 1582 and 15 February 1923, resp. not at all, actually.
bool IsLeapYear(int year)
{
boost::gregorian::date d1(year, 1, 1);
boost::gregorian::date d2 = d1 + boost::gregorian::years(1);
boost::gregorian::date_duration diff = d2 - d1;
return diff.days() != 365;
}
This
bool IsLeapYear(int year)
{
if ((year % 4 == 0) && (year % 100 != 0) && (year % 400 == 0)) return true;
else return false;
}
is incorrect. It returns false
for 2000. Better:
bool IsLeapYear(int year)
{
if (year % 400 == 0) return true;
if ((year % 4 == 0) && (year % 100 != 0)) return true;
return false;
}