kern

changeset 37:2c401f69128e

finished with the klibc/time.c stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 10 Jun 2011 19:54:47 +0300
parents e70b1ab9613e
children e6f75f91e606
files src/klibc/time.c src/klibc/time.h src/rtc.c src/rtc.h src/timer.c
diffstat 5 files changed, 84 insertions(+), 12 deletions(-) [+]
line diff
     1.1 --- a/src/klibc/time.c	Fri Jun 10 05:33:38 2011 +0300
     1.2 +++ b/src/klibc/time.c	Fri Jun 10 19:54:47 2011 +0300
     1.3 @@ -1,12 +1,20 @@
     1.4  #include <stdio.h>
     1.5  #include "time.h"
     1.6 +#include "rtc.h"
     1.7 +#include "timer.h"
     1.8 +#include "config.h"
     1.9  
    1.10  #define MINSEC		60
    1.11  #define HOURSEC		(60 * MINSEC)
    1.12  #define DAYSEC		(24 * HOURSEC)
    1.13 +#define YEARDAYS(x)	(is_leap_year(x) ? 366 : 365)
    1.14  
    1.15  static int is_leap_year(int yr);
    1.16  
    1.17 +static int mdays[2][12] = {
    1.18 +	{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    1.19 +	{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    1.20 +};
    1.21  
    1.22  static char *wday[] = {
    1.23  	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
    1.24 @@ -17,10 +25,22 @@
    1.25  };
    1.26  
    1.27  
    1.28 +time_t time(time_t *tp)
    1.29 +{
    1.30 +	time_t res = start_time + nticks / TICK_FREQ_HZ;
    1.31 +
    1.32 +	if(tp) *tp = res;
    1.33 +	return res;
    1.34 +}
    1.35 +
    1.36  char *asctime(struct tm *tm)
    1.37  {
    1.38  	static char buf[64];
    1.39 +	return asctime_r(tm, buf);
    1.40 +}
    1.41  
    1.42 +char *asctime_r(struct tm *tm, char *buf)
    1.43 +{
    1.44  	sprintf(buf, "%s %s %d %02d:%02d:%02d %d\n", wday[tm->tm_wday],
    1.45  			mon[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min,
    1.46  			tm->tm_sec, tm->tm_year + 1900);
    1.47 @@ -34,24 +54,64 @@
    1.48  	int days = day_of_year(tm->tm_year + 1900, tm->tm_mon, tm->tm_mday - 1);
    1.49  
    1.50  	for(i=0; i<num_years; i++) {
    1.51 -		days += is_leap_year(year++) ? 366 : 365;
    1.52 +		days += YEARDAYS(year++);
    1.53  	}
    1.54  
    1.55  	return (time_t)days * DAYSEC + tm->tm_hour * HOURSEC +
    1.56  		tm->tm_min * MINSEC + tm->tm_sec;
    1.57  }
    1.58  
    1.59 +struct tm *gmtime(time_t *tp)
    1.60 +{
    1.61 +	static struct tm tm;
    1.62 +	return gmtime_r(tp, &tm);
    1.63 +}
    1.64 +
    1.65 +struct tm *gmtime_r(time_t *tp, struct tm *tm)
    1.66 +{
    1.67 +	int yrsec, yrdays, monsec, leap, day, num_days = 0;
    1.68 +	int year = 1970;
    1.69 +	time_t t = *tp;
    1.70 +
    1.71 +	while(t >= (yrsec = (yrdays = YEARDAYS(year)) * DAYSEC)) {
    1.72 +		t -= yrsec;
    1.73 +		year++;
    1.74 +		num_days += yrdays;
    1.75 +	}
    1.76 +	tm->tm_year = year - 1900;
    1.77 +
    1.78 +	leap = is_leap_year(year);
    1.79 +	tm->tm_mon = 0;
    1.80 +	while(t >= (monsec = mdays[leap][tm->tm_mon] * DAYSEC)) {
    1.81 +		num_days += mdays[leap][tm->tm_mon++];
    1.82 +		t -= monsec;
    1.83 +	}
    1.84 +
    1.85 +	day = t / DAYSEC;
    1.86 +	tm->tm_mday = day + 1;
    1.87 +	t %= DAYSEC;
    1.88 +
    1.89 +	tm->tm_hour = t / HOURSEC;
    1.90 +	t %= HOURSEC;
    1.91 +
    1.92 +	tm->tm_min = t / MINSEC;
    1.93 +	tm->tm_sec = t % MINSEC;
    1.94 +
    1.95 +	num_days += day;
    1.96 +	tm->tm_wday = (num_days + 4) % 7;
    1.97 +	tm->tm_yday = day_of_year(year, tm->tm_mon, day);
    1.98 +	return tm;
    1.99 +}
   1.100 +
   1.101  int day_of_year(int year, int mon, int day)
   1.102  {
   1.103 -	static int commdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
   1.104 -	static int leapdays[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
   1.105 -	int *mdays, i, yday;
   1.106 +	int i, yday, leap;
   1.107  
   1.108 -	mdays = is_leap_year(year) ? leapdays : commdays;
   1.109 +	leap = is_leap_year(year) ? 1 : 0;
   1.110 +	yday = day;
   1.111  
   1.112 -	yday = day;
   1.113  	for(i=0; i<mon; i++) {
   1.114 -		yday += mdays[i];
   1.115 +		yday += mdays[leap][i];
   1.116  	}
   1.117  	return yday;
   1.118  }
     2.1 --- a/src/klibc/time.h	Fri Jun 10 05:33:38 2011 +0300
     2.2 +++ b/src/klibc/time.h	Fri Jun 10 19:54:47 2011 +0300
     2.3 @@ -15,8 +15,13 @@
     2.4  	int tm_isdst;
     2.5  };
     2.6  
     2.7 +time_t time(time_t *tp);
     2.8  char *asctime(struct tm *tm);
     2.9 +char *asctime_r(struct tm *tm, char *buf);
    2.10 +
    2.11  time_t mktime(struct tm *tm);
    2.12 +struct tm *gmtime(time_t *tp);
    2.13 +struct tm *gmtime_r(time_t *tp, struct tm *tm);
    2.14  
    2.15  /* non-standard helpers */
    2.16  int day_of_year(int year, int mon, int day);
     3.1 --- a/src/rtc.c	Fri Jun 10 05:33:38 2011 +0300
     3.2 +++ b/src/rtc.c	Fri Jun 10 19:54:47 2011 +0300
     3.3 @@ -1,6 +1,7 @@
     3.4  #include <stdio.h>
     3.5  #include <time.h>
     3.6  #include <asmops.h>
     3.7 +#include "rtc.h"
     3.8  
     3.9  /* CMOS I/O ports */
    3.10  #define PORT_CTL	0x70
    3.11 @@ -33,18 +34,18 @@
    3.12  static void read_rtc(struct tm *tm);
    3.13  static int read_reg(int reg);
    3.14  
    3.15 -static unsigned long start_time;
    3.16 -
    3.17  
    3.18  void init_rtc(void)
    3.19  {
    3.20  	struct tm tm;
    3.21  
    3.22  	read_rtc(&tm);
    3.23 +	start_time = mktime(&tm);
    3.24 +
    3.25  	printf("System real-time clock: %s", asctime(&tm));
    3.26 -	printf("time_t: %ld\n", mktime(&tm));
    3.27  }
    3.28  
    3.29 +
    3.30  static void read_rtc(struct tm *tm)
    3.31  {
    3.32  	int statb, pm;
     4.1 --- a/src/rtc.h	Fri Jun 10 05:33:38 2011 +0300
     4.2 +++ b/src/rtc.h	Fri Jun 10 19:54:47 2011 +0300
     4.3 @@ -1,7 +1,9 @@
     4.4  #ifndef _RTC_H_
     4.5  #define _RTC_H_
     4.6  
     4.7 +/* the time read from rtc during init */
     4.8 +time_t start_time;
     4.9 +
    4.10  void init_rtc(void);
    4.11  
    4.12 -
    4.13  #endif	/* _RTC_H_ */
     5.1 --- a/src/timer.c	Fri Jun 10 05:33:38 2011 +0300
     5.2 +++ b/src/timer.c	Fri Jun 10 19:54:47 2011 +0300
     5.3 @@ -1,4 +1,5 @@
     5.4  #include <stdio.h>
     5.5 +#include <time.h>
     5.6  #include "intr.h"
     5.7  #include "asmops.h"
     5.8  #include "timer.h"
     5.9 @@ -70,6 +71,9 @@
    5.10  	nticks++;
    5.11  
    5.12  	if(nticks % TICK_FREQ_HZ == 0) {
    5.13 -		printf("%lu sec\n", nticks / TICK_FREQ_HZ);
    5.14 +		/*printf("%lu sec\n", nticks / TICK_FREQ_HZ);*/
    5.15 +		time_t t = time(0);
    5.16 +
    5.17 +		printf("%s", asctime(gmtime(&t)));
    5.18  	}
    5.19  }