kern

diff src/klibc/time.c @ 36:e70b1ab9613e

- added cmos rtc code - added time/date functions in klibc - implemented an iowait macro with output to 0x80
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 10 Jun 2011 05:33:38 +0300
parents
children 2c401f69128e
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/klibc/time.c	Fri Jun 10 05:33:38 2011 +0300
     1.3 @@ -0,0 +1,70 @@
     1.4 +#include <stdio.h>
     1.5 +#include "time.h"
     1.6 +
     1.7 +#define MINSEC		60
     1.8 +#define HOURSEC		(60 * MINSEC)
     1.9 +#define DAYSEC		(24 * HOURSEC)
    1.10 +
    1.11 +static int is_leap_year(int yr);
    1.12 +
    1.13 +
    1.14 +static char *wday[] = {
    1.15 +	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
    1.16 +};
    1.17 +static char *mon[] = {
    1.18 +	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
    1.19 +	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    1.20 +};
    1.21 +
    1.22 +
    1.23 +char *asctime(struct tm *tm)
    1.24 +{
    1.25 +	static char buf[64];
    1.26 +
    1.27 +	sprintf(buf, "%s %s %d %02d:%02d:%02d %d\n", wday[tm->tm_wday],
    1.28 +			mon[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min,
    1.29 +			tm->tm_sec, tm->tm_year + 1900);
    1.30 +	return buf;
    1.31 +}
    1.32 +
    1.33 +time_t mktime(struct tm *tm)
    1.34 +{
    1.35 +	int i, num_years = tm->tm_year - 70;
    1.36 +	int year = 1970;
    1.37 +	int days = day_of_year(tm->tm_year + 1900, tm->tm_mon, tm->tm_mday - 1);
    1.38 +
    1.39 +	for(i=0; i<num_years; i++) {
    1.40 +		days += is_leap_year(year++) ? 366 : 365;
    1.41 +	}
    1.42 +
    1.43 +	return (time_t)days * DAYSEC + tm->tm_hour * HOURSEC +
    1.44 +		tm->tm_min * MINSEC + tm->tm_sec;
    1.45 +}
    1.46 +
    1.47 +int day_of_year(int year, int mon, int day)
    1.48 +{
    1.49 +	static int commdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    1.50 +	static int leapdays[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    1.51 +	int *mdays, i, yday;
    1.52 +
    1.53 +	mdays = is_leap_year(year) ? leapdays : commdays;
    1.54 +
    1.55 +	yday = day;
    1.56 +	for(i=0; i<mon; i++) {
    1.57 +		yday += mdays[i];
    1.58 +	}
    1.59 +	return yday;
    1.60 +}
    1.61 +
    1.62 +static int is_leap_year(int yr)
    1.63 +{
    1.64 +	/* exceptions first */
    1.65 +	if(yr % 400 == 0) {
    1.66 +		return 1;
    1.67 +	}
    1.68 +	if(yr % 100 == 0) {
    1.69 +		return 0;
    1.70 +	}
    1.71 +	/* standard case */
    1.72 +	return yr % 4 == 0;
    1.73 +}