This should be accurate (fills the abbreviated simulation struct tm, mine yearuses Common Era instead of the 1900 era):
struct xtm
{
unsigned int year, mon, day, hour, min, sec;
};
void untime(unsigned long unixtime, struct xtm *tm)
{
tm->sec = unixtime % 60;
unixtime /= 60;
tm->min = unixtime % 60;
unixtime /= 60;
tm->hour = unixtime % 24;
unixtime /= 24;
unixtime += 719499;
for (tm->year = 1969; unixtime > YEAR_TO_DAYS(tm->year + 1) + 30; tm->year++)
;
unixtime -= YEAR_TO_DAYS(tm->year);
for (tm->mon = 1; tm->mon < 12 && unixtime > 367*(tm->mon+1)/12; tm->mon++)
;
unixtime -= 367*tm->mon/12;
tm->mon += 2;
if (tm->mon > 12)
{
tm->mon -= 12;
tm->year++;
}
tm->day = unixtime;
}
I apologize for all the magic numbers. 367 * month / 12 is a neat trick to generate a 30/31 day calendar sequence. The calculation works with the years that begin in March before fixing at the end, which facilitates the process, because then the leap day falls at the end of the "year".
source
share