nuclear@36: #include nuclear@36: #include "time.h" nuclear@37: #include "rtc.h" nuclear@37: #include "timer.h" nuclear@37: #include "config.h" nuclear@36: nuclear@36: #define MINSEC 60 nuclear@36: #define HOURSEC (60 * MINSEC) nuclear@36: #define DAYSEC (24 * HOURSEC) nuclear@37: #define YEARDAYS(x) (is_leap_year(x) ? 366 : 365) nuclear@36: nuclear@36: static int is_leap_year(int yr); nuclear@36: nuclear@37: static int mdays[2][12] = { nuclear@37: {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, nuclear@37: {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} nuclear@37: }; nuclear@36: nuclear@36: static char *wday[] = { nuclear@36: "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" nuclear@36: }; nuclear@36: static char *mon[] = { nuclear@36: "Jan", "Feb", "Mar", "Apr", "May", "Jun", nuclear@36: "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" nuclear@36: }; nuclear@36: nuclear@36: nuclear@37: time_t time(time_t *tp) nuclear@37: { nuclear@37: time_t res = start_time + nticks / TICK_FREQ_HZ; nuclear@37: nuclear@37: if(tp) *tp = res; nuclear@37: return res; nuclear@37: } nuclear@37: nuclear@36: char *asctime(struct tm *tm) nuclear@36: { nuclear@36: static char buf[64]; nuclear@37: return asctime_r(tm, buf); nuclear@37: } nuclear@36: nuclear@37: char *asctime_r(struct tm *tm, char *buf) nuclear@37: { nuclear@36: sprintf(buf, "%s %s %d %02d:%02d:%02d %d\n", wday[tm->tm_wday], nuclear@36: mon[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min, nuclear@36: tm->tm_sec, tm->tm_year + 1900); nuclear@36: return buf; nuclear@36: } nuclear@36: nuclear@36: time_t mktime(struct tm *tm) nuclear@36: { nuclear@36: int i, num_years = tm->tm_year - 70; nuclear@36: int year = 1970; nuclear@36: int days = day_of_year(tm->tm_year + 1900, tm->tm_mon, tm->tm_mday - 1); nuclear@36: nuclear@36: for(i=0; itm_hour * HOURSEC + nuclear@36: tm->tm_min * MINSEC + tm->tm_sec; nuclear@36: } nuclear@36: nuclear@37: struct tm *gmtime(time_t *tp) nuclear@37: { nuclear@37: static struct tm tm; nuclear@37: return gmtime_r(tp, &tm); nuclear@37: } nuclear@37: nuclear@37: struct tm *gmtime_r(time_t *tp, struct tm *tm) nuclear@37: { nuclear@38: int year, days, leap, yrdays; nuclear@38: time_t t; nuclear@37: nuclear@38: year = 1970; nuclear@38: days = *tp / DAYSEC; nuclear@38: t = *tp % DAYSEC; nuclear@38: nuclear@38: tm->tm_wday = (days + 4) % 7; nuclear@38: nuclear@38: while(days >= (yrdays = YEARDAYS(year))) { nuclear@38: days -= yrdays; nuclear@37: year++; nuclear@37: } nuclear@37: tm->tm_year = year - 1900; nuclear@39: tm->tm_yday = days; nuclear@37: nuclear@37: leap = is_leap_year(year); nuclear@37: tm->tm_mon = 0; nuclear@38: while(days >= mdays[leap][tm->tm_mon]) { nuclear@38: days -= mdays[leap][tm->tm_mon++]; nuclear@37: } nuclear@37: nuclear@38: tm->tm_mday = days + 1; nuclear@37: nuclear@37: tm->tm_hour = t / HOURSEC; nuclear@37: t %= HOURSEC; nuclear@37: tm->tm_min = t / MINSEC; nuclear@37: tm->tm_sec = t % MINSEC; nuclear@37: return tm; nuclear@37: } nuclear@37: nuclear@36: int day_of_year(int year, int mon, int day) nuclear@36: { nuclear@37: int i, yday, leap; nuclear@36: nuclear@37: leap = is_leap_year(year) ? 1 : 0; nuclear@37: yday = day; nuclear@36: nuclear@36: for(i=0; i