kern

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