Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/String.h>
28#include <AK/StringBuilder.h>
29#include <Kernel/KernelInfoPage.h>
30#include <Kernel/Syscall.h>
31#include <assert.h>
32#include <errno.h>
33#include <stdio.h>
34#include <string.h>
35#include <sys/time.h>
36#include <sys/times.h>
37#include <time.h>
38
39extern "C" {
40
41time_t time(time_t* tloc)
42{
43 struct timeval tv;
44 struct timezone tz;
45 if (gettimeofday(&tv, &tz) < 0)
46 return (time_t)-1;
47 if (tloc)
48 *tloc = tv.tv_sec;
49 return tv.tv_sec;
50}
51
52int gettimeofday(struct timeval* __restrict__ tv, void* __restrict__)
53{
54 static volatile KernelInfoPage* kernel_info;
55 if (!kernel_info)
56 kernel_info = (volatile KernelInfoPage*)syscall(SC_get_kernel_info_page);
57
58 for (;;) {
59 auto serial = kernel_info->serial;
60 *tv = const_cast<struct timeval&>(kernel_info->now);
61 if (serial == kernel_info->serial)
62 break;
63 }
64 return 0;
65}
66
67char* ctime(const time_t* t)
68{
69 return asctime(localtime(t));
70}
71
72static inline bool __is_leap_year(int year)
73{
74 return ((year % 4 == 0) && ((year % 100 != 0) || (year % 400) == 0));
75}
76
77static const int __days_per_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
78static const int __seconds_per_day = 60 * 60 * 24;
79
80static void time_to_tm(struct tm* tm, time_t t)
81{
82 int days = t / __seconds_per_day;
83 int remaining = t % __seconds_per_day;
84 tm->tm_sec = remaining % 60;
85 remaining /= 60;
86 tm->tm_min = remaining % 60;
87 tm->tm_hour = remaining / 60;
88 tm->tm_wday = (4 + days) % 7;
89 int year;
90 for (year = 1970; days >= 365 + __is_leap_year(year); ++year)
91 days -= 365 + __is_leap_year(year);
92 tm->tm_year = year - 1900;
93 tm->tm_yday = days;
94 tm->tm_mday = 1;
95 if (__is_leap_year(year) && days == 59)
96 ++tm->tm_mday;
97 if (__is_leap_year(year) && days >= 59)
98 --days;
99 int month;
100 for (month = 0; month < 11 && days >= __days_per_month[month]; ++month)
101 days -= __days_per_month[month];
102 tm->tm_mon = month;
103 tm->tm_mday += days;
104}
105
106time_t mktime(struct tm* tm)
107{
108 int days = 0;
109 int seconds = tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
110 for (int year = 70; year < tm->tm_year; ++year)
111 days += 365 + __is_leap_year(1900 + year);
112
113 tm->tm_yday = tm->tm_mday - 1;
114 for (int month = 0; month < tm->tm_mon; ++month)
115 tm->tm_yday += __days_per_month[month];
116 if (tm->tm_mon > 1 && __is_leap_year(1900 + tm->tm_year))
117 ++tm->tm_yday;
118
119 days += tm->tm_yday;
120 return days * __seconds_per_day + seconds + timezone;
121}
122
123struct tm* localtime(const time_t* t)
124{
125 static struct tm tm_buf;
126 return localtime_r(t, &tm_buf);
127}
128
129struct tm* localtime_r(const time_t* t, struct tm* tm)
130{
131 if (!t)
132 return nullptr;
133 time_to_tm(tm, (*t) - timezone);
134 return tm;
135}
136
137struct tm* gmtime(const time_t* t)
138{
139 static struct tm tm_buf;
140 return gmtime_r(t, &tm_buf);
141}
142
143struct tm* gmtime_r(const time_t* t, struct tm* tm)
144{
145 if (!t)
146 return nullptr;
147 time_to_tm(tm, *t);
148 return tm;
149}
150
151char* asctime(const struct tm* tm)
152{
153 static char buffer[69];
154 strftime(buffer, sizeof buffer, "%a %b %e %T %Y", tm);
155 return buffer;
156}
157
158//FIXME: Some formats are not supported.
159size_t strftime(char* destination, size_t max_size, const char* format, const struct tm* tm)
160{
161 const char wday_short_names[7][4] = {
162 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
163 };
164 const char wday_long_names[7][10] = {
165 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
166 };
167 const char mon_short_names[12][4] = {
168 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
169 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
170 };
171 const char mon_long_names[12][10] = {
172 "January", "February", "March", "April", "May", "June",
173 "July", "Auguest", "September", "October", "November", "December"
174 };
175
176 StringBuilder builder { max_size - 1 };
177
178 const int format_len = strlen(format);
179 for (int i = 0; i < format_len; ++i) {
180 if (format[i] != '%') {
181 builder.append(format[i]);
182 } else {
183 if (++i >= format_len)
184 return 0;
185
186 switch (format[i]) {
187 case 'a':
188 builder.append(wday_short_names[tm->tm_wday]);
189 break;
190 case 'A':
191 builder.append(wday_long_names[tm->tm_wday]);
192 break;
193 case 'b':
194 builder.append(mon_short_names[tm->tm_mon]);
195 break;
196 case 'B':
197 builder.append(mon_long_names[tm->tm_mon]);
198 break;
199 case 'C':
200 builder.appendf("%02d", (tm->tm_year + 1900) / 100);
201 break;
202 case 'd':
203 builder.appendf("%02d", tm->tm_mday);
204 break;
205 case 'D':
206 builder.appendf("%02d/%02d/%02d", tm->tm_mon + 1, tm->tm_mday, (tm->tm_year + 1900) % 100);
207 break;
208 case 'e':
209 builder.appendf("%2d", tm->tm_mday);
210 break;
211 case 'h':
212 builder.append(mon_short_names[tm->tm_mon]);
213 break;
214 case 'H':
215 builder.appendf("%02d", tm->tm_hour);
216 break;
217 case 'I':
218 builder.appendf("%02d", tm->tm_hour % 12);
219 break;
220 case 'j':
221 builder.appendf("%03d", tm->tm_yday + 1);
222 break;
223 case 'm':
224 builder.appendf("%02d", tm->tm_mon + 1);
225 break;
226 case 'M':
227 builder.appendf("%02d", tm->tm_min);
228 break;
229 case 'n':
230 builder.append('\n');
231 break;
232 case 'p':
233 builder.append(tm->tm_hour < 12 ? "a.m." : "p.m.");
234 break;
235 case 'r':
236 builder.appendf("%02d:%02d:%02d %s", tm->tm_hour % 12, tm->tm_min, tm->tm_sec, tm->tm_hour < 12 ? "a.m." : "p.m.");
237 break;
238 case 'R':
239 builder.appendf("%02d:%02d", tm->tm_hour, tm->tm_min);
240 break;
241 case 'S':
242 builder.appendf("%02d", tm->tm_sec);
243 break;
244 case 't':
245 builder.append('\t');
246 break;
247 case 'T':
248 builder.appendf("%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
249 break;
250 case 'u':
251 builder.appendf("%d", tm->tm_wday ? tm->tm_wday : 7);
252 break;
253 case 'U': {
254 const int wday_of_year_beginning = (tm->tm_wday + 6 * tm->tm_yday) % 7;
255 const int week_number = (tm->tm_yday + wday_of_year_beginning) / 7;
256 builder.appendf("%02d", week_number);
257 break;
258 }
259 case 'V': {
260 const int wday_of_year_beginning = (tm->tm_wday + 6 + 6 * tm->tm_yday) % 7;
261 int week_number = (tm->tm_yday + wday_of_year_beginning) / 7 + 1;
262 if (wday_of_year_beginning > 3) {
263 if (tm->tm_yday >= 7 - wday_of_year_beginning)
264 --week_number;
265 else {
266 const int days_of_last_year = 365 + __is_leap_year(tm->tm_year + 1900 - 1);
267 const int wday_of_last_year_beginning = (wday_of_year_beginning + 6 * days_of_last_year) % 7;
268 week_number = (days_of_last_year + wday_of_last_year_beginning) / 7 + 1;
269 if (wday_of_last_year_beginning > 3)
270 --week_number;
271 }
272 }
273 builder.appendf("%02d", week_number);
274 break;
275 }
276 case 'w':
277 builder.appendf("%d", tm->tm_wday);
278 break;
279 case 'W': {
280 const int wday_of_year_beginning = (tm->tm_wday + 6 + 6 * tm->tm_yday) % 7;
281 const int week_number = (tm->tm_yday + wday_of_year_beginning) / 7;
282 builder.appendf("%02d", week_number);
283 break;
284 }
285 case 'y':
286 builder.appendf("%02d", (tm->tm_year + 1900) % 100);
287 break;
288 case 'Y':
289 builder.appendf("%d", tm->tm_year + 1900);
290 break;
291 case '%':
292 builder.append('%');
293 break;
294 default:
295 return 0;
296 }
297 }
298 if (builder.length() > max_size - 1)
299 return 0;
300 }
301
302 strcpy(destination, builder.build().characters());
303 return builder.length();
304}
305
306long timezone = 0;
307long altzone;
308char* tzname[2];
309int daylight;
310
311void tzset()
312{
313 //FIXME: Here we prepend we are in UTC+0.
314 timezone = 0;
315}
316
317clock_t clock()
318{
319 struct tms tms;
320 times(&tms);
321 return tms.tms_utime + tms.tms_stime;
322}
323
324int clock_gettime(clockid_t clock_id, struct timespec* ts)
325{
326 int rc = syscall(SC_clock_gettime, clock_id, ts);
327 __RETURN_WITH_ERRNO(rc, rc, -1);
328}
329
330int clock_settime(clockid_t clock_id, struct timespec* ts)
331{
332 int rc = syscall(SC_clock_settime, clock_id, ts);
333 __RETURN_WITH_ERRNO(rc, rc, -1);
334}
335
336int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec* requested_sleep, struct timespec* remaining_sleep)
337{
338 Syscall::SC_clock_nanosleep_params params { clock_id, flags, requested_sleep, remaining_sleep };
339 int rc = syscall(SC_clock_nanosleep, ¶ms);
340 __RETURN_WITH_ERRNO(rc, rc, -1);
341}
342
343int clock_getres(clockid_t, struct timespec*)
344{
345 ASSERT_NOT_REACHED();
346}
347}