I am making a program that requires a duration (in time_t) of a year.
In other words, time_tfor DD / MM / YYYY + duration = time_tfor DD / MM / YYYY + 1
Thus, this may not always be 365 days (and 02/29/2012 will become 02/28/2013)
Here is the algorithm I came with:
if YEAR is leap than
if we are before the 29th feb' than return 365+1 days
else if we are the 29th feb' than return 365-1 days
else return 365 days
else if YEAR+1 is leap than
if we are before or the 28th feb' than return 365 days
else return 365+1 days
else return 365 days
Here is the day 60 * 60 * 24 seconds
This algorithm works. But I was wondering if there is another way to do this without all the conditions and only 2 possible return values or just a "trick" to optimize this thing.
I tried to zoom tm_yearin struct tmas follows:
struct tm Tm (*localtime(&t));
if (Tm.tm_mon == 2 && Tm.tm_mday == 29) --Tm.tm_mday;
++Tm.tm_year;
return mktime(&Tm) - t;
But the result is not what I want, I got -1 hour or -25 ...
I guess, because the year is not exactly 365 * 24 * 60 * 60.