int __getTime( const time_t timer, struct tm *tmp ) { int days; int timeOfDay; int year; int mon; ldiv_t result; /* Calculate days since epoch */ days = timer / SECSPERDAY; timeOfDay = timer % SECSPERDAY; if (timeOfDay < 0) { timeOfDay += SECSPERDAY; days -= 1; } /* Calculate years since epoch */ year = days / DAYSPERYEAR; while (__daysSinceEpoch(year, 0) > days) { year--; } /* Calculcate weekday */ tmp->tm_wday = (days + EPOCH_WDAY) % DAYSPERWEEK; if (tmp->tm_wday < 0) { tmp->tm_wday += DAYSPERWEEK; } /* Find year and days left */ days -= __daysSinceEpoch(year, 0); year += EPOCH_YEAR; /* Calculate month */ for (mon = 0; (days >= __julday(year - TM_YEAR_BASE, mon + 1, 0)) && (mon < 11); mon++); /* Setup time structure */ tmp->tm_year = year - TM_YEAR_BASE; tmp->tm_mon = mon; tmp->tm_mday = (days - __julday(tmp->tm_year, mon, 0)) + 1; tmp->tm_yday = __julday(tmp->tm_year, mon, tmp->tm_mday) - 1; tmp->tm_hour = timeOfDay / SECSPERHOUR; timeOfDay %= SECSPERHOUR; ldiv_r((long) timeOfDay, (long) SECSPERMIN, &result); tmp->tm_min = result.quot; tmp->tm_sec = result.rem; return OK; }
ldiv_t ldiv( long numer, long denom ) { static ldiv_t result; ldiv_r(numer, denom, &result); return result; }
int __getTime ( const time_t timer, /* time represented as seconds from epoch */ struct tm * tmp /* pointer to broken-down time structure */ ) { long days; long timeOfDay; long year; long mon; ldiv_t result; /* Calulate number of days since epoch */ days = timer / SECSPERDAY; timeOfDay = timer % SECSPERDAY; /* If time of day is negative, subtract one day, and add SECSPERDAY * to make it positive. */ if(timeOfDay<0) { timeOfDay+=SECSPERDAY; days-=1; } /* Calulate number of years since epoch */ year = days / DAYSPERYEAR; while ( __daysSinceEpoch (year, 0) > days ) year--; /* Calulate the day of the week */ tmp->tm_wday = (days + EPOCH_WDAY) % DAYSPERWEEK; /* * If there is a negative weekday, add DAYSPERWEEK to make it positive */ if(tmp->tm_wday<0) tmp->tm_wday+=DAYSPERWEEK; /* Find year and remaining days */ days -= __daysSinceEpoch (year, 0); year += EPOCH_YEAR; /* Find month */ /* __jullday needs years since TM_YEAR_BASE (SPR 4251) */ for ( mon = 0; (days >= __julday (year - TM_YEAR_BASE, mon + 1, 0)) && (mon < 11); mon++ ) ; /* Initialise tm structure */ tmp->tm_year = year - TM_YEAR_BASE; /* years since 1900 */ tmp->tm_mon = mon; tmp->tm_mday = (days - __julday (tmp->tm_year, mon, 0)) + 1; tmp->tm_yday = __julday (tmp->tm_year, mon, tmp->tm_mday) - 1; tmp->tm_hour = timeOfDay / SECSPERHOUR; timeOfDay %= SECSPERHOUR; ldiv_r (timeOfDay, SECSPERMIN, &result); tmp->tm_min = result.quot; tmp->tm_sec = result.rem; return(OK); }