I am trying to implement in C two simple convertors, date/time to time-stamp and vice versa, without any dependencies on time library routines, such as mktime, etc.
OP is all ready well handling the hours, minutes, seconds. Just a bit of assist on Y,M,D.
Note: The number of days from Jan 1, 2000 to Dec 31, 2099 needs at least a 16 bit integer. Following should work even if unsigned
is 2 bytes.
unsigned DivRem(unsigned Dividend, unsigned Divisor, unsigned *Remainder) {
unsigned Quotient = Dividend/Divisor;
*Remainder = Dividend - Quotient*Divisor;
return Quotient;
}
void Day2000ToYMD(unsigned DaySince2000Jan1, unsigned *Y, unsigned *M, unsigned *D) {
unsigned OlympiadDay; // Every 4 years is an Olympiad
*Y = 4*DivRem(DaySince2000Jan1, 365*4+1, &OlympiadDay);
*D = 1;
if (OlympiadDay >= (31+29-1)) { // deal with Feb 29th and after
OlympiadDay--;
if (OlympiadDay == (31+29-1)) {
(*D)++;
}
}
unsigned YearDay; // Day of the year 0 to 364
*Y += DivRem(OlympiadDay, 365, &YearDay);
static const unsigned short days[] = {0,31,59,90,120,151,181,212,243,273,304,334,365};
*M = 1;
while (days[*M] <= YearDay) (*M)++;
*D += YearDay - days[*M - 1];
}
[Edit] The answer provided tries to keep the concept of the year as Jan 1 to Dec 31. As this answer does not need to handle the leap year years about 100 and 400 years, I've kept with this style.
In general, once those 2 rules are added, the math becomes easier to if one shifts the beginning of the year to March 1 and ending on Feb 28/29. FWIW, this is a more consistent view of the ancient development of the Julian/Gregorian calendar. Thus *Oct*ober is then the 8th month and *Dec*ember is the 10th month.