kern

annotate 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
rev   line source
nuclear@36 1 #include <stdio.h>
nuclear@36 2 #include "time.h"
nuclear@36 3
nuclear@36 4 #define MINSEC 60
nuclear@36 5 #define HOURSEC (60 * MINSEC)
nuclear@36 6 #define DAYSEC (24 * HOURSEC)
nuclear@36 7
nuclear@36 8 static int is_leap_year(int yr);
nuclear@36 9
nuclear@36 10
nuclear@36 11 static char *wday[] = {
nuclear@36 12 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
nuclear@36 13 };
nuclear@36 14 static char *mon[] = {
nuclear@36 15 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
nuclear@36 16 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
nuclear@36 17 };
nuclear@36 18
nuclear@36 19
nuclear@36 20 char *asctime(struct tm *tm)
nuclear@36 21 {
nuclear@36 22 static char buf[64];
nuclear@36 23
nuclear@36 24 sprintf(buf, "%s %s %d %02d:%02d:%02d %d\n", wday[tm->tm_wday],
nuclear@36 25 mon[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min,
nuclear@36 26 tm->tm_sec, tm->tm_year + 1900);
nuclear@36 27 return buf;
nuclear@36 28 }
nuclear@36 29
nuclear@36 30 time_t mktime(struct tm *tm)
nuclear@36 31 {
nuclear@36 32 int i, num_years = tm->tm_year - 70;
nuclear@36 33 int year = 1970;
nuclear@36 34 int days = day_of_year(tm->tm_year + 1900, tm->tm_mon, tm->tm_mday - 1);
nuclear@36 35
nuclear@36 36 for(i=0; i<num_years; i++) {
nuclear@36 37 days += is_leap_year(year++) ? 366 : 365;
nuclear@36 38 }
nuclear@36 39
nuclear@36 40 return (time_t)days * DAYSEC + tm->tm_hour * HOURSEC +
nuclear@36 41 tm->tm_min * MINSEC + tm->tm_sec;
nuclear@36 42 }
nuclear@36 43
nuclear@36 44 int day_of_year(int year, int mon, int day)
nuclear@36 45 {
nuclear@36 46 static int commdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
nuclear@36 47 static int leapdays[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
nuclear@36 48 int *mdays, i, yday;
nuclear@36 49
nuclear@36 50 mdays = is_leap_year(year) ? leapdays : commdays;
nuclear@36 51
nuclear@36 52 yday = day;
nuclear@36 53 for(i=0; i<mon; i++) {
nuclear@36 54 yday += mdays[i];
nuclear@36 55 }
nuclear@36 56 return yday;
nuclear@36 57 }
nuclear@36 58
nuclear@36 59 static int is_leap_year(int yr)
nuclear@36 60 {
nuclear@36 61 /* exceptions first */
nuclear@36 62 if(yr % 400 == 0) {
nuclear@36 63 return 1;
nuclear@36 64 }
nuclear@36 65 if(yr % 100 == 0) {
nuclear@36 66 return 0;
nuclear@36 67 }
nuclear@36 68 /* standard case */
nuclear@36 69 return yr % 4 == 0;
nuclear@36 70 }