kern

diff src/klibc/time.c @ 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
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  }