2 pcboot - bootable PC demo/game kernel
3 Copyright (C) 2018-2019 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY, without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
25 #define HOURSEC (60 * MINSEC)
26 #define DAYSEC (24 * HOURSEC)
27 #define YEARDAYS(x) (is_leap_year(x) ? 366 : 365)
29 /* 1-1-1970 was a thursday */
32 static int is_leap_year(int yr);
34 static int mdays[2][12] = {
35 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
36 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
39 static char *wday[] = {
40 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
42 static char *mon[] = {
43 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
44 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
48 time_t time(time_t *tp)
50 time_t res = start_time + nticks / TICK_FREQ_HZ;
56 char *asctime(struct tm *tm)
59 return asctime_r(tm, buf);
62 char *asctime_r(struct tm *tm, char *buf)
64 sprintf(buf, "%s %s %d %02d:%02d:%02d %d\n", wday[tm->tm_wday],
65 mon[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min,
66 tm->tm_sec, tm->tm_year + 1900);
70 time_t mktime(struct tm *tm)
72 int i, num_years = tm->tm_year - 70;
74 int days = day_of_year(tm->tm_year + 1900, tm->tm_mon, tm->tm_mday - 1);
76 /* set correct yearday */
79 for(i=0; i<num_years; i++) {
80 days += YEARDAYS(year++);
83 /* set wday correctly */
84 tm->tm_wday = (days + EPOCH_WDAY) % 7;
86 return (time_t)days * DAYSEC + tm->tm_hour * HOURSEC +
87 tm->tm_min * MINSEC + tm->tm_sec;
90 struct tm *gmtime(time_t *tp)
93 return gmtime_r(tp, &tm);
96 struct tm *gmtime_r(time_t *tp, struct tm *tm)
98 int year, days, leap, yrdays;
105 tm->tm_wday = (days + EPOCH_WDAY) % 7;
107 while(days >= (yrdays = YEARDAYS(year))) {
111 tm->tm_year = year - 1900;
114 leap = is_leap_year(year);
116 while(days >= mdays[leap][tm->tm_mon]) {
117 days -= mdays[leap][tm->tm_mon++];
120 tm->tm_mday = days + 1;
122 tm->tm_hour = t / HOURSEC;
124 tm->tm_min = t / MINSEC;
125 tm->tm_sec = t % MINSEC;
129 struct tm *localtime(time_t *tp)
132 return localtime_r(tp, &tm);
135 struct tm *localtime_r(time_t *tp, struct tm *tm)
137 time_t t = *tp + timezone;
138 return gmtime_r(&t, tm);
141 int day_of_year(int year, int mon, int day)
145 leap = is_leap_year(year) ? 1 : 0;
148 for(i=0; i<mon; i++) {
149 yday += mdays[leap][i];
154 static int is_leap_year(int yr)
156 /* exceptions first */