# HG changeset patch # User John Tsiombikas # Date 1307724887 -10800 # Node ID 2c401f69128e715b3c4cff42b06da953791364a5 # Parent e70b1ab9613e461734aa51bb3c2547a775b7f169 finished with the klibc/time.c stuff diff -r e70b1ab9613e -r 2c401f69128e src/klibc/time.c --- a/src/klibc/time.c Fri Jun 10 05:33:38 2011 +0300 +++ b/src/klibc/time.c Fri Jun 10 19:54:47 2011 +0300 @@ -1,12 +1,20 @@ #include #include "time.h" +#include "rtc.h" +#include "timer.h" +#include "config.h" #define MINSEC 60 #define HOURSEC (60 * MINSEC) #define DAYSEC (24 * HOURSEC) +#define YEARDAYS(x) (is_leap_year(x) ? 366 : 365) static int is_leap_year(int yr); +static int mdays[2][12] = { + {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, + {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} +}; static char *wday[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" @@ -17,10 +25,22 @@ }; +time_t time(time_t *tp) +{ + time_t res = start_time + nticks / TICK_FREQ_HZ; + + if(tp) *tp = res; + return res; +} + char *asctime(struct tm *tm) { static char buf[64]; + return asctime_r(tm, buf); +} +char *asctime_r(struct tm *tm, char *buf) +{ sprintf(buf, "%s %s %d %02d:%02d:%02d %d\n", wday[tm->tm_wday], mon[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_year + 1900); @@ -34,24 +54,64 @@ int days = day_of_year(tm->tm_year + 1900, tm->tm_mon, tm->tm_mday - 1); for(i=0; itm_hour * HOURSEC + tm->tm_min * MINSEC + tm->tm_sec; } +struct tm *gmtime(time_t *tp) +{ + static struct tm tm; + return gmtime_r(tp, &tm); +} + +struct tm *gmtime_r(time_t *tp, struct tm *tm) +{ + int yrsec, yrdays, monsec, leap, day, num_days = 0; + int year = 1970; + time_t t = *tp; + + while(t >= (yrsec = (yrdays = YEARDAYS(year)) * DAYSEC)) { + t -= yrsec; + year++; + num_days += yrdays; + } + tm->tm_year = year - 1900; + + leap = is_leap_year(year); + tm->tm_mon = 0; + while(t >= (monsec = mdays[leap][tm->tm_mon] * DAYSEC)) { + num_days += mdays[leap][tm->tm_mon++]; + t -= monsec; + } + + day = t / DAYSEC; + tm->tm_mday = day + 1; + t %= DAYSEC; + + tm->tm_hour = t / HOURSEC; + t %= HOURSEC; + + tm->tm_min = t / MINSEC; + tm->tm_sec = t % MINSEC; + + num_days += day; + tm->tm_wday = (num_days + 4) % 7; + tm->tm_yday = day_of_year(year, tm->tm_mon, day); + return tm; +} + int day_of_year(int year, int mon, int day) { - static int commdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - static int leapdays[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - int *mdays, i, yday; + int i, yday, leap; - mdays = is_leap_year(year) ? leapdays : commdays; + leap = is_leap_year(year) ? 1 : 0; + yday = day; - yday = day; for(i=0; i #include #include +#include "rtc.h" /* CMOS I/O ports */ #define PORT_CTL 0x70 @@ -33,18 +34,18 @@ static void read_rtc(struct tm *tm); static int read_reg(int reg); -static unsigned long start_time; - void init_rtc(void) { struct tm tm; read_rtc(&tm); + start_time = mktime(&tm); + printf("System real-time clock: %s", asctime(&tm)); - printf("time_t: %ld\n", mktime(&tm)); } + static void read_rtc(struct tm *tm) { int statb, pm; diff -r e70b1ab9613e -r 2c401f69128e src/rtc.h --- a/src/rtc.h Fri Jun 10 05:33:38 2011 +0300 +++ b/src/rtc.h Fri Jun 10 19:54:47 2011 +0300 @@ -1,7 +1,9 @@ #ifndef _RTC_H_ #define _RTC_H_ +/* the time read from rtc during init */ +time_t start_time; + void init_rtc(void); - #endif /* _RTC_H_ */ diff -r e70b1ab9613e -r 2c401f69128e src/timer.c --- a/src/timer.c Fri Jun 10 05:33:38 2011 +0300 +++ b/src/timer.c Fri Jun 10 19:54:47 2011 +0300 @@ -1,4 +1,5 @@ #include +#include #include "intr.h" #include "asmops.h" #include "timer.h" @@ -70,6 +71,9 @@ nticks++; if(nticks % TICK_FREQ_HZ == 0) { - printf("%lu sec\n", nticks / TICK_FREQ_HZ); + /*printf("%lu sec\n", nticks / TICK_FREQ_HZ);*/ + time_t t = time(0); + + printf("%s", asctime(gmtime(&t))); } }