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.
The time-stamp is in seconds, and the date/time structure is in the following format:
unsigned char year: 0 to 99 (representing the range 2000 to 2099)
unsigned char month: 1 to 12
unsigned char day: 1 to 31
unsigned char hour: 0 to 23
unsigned char minute: 0 to 59
unsigned char second: 0 to 59
I would like to have a second opinion on the dt2ts convertor (assuming that the input is legal):
unsigned int dt2ts(const dt_t* dt)
{
static unsigned short days[] = {0,31,59,90,120,151,181,212,243,273,304,334};
return ((((dt->year*365+dt->year/4)+days[dt->month-1]+dt->day)*24+dt->hour)*60+dt->minute)*60+dt->second;
}
In addition to that, I would appreciate some help completing the ts2dt convertor:
void ts2dt(unsigned int ts,dt_t* dt)
{
dt->second = ts%60; ts /= 60;
dt->minute = ts%60; ts /= 60;
dt->hour = ts%24; ts /= 24;
dt->day = ?????;
dt->month = ?????;
dt->year = ?????;
}
Thanks