rev |
line source |
nuclear@36
|
1 #include <stdio.h>
|
nuclear@36
|
2 #include "time.h"
|
nuclear@37
|
3 #include "rtc.h"
|
nuclear@37
|
4 #include "timer.h"
|
nuclear@37
|
5 #include "config.h"
|
nuclear@36
|
6
|
nuclear@36
|
7 #define MINSEC 60
|
nuclear@36
|
8 #define HOURSEC (60 * MINSEC)
|
nuclear@36
|
9 #define DAYSEC (24 * HOURSEC)
|
nuclear@37
|
10 #define YEARDAYS(x) (is_leap_year(x) ? 366 : 365)
|
nuclear@36
|
11
|
nuclear@36
|
12 static int is_leap_year(int yr);
|
nuclear@36
|
13
|
nuclear@37
|
14 static int mdays[2][12] = {
|
nuclear@37
|
15 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
|
nuclear@37
|
16 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
|
nuclear@37
|
17 };
|
nuclear@36
|
18
|
nuclear@36
|
19 static char *wday[] = {
|
nuclear@36
|
20 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
nuclear@36
|
21 };
|
nuclear@36
|
22 static char *mon[] = {
|
nuclear@36
|
23 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
nuclear@36
|
24 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
nuclear@36
|
25 };
|
nuclear@36
|
26
|
nuclear@36
|
27
|
nuclear@37
|
28 time_t time(time_t *tp)
|
nuclear@37
|
29 {
|
nuclear@37
|
30 time_t res = start_time + nticks / TICK_FREQ_HZ;
|
nuclear@37
|
31
|
nuclear@37
|
32 if(tp) *tp = res;
|
nuclear@37
|
33 return res;
|
nuclear@37
|
34 }
|
nuclear@37
|
35
|
nuclear@36
|
36 char *asctime(struct tm *tm)
|
nuclear@36
|
37 {
|
nuclear@36
|
38 static char buf[64];
|
nuclear@37
|
39 return asctime_r(tm, buf);
|
nuclear@37
|
40 }
|
nuclear@36
|
41
|
nuclear@37
|
42 char *asctime_r(struct tm *tm, char *buf)
|
nuclear@37
|
43 {
|
nuclear@36
|
44 sprintf(buf, "%s %s %d %02d:%02d:%02d %d\n", wday[tm->tm_wday],
|
nuclear@36
|
45 mon[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min,
|
nuclear@36
|
46 tm->tm_sec, tm->tm_year + 1900);
|
nuclear@36
|
47 return buf;
|
nuclear@36
|
48 }
|
nuclear@36
|
49
|
nuclear@36
|
50 time_t mktime(struct tm *tm)
|
nuclear@36
|
51 {
|
nuclear@36
|
52 int i, num_years = tm->tm_year - 70;
|
nuclear@36
|
53 int year = 1970;
|
nuclear@36
|
54 int days = day_of_year(tm->tm_year + 1900, tm->tm_mon, tm->tm_mday - 1);
|
nuclear@36
|
55
|
nuclear@36
|
56 for(i=0; i<num_years; i++) {
|
nuclear@37
|
57 days += YEARDAYS(year++);
|
nuclear@36
|
58 }
|
nuclear@36
|
59
|
nuclear@36
|
60 return (time_t)days * DAYSEC + tm->tm_hour * HOURSEC +
|
nuclear@36
|
61 tm->tm_min * MINSEC + tm->tm_sec;
|
nuclear@36
|
62 }
|
nuclear@36
|
63
|
nuclear@37
|
64 struct tm *gmtime(time_t *tp)
|
nuclear@37
|
65 {
|
nuclear@37
|
66 static struct tm tm;
|
nuclear@37
|
67 return gmtime_r(tp, &tm);
|
nuclear@37
|
68 }
|
nuclear@37
|
69
|
nuclear@37
|
70 struct tm *gmtime_r(time_t *tp, struct tm *tm)
|
nuclear@37
|
71 {
|
nuclear@38
|
72 int year, days, leap, yrdays;
|
nuclear@38
|
73 time_t t;
|
nuclear@37
|
74
|
nuclear@38
|
75 year = 1970;
|
nuclear@38
|
76 days = *tp / DAYSEC;
|
nuclear@38
|
77 t = *tp % DAYSEC;
|
nuclear@38
|
78
|
nuclear@38
|
79 tm->tm_wday = (days + 4) % 7;
|
nuclear@38
|
80
|
nuclear@38
|
81 while(days >= (yrdays = YEARDAYS(year))) {
|
nuclear@38
|
82 days -= yrdays;
|
nuclear@37
|
83 year++;
|
nuclear@37
|
84 }
|
nuclear@37
|
85 tm->tm_year = year - 1900;
|
nuclear@39
|
86 tm->tm_yday = days;
|
nuclear@37
|
87
|
nuclear@37
|
88 leap = is_leap_year(year);
|
nuclear@37
|
89 tm->tm_mon = 0;
|
nuclear@38
|
90 while(days >= mdays[leap][tm->tm_mon]) {
|
nuclear@38
|
91 days -= mdays[leap][tm->tm_mon++];
|
nuclear@37
|
92 }
|
nuclear@37
|
93
|
nuclear@38
|
94 tm->tm_mday = days + 1;
|
nuclear@37
|
95
|
nuclear@37
|
96 tm->tm_hour = t / HOURSEC;
|
nuclear@37
|
97 t %= HOURSEC;
|
nuclear@37
|
98 tm->tm_min = t / MINSEC;
|
nuclear@37
|
99 tm->tm_sec = t % MINSEC;
|
nuclear@37
|
100 return tm;
|
nuclear@37
|
101 }
|
nuclear@37
|
102
|
nuclear@36
|
103 int day_of_year(int year, int mon, int day)
|
nuclear@36
|
104 {
|
nuclear@37
|
105 int i, yday, leap;
|
nuclear@36
|
106
|
nuclear@37
|
107 leap = is_leap_year(year) ? 1 : 0;
|
nuclear@37
|
108 yday = day;
|
nuclear@36
|
109
|
nuclear@36
|
110 for(i=0; i<mon; i++) {
|
nuclear@37
|
111 yday += mdays[leap][i];
|
nuclear@36
|
112 }
|
nuclear@36
|
113 return yday;
|
nuclear@36
|
114 }
|
nuclear@36
|
115
|
nuclear@36
|
116 static int is_leap_year(int yr)
|
nuclear@36
|
117 {
|
nuclear@36
|
118 /* exceptions first */
|
nuclear@36
|
119 if(yr % 400 == 0) {
|
nuclear@36
|
120 return 1;
|
nuclear@36
|
121 }
|
nuclear@36
|
122 if(yr % 100 == 0) {
|
nuclear@36
|
123 return 0;
|
nuclear@36
|
124 }
|
nuclear@36
|
125 /* standard case */
|
nuclear@36
|
126 return yr % 4 == 0;
|
nuclear@36
|
127 }
|