1 | /*
|
---|
2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
3 | * Copyright (c) 2011 Petr Koupy
|
---|
4 | * Copyright (c) 2011 Jiri Zarevucky
|
---|
5 | * All rights reserved.
|
---|
6 | *
|
---|
7 | * Redistribution and use in source and binary forms, with or without
|
---|
8 | * modification, are permitted provided that the following conditions
|
---|
9 | * are met:
|
---|
10 | *
|
---|
11 | * - Redistributions of source code must retain the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer.
|
---|
13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
14 | * notice, this list of conditions and the following disclaimer in the
|
---|
15 | * documentation and/or other materials provided with the distribution.
|
---|
16 | * - The name of the author may not be used to endorse or promote products
|
---|
17 | * derived from this software without specific prior written permission.
|
---|
18 | *
|
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /** @addtogroup libc
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 | /** @file
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <sys/time.h>
|
---|
38 | #include <time.h>
|
---|
39 | #include <bool.h>
|
---|
40 | #include <libarch/barrier.h>
|
---|
41 | #include <macros.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <sysinfo.h>
|
---|
44 | #include <as.h>
|
---|
45 | #include <ddi.h>
|
---|
46 | #include <libc.h>
|
---|
47 | #include <stdint.h>
|
---|
48 | #include <stdio.h>
|
---|
49 | #include <ctype.h>
|
---|
50 | #include <assert.h>
|
---|
51 | #include <unistd.h>
|
---|
52 |
|
---|
53 | #define ASCTIME_BUF_LEN 26
|
---|
54 |
|
---|
55 | /** Pointer to kernel shared variables with time */
|
---|
56 | struct {
|
---|
57 | volatile sysarg_t seconds1;
|
---|
58 | volatile sysarg_t useconds;
|
---|
59 | volatile sysarg_t seconds2;
|
---|
60 | } *ktime = NULL;
|
---|
61 |
|
---|
62 | /* Helper functions ***********************************************************/
|
---|
63 |
|
---|
64 | #define HOURS_PER_DAY (24)
|
---|
65 | #define MINS_PER_HOUR (60)
|
---|
66 | #define SECS_PER_MIN (60)
|
---|
67 | #define MINS_PER_DAY (MINS_PER_HOUR * HOURS_PER_DAY)
|
---|
68 | #define SECS_PER_HOUR (SECS_PER_MIN * MINS_PER_HOUR)
|
---|
69 | #define SECS_PER_DAY (SECS_PER_HOUR * HOURS_PER_DAY)
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Checks whether the year is a leap year.
|
---|
73 | *
|
---|
74 | * @param year Year since 1900 (e.g. for 1970, the value is 70).
|
---|
75 | * @return true if year is a leap year, false otherwise
|
---|
76 | */
|
---|
77 | static bool _is_leap_year(time_t year)
|
---|
78 | {
|
---|
79 | year += 1900;
|
---|
80 |
|
---|
81 | if (year % 400 == 0)
|
---|
82 | return true;
|
---|
83 | if (year % 100 == 0)
|
---|
84 | return false;
|
---|
85 | if (year % 4 == 0)
|
---|
86 | return true;
|
---|
87 | return false;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Returns how many days there are in the given month of the given year.
|
---|
92 | * Note that year is only taken into account if month is February.
|
---|
93 | *
|
---|
94 | * @param year Year since 1900 (can be negative).
|
---|
95 | * @param mon Month of the year. 0 for January, 11 for December.
|
---|
96 | * @return Number of days in the specified month.
|
---|
97 | */
|
---|
98 | static int _days_in_month(time_t year, time_t mon)
|
---|
99 | {
|
---|
100 | assert(mon >= 0 && mon <= 11);
|
---|
101 |
|
---|
102 | static int month_days[] =
|
---|
103 | { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
---|
104 |
|
---|
105 | if (mon == 1) {
|
---|
106 | year += 1900;
|
---|
107 | /* february */
|
---|
108 | return _is_leap_year(year) ? 29 : 28;
|
---|
109 | } else {
|
---|
110 | return month_days[mon];
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * For specified year, month and day of month, returns which day of that year
|
---|
116 | * it is.
|
---|
117 | *
|
---|
118 | * For example, given date 2011-01-03, the corresponding expression is:
|
---|
119 | * _day_of_year(111, 0, 3) == 2
|
---|
120 | *
|
---|
121 | * @param year Year (year 1900 = 0, can be negative).
|
---|
122 | * @param mon Month (January = 0).
|
---|
123 | * @param mday Day of month (First day is 1).
|
---|
124 | * @return Day of year (First day is 0).
|
---|
125 | */
|
---|
126 | static int _day_of_year(time_t year, time_t mon, time_t mday)
|
---|
127 | {
|
---|
128 | static int mdays[] =
|
---|
129 | { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
|
---|
130 | static int leap_mdays[] =
|
---|
131 | { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
|
---|
132 |
|
---|
133 | return (_is_leap_year(year) ? leap_mdays[mon] : mdays[mon]) + mday - 1;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Integer division that rounds to negative infinity.
|
---|
138 | * Used by some functions in this file.
|
---|
139 | *
|
---|
140 | * @param op1 Dividend.
|
---|
141 | * @param op2 Divisor.
|
---|
142 | * @return Rounded quotient.
|
---|
143 | */
|
---|
144 | static time_t _floor_div(time_t op1, time_t op2)
|
---|
145 | {
|
---|
146 | if (op1 >= 0 || op1 % op2 == 0) {
|
---|
147 | return op1 / op2;
|
---|
148 | } else {
|
---|
149 | return op1 / op2 - 1;
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Modulo that rounds to negative infinity.
|
---|
155 | * Used by some functions in this file.
|
---|
156 | *
|
---|
157 | * @param op1 Dividend.
|
---|
158 | * @param op2 Divisor.
|
---|
159 | * @return Remainder.
|
---|
160 | */
|
---|
161 | static time_t _floor_mod(time_t op1, time_t op2)
|
---|
162 | {
|
---|
163 | int div = _floor_div(op1, op2);
|
---|
164 |
|
---|
165 | /* (a / b) * b + a % b == a */
|
---|
166 | /* thus, a % b == a - (a / b) * b */
|
---|
167 |
|
---|
168 | int result = op1 - div * op2;
|
---|
169 |
|
---|
170 | /* Some paranoid checking to ensure I didn't make a mistake here. */
|
---|
171 | assert(result >= 0);
|
---|
172 | assert(result < op2);
|
---|
173 | assert(div * op2 + result == op1);
|
---|
174 |
|
---|
175 | return result;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Number of days since the Epoch.
|
---|
180 | * Epoch is 1970-01-01, which is also equal to day 0.
|
---|
181 | *
|
---|
182 | * @param year Year (year 1900 = 0, may be negative).
|
---|
183 | * @param mon Month (January = 0).
|
---|
184 | * @param mday Day of month (first day = 1).
|
---|
185 | * @return Number of days since the Epoch.
|
---|
186 | */
|
---|
187 | static time_t _days_since_epoch(time_t year, time_t mon, time_t mday)
|
---|
188 | {
|
---|
189 | return (year - 70) * 365 + _floor_div(year - 69, 4) -
|
---|
190 | _floor_div(year - 1, 100) + _floor_div(year + 299, 400) +
|
---|
191 | _day_of_year(year, mon, mday);
|
---|
192 | }
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Seconds since the Epoch. see also _days_since_epoch().
|
---|
196 | *
|
---|
197 | * @param tm Normalized broken-down time.
|
---|
198 | * @return Number of seconds since the epoch, not counting leap seconds.
|
---|
199 | */
|
---|
200 | static time_t _secs_since_epoch(const struct tm *tm)
|
---|
201 | {
|
---|
202 | return _days_since_epoch(tm->tm_year, tm->tm_mon, tm->tm_mday) *
|
---|
203 | SECS_PER_DAY + tm->tm_hour * SECS_PER_HOUR +
|
---|
204 | tm->tm_min * SECS_PER_MIN + tm->tm_sec;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Which day of week the specified date is.
|
---|
209 | *
|
---|
210 | * @param year Year (year 1900 = 0).
|
---|
211 | * @param mon Month (January = 0).
|
---|
212 | * @param mday Day of month (first = 1).
|
---|
213 | * @return Day of week (Sunday = 0).
|
---|
214 | */
|
---|
215 | static int _day_of_week(time_t year, time_t mon, time_t mday)
|
---|
216 | {
|
---|
217 | /* 1970-01-01 is Thursday */
|
---|
218 | return _floor_mod((_days_since_epoch(year, mon, mday) + 4), 7);
|
---|
219 | }
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Normalizes the broken-down time and optionally adds specified amount of
|
---|
223 | * seconds.
|
---|
224 | *
|
---|
225 | * @param tm Broken-down time to normalize.
|
---|
226 | * @param sec_add Seconds to add.
|
---|
227 | * @return 0 on success, -1 on overflow
|
---|
228 | */
|
---|
229 | static int _normalize_time(struct tm *tm, time_t sec_add)
|
---|
230 | {
|
---|
231 | // TODO: DST correction
|
---|
232 |
|
---|
233 | /* Set initial values. */
|
---|
234 | time_t sec = tm->tm_sec + sec_add;
|
---|
235 | time_t min = tm->tm_min;
|
---|
236 | time_t hour = tm->tm_hour;
|
---|
237 | time_t day = tm->tm_mday - 1;
|
---|
238 | time_t mon = tm->tm_mon;
|
---|
239 | time_t year = tm->tm_year;
|
---|
240 |
|
---|
241 | /* Adjust time. */
|
---|
242 | min += _floor_div(sec, SECS_PER_MIN);
|
---|
243 | sec = _floor_mod(sec, SECS_PER_MIN);
|
---|
244 | hour += _floor_div(min, MINS_PER_HOUR);
|
---|
245 | min = _floor_mod(min, MINS_PER_HOUR);
|
---|
246 | day += _floor_div(hour, HOURS_PER_DAY);
|
---|
247 | hour = _floor_mod(hour, HOURS_PER_DAY);
|
---|
248 |
|
---|
249 | /* Adjust month. */
|
---|
250 | year += _floor_div(mon, 12);
|
---|
251 | mon = _floor_mod(mon, 12);
|
---|
252 |
|
---|
253 | /* Now the difficult part - days of month. */
|
---|
254 |
|
---|
255 | /* First, deal with whole cycles of 400 years = 146097 days. */
|
---|
256 | year += _floor_div(day, 146097) * 400;
|
---|
257 | day = _floor_mod(day, 146097);
|
---|
258 |
|
---|
259 | /* Then, go in one year steps. */
|
---|
260 | if (mon <= 1) {
|
---|
261 | /* January and February. */
|
---|
262 | while (day > 365) {
|
---|
263 | day -= _is_leap_year(year) ? 366 : 365;
|
---|
264 | year++;
|
---|
265 | }
|
---|
266 | } else {
|
---|
267 | /* Rest of the year. */
|
---|
268 | while (day > 365) {
|
---|
269 | day -= _is_leap_year(year + 1) ? 366 : 365;
|
---|
270 | year++;
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | /* Finally, finish it off month per month. */
|
---|
275 | while (day >= _days_in_month(year, mon)) {
|
---|
276 | day -= _days_in_month(year, mon);
|
---|
277 | mon++;
|
---|
278 | if (mon >= 12) {
|
---|
279 | mon -= 12;
|
---|
280 | year++;
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | /* Calculate the remaining two fields. */
|
---|
285 | tm->tm_yday = _day_of_year(year, mon, day + 1);
|
---|
286 | tm->tm_wday = _day_of_week(year, mon, day + 1);
|
---|
287 |
|
---|
288 | /* And put the values back to the struct. */
|
---|
289 | tm->tm_sec = (int) sec;
|
---|
290 | tm->tm_min = (int) min;
|
---|
291 | tm->tm_hour = (int) hour;
|
---|
292 | tm->tm_mday = (int) day + 1;
|
---|
293 | tm->tm_mon = (int) mon;
|
---|
294 |
|
---|
295 | /* Casts to work around libc brain-damage. */
|
---|
296 | if (year > ((int)INT_MAX) || year < ((int)INT_MIN)) {
|
---|
297 | tm->tm_year = (year < 0) ? ((int)INT_MIN) : ((int)INT_MAX);
|
---|
298 | return -1;
|
---|
299 | }
|
---|
300 |
|
---|
301 | tm->tm_year = (int) year;
|
---|
302 | return 0;
|
---|
303 | }
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * Which day the week-based year starts on, relative to the first calendar day.
|
---|
307 | * E.g. if the year starts on December 31st, the return value is -1.
|
---|
308 | *
|
---|
309 | * @param Year since 1900.
|
---|
310 | * @return Offset of week-based year relative to calendar year.
|
---|
311 | */
|
---|
312 | static int _wbyear_offset(int year)
|
---|
313 | {
|
---|
314 | int start_wday = _day_of_week(year, 0, 1);
|
---|
315 | return _floor_mod(4 - start_wday, 7) - 3;
|
---|
316 | }
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * Returns week-based year of the specified time.
|
---|
320 | *
|
---|
321 | * @param tm Normalized broken-down time.
|
---|
322 | * @return Week-based year.
|
---|
323 | */
|
---|
324 | static int _wbyear(const struct tm *tm)
|
---|
325 | {
|
---|
326 | int day = tm->tm_yday - _wbyear_offset(tm->tm_year);
|
---|
327 | if (day < 0) {
|
---|
328 | /* Last week of previous year. */
|
---|
329 | return tm->tm_year - 1;
|
---|
330 | }
|
---|
331 | if (day > 364 + _is_leap_year(tm->tm_year)) {
|
---|
332 | /* First week of next year. */
|
---|
333 | return tm->tm_year + 1;
|
---|
334 | }
|
---|
335 | /* All the other days are in the calendar year. */
|
---|
336 | return tm->tm_year;
|
---|
337 | }
|
---|
338 |
|
---|
339 | /**
|
---|
340 | * Week number of the year, assuming weeks start on sunday.
|
---|
341 | * The first Sunday of January is the first day of week 1;
|
---|
342 | * days in the new year before this are in week 0.
|
---|
343 | *
|
---|
344 | * @param tm Normalized broken-down time.
|
---|
345 | * @return The week number (0 - 53).
|
---|
346 | */
|
---|
347 | static int _sun_week_number(const struct tm *tm)
|
---|
348 | {
|
---|
349 | int first_day = (7 - _day_of_week(tm->tm_year, 0, 1)) % 7;
|
---|
350 | return (tm->tm_yday - first_day + 7) / 7;
|
---|
351 | }
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Week number of the year, assuming weeks start on monday.
|
---|
355 | * If the week containing January 1st has four or more days in the new year,
|
---|
356 | * then it is considered week 1. Otherwise, it is the last week of the previous
|
---|
357 | * year, and the next week is week 1. Both January 4th and the first Thursday
|
---|
358 | * of January are always in week 1.
|
---|
359 | *
|
---|
360 | * @param tm Normalized broken-down time.
|
---|
361 | * @return The week number (1 - 53).
|
---|
362 | */
|
---|
363 | static int _iso_week_number(const struct tm *tm)
|
---|
364 | {
|
---|
365 | int day = tm->tm_yday - _wbyear_offset(tm->tm_year);
|
---|
366 | if (day < 0) {
|
---|
367 | /* Last week of previous year. */
|
---|
368 | return 53;
|
---|
369 | }
|
---|
370 | if (day > 364 + _is_leap_year(tm->tm_year)) {
|
---|
371 | /* First week of next year. */
|
---|
372 | return 1;
|
---|
373 | }
|
---|
374 | /* All the other days give correct answer. */
|
---|
375 | return (day / 7 + 1);
|
---|
376 | }
|
---|
377 |
|
---|
378 | /**
|
---|
379 | * Week number of the year, assuming weeks start on monday.
|
---|
380 | * The first Monday of January is the first day of week 1;
|
---|
381 | * days in the new year before this are in week 0.
|
---|
382 | *
|
---|
383 | * @param tm Normalized broken-down time.
|
---|
384 | * @return The week number (0 - 53).
|
---|
385 | */
|
---|
386 | static int _mon_week_number(const struct tm *tm)
|
---|
387 | {
|
---|
388 | int first_day = (1 - _day_of_week(tm->tm_year, 0, 1)) % 7;
|
---|
389 | return (tm->tm_yday - first_day + 7) / 7;
|
---|
390 | }
|
---|
391 |
|
---|
392 | /******************************************************************************/
|
---|
393 |
|
---|
394 |
|
---|
395 | /** Add microseconds to given timeval.
|
---|
396 | *
|
---|
397 | * @param tv Destination timeval.
|
---|
398 | * @param usecs Number of microseconds to add.
|
---|
399 | *
|
---|
400 | */
|
---|
401 | void tv_add(struct timeval *tv, suseconds_t usecs)
|
---|
402 | {
|
---|
403 | tv->tv_sec += usecs / 1000000;
|
---|
404 | tv->tv_usec += usecs % 1000000;
|
---|
405 |
|
---|
406 | if (tv->tv_usec > 1000000) {
|
---|
407 | tv->tv_sec++;
|
---|
408 | tv->tv_usec -= 1000000;
|
---|
409 | }
|
---|
410 | }
|
---|
411 |
|
---|
412 | /** Subtract two timevals.
|
---|
413 | *
|
---|
414 | * @param tv1 First timeval.
|
---|
415 | * @param tv2 Second timeval.
|
---|
416 | *
|
---|
417 | * @return Difference between tv1 and tv2 (tv1 - tv2) in
|
---|
418 | * microseconds.
|
---|
419 | *
|
---|
420 | */
|
---|
421 | suseconds_t tv_sub(struct timeval *tv1, struct timeval *tv2)
|
---|
422 | {
|
---|
423 | return (tv1->tv_usec - tv2->tv_usec) +
|
---|
424 | ((tv1->tv_sec - tv2->tv_sec) * 1000000);
|
---|
425 | }
|
---|
426 |
|
---|
427 | /** Decide if one timeval is greater than the other.
|
---|
428 | *
|
---|
429 | * @param t1 First timeval.
|
---|
430 | * @param t2 Second timeval.
|
---|
431 | *
|
---|
432 | * @return True if tv1 is greater than tv2.
|
---|
433 | * @return False otherwise.
|
---|
434 | *
|
---|
435 | */
|
---|
436 | int tv_gt(struct timeval *tv1, struct timeval *tv2)
|
---|
437 | {
|
---|
438 | if (tv1->tv_sec > tv2->tv_sec)
|
---|
439 | return true;
|
---|
440 |
|
---|
441 | if ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec > tv2->tv_usec))
|
---|
442 | return true;
|
---|
443 |
|
---|
444 | return false;
|
---|
445 | }
|
---|
446 |
|
---|
447 | /** Decide if one timeval is greater than or equal to the other.
|
---|
448 | *
|
---|
449 | * @param tv1 First timeval.
|
---|
450 | * @param tv2 Second timeval.
|
---|
451 | *
|
---|
452 | * @return True if tv1 is greater than or equal to tv2.
|
---|
453 | * @return False otherwise.
|
---|
454 | *
|
---|
455 | */
|
---|
456 | int tv_gteq(struct timeval *tv1, struct timeval *tv2)
|
---|
457 | {
|
---|
458 | if (tv1->tv_sec > tv2->tv_sec)
|
---|
459 | return true;
|
---|
460 |
|
---|
461 | if ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec >= tv2->tv_usec))
|
---|
462 | return true;
|
---|
463 |
|
---|
464 | return false;
|
---|
465 | }
|
---|
466 |
|
---|
467 | /** Get time of day
|
---|
468 | *
|
---|
469 | * The time variables are memory mapped (read-only) from kernel which
|
---|
470 | * updates them periodically.
|
---|
471 | *
|
---|
472 | * As it is impossible to read 2 values atomically, we use a trick:
|
---|
473 | * First we read the seconds, then we read the microseconds, then we
|
---|
474 | * read the seconds again. If a second elapsed in the meantime, set
|
---|
475 | * the microseconds to zero.
|
---|
476 | *
|
---|
477 | * This assures that the values returned by two subsequent calls
|
---|
478 | * to gettimeofday() are monotonous.
|
---|
479 | *
|
---|
480 | */
|
---|
481 | int gettimeofday(struct timeval *tv, struct timezone *tz)
|
---|
482 | {
|
---|
483 | if (ktime == NULL) {
|
---|
484 | uintptr_t faddr;
|
---|
485 | int rc = sysinfo_get_value("clock.faddr", &faddr);
|
---|
486 | if (rc != EOK) {
|
---|
487 | errno = rc;
|
---|
488 | return -1;
|
---|
489 | }
|
---|
490 |
|
---|
491 | void *addr;
|
---|
492 | rc = physmem_map((void *) faddr, 1,
|
---|
493 | AS_AREA_READ | AS_AREA_CACHEABLE, &addr);
|
---|
494 | if (rc != EOK) {
|
---|
495 | as_area_destroy(addr);
|
---|
496 | errno = rc;
|
---|
497 | return -1;
|
---|
498 | }
|
---|
499 |
|
---|
500 | ktime = addr;
|
---|
501 | }
|
---|
502 |
|
---|
503 | if (tz) {
|
---|
504 | tz->tz_minuteswest = 0;
|
---|
505 | tz->tz_dsttime = DST_NONE;
|
---|
506 | }
|
---|
507 |
|
---|
508 | sysarg_t s2 = ktime->seconds2;
|
---|
509 |
|
---|
510 | read_barrier();
|
---|
511 | tv->tv_usec = ktime->useconds;
|
---|
512 |
|
---|
513 | read_barrier();
|
---|
514 | sysarg_t s1 = ktime->seconds1;
|
---|
515 |
|
---|
516 | if (s1 != s2) {
|
---|
517 | tv->tv_sec = max(s1, s2);
|
---|
518 | tv->tv_usec = 0;
|
---|
519 | } else
|
---|
520 | tv->tv_sec = s1;
|
---|
521 |
|
---|
522 | return 0;
|
---|
523 | }
|
---|
524 |
|
---|
525 | time_t time(time_t *tloc)
|
---|
526 | {
|
---|
527 | struct timeval tv;
|
---|
528 | if (gettimeofday(&tv, NULL))
|
---|
529 | return (time_t) -1;
|
---|
530 |
|
---|
531 | if (tloc)
|
---|
532 | *tloc = tv.tv_sec;
|
---|
533 |
|
---|
534 | return tv.tv_sec;
|
---|
535 | }
|
---|
536 |
|
---|
537 | /** Wait unconditionally for specified number of microseconds
|
---|
538 | *
|
---|
539 | */
|
---|
540 | int usleep(useconds_t usec)
|
---|
541 | {
|
---|
542 | (void) __SYSCALL1(SYS_THREAD_USLEEP, usec);
|
---|
543 | return 0;
|
---|
544 | }
|
---|
545 |
|
---|
546 | void udelay(useconds_t time)
|
---|
547 | {
|
---|
548 | (void) __SYSCALL1(SYS_THREAD_UDELAY, (sysarg_t) time);
|
---|
549 | }
|
---|
550 |
|
---|
551 |
|
---|
552 | /** Wait unconditionally for specified number of seconds
|
---|
553 | *
|
---|
554 | */
|
---|
555 | unsigned int sleep(unsigned int sec)
|
---|
556 | {
|
---|
557 | /*
|
---|
558 | * Sleep in 1000 second steps to support
|
---|
559 | * full argument range
|
---|
560 | */
|
---|
561 |
|
---|
562 | while (sec > 0) {
|
---|
563 | unsigned int period = (sec > 1000) ? 1000 : sec;
|
---|
564 |
|
---|
565 | usleep(period * 1000000);
|
---|
566 | sec -= period;
|
---|
567 | }
|
---|
568 |
|
---|
569 | return 0;
|
---|
570 | }
|
---|
571 |
|
---|
572 | /**
|
---|
573 | * This function first normalizes the provided broken-down time
|
---|
574 | * (moves all values to their proper bounds) and then tries to
|
---|
575 | * calculate the appropriate time_t representation.
|
---|
576 | *
|
---|
577 | * @param tm Broken-down time.
|
---|
578 | * @return time_t representation of the time, undefined value on overflow.
|
---|
579 | */
|
---|
580 | time_t mktime(struct tm *tm)
|
---|
581 | {
|
---|
582 | // TODO: take DST flag into account
|
---|
583 | // TODO: detect overflow
|
---|
584 |
|
---|
585 | _normalize_time(tm, 0);
|
---|
586 | return _secs_since_epoch(tm);
|
---|
587 | }
|
---|
588 |
|
---|
589 | /**
|
---|
590 | * Convert time and date to a string, based on a specified format and
|
---|
591 | * current locale.
|
---|
592 | *
|
---|
593 | * @param s Buffer to write string to.
|
---|
594 | * @param maxsize Size of the buffer.
|
---|
595 | * @param format Format of the output.
|
---|
596 | * @param tm Broken-down time to format.
|
---|
597 | * @return Number of bytes written.
|
---|
598 | */
|
---|
599 | size_t strftime(char *restrict s, size_t maxsize,
|
---|
600 | const char *restrict format, const struct tm *restrict tm)
|
---|
601 | {
|
---|
602 | assert(s != NULL);
|
---|
603 | assert(format != NULL);
|
---|
604 | assert(tm != NULL);
|
---|
605 |
|
---|
606 | // TODO: use locale
|
---|
607 | static const char *wday_abbr[] = {
|
---|
608 | "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
---|
609 | };
|
---|
610 | static const char *wday[] = {
|
---|
611 | "Sunday", "Monday", "Tuesday", "Wednesday",
|
---|
612 | "Thursday", "Friday", "Saturday"
|
---|
613 | };
|
---|
614 | static const char *mon_abbr[] = {
|
---|
615 | "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
---|
616 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
---|
617 | };
|
---|
618 | static const char *mon[] = {
|
---|
619 | "January", "February", "March", "April", "May", "June", "July",
|
---|
620 | "August", "September", "October", "November", "December"
|
---|
621 | };
|
---|
622 |
|
---|
623 | if (maxsize < 1) {
|
---|
624 | return 0;
|
---|
625 | }
|
---|
626 |
|
---|
627 | char *ptr = s;
|
---|
628 | size_t consumed;
|
---|
629 | size_t remaining = maxsize;
|
---|
630 |
|
---|
631 | #define append(...) { \
|
---|
632 | /* FIXME: this requires POSIX-correct snprintf */ \
|
---|
633 | /* otherwise it won't work with non-ascii chars */ \
|
---|
634 | consumed = snprintf(ptr, remaining, __VA_ARGS__); \
|
---|
635 | if (consumed >= remaining) { \
|
---|
636 | return 0; \
|
---|
637 | } \
|
---|
638 | ptr += consumed; \
|
---|
639 | remaining -= consumed; \
|
---|
640 | }
|
---|
641 |
|
---|
642 | #define recurse(fmt) { \
|
---|
643 | consumed = strftime(ptr, remaining, fmt, tm); \
|
---|
644 | if (consumed == 0) { \
|
---|
645 | return 0; \
|
---|
646 | } \
|
---|
647 | ptr += consumed; \
|
---|
648 | remaining -= consumed; \
|
---|
649 | }
|
---|
650 |
|
---|
651 | #define TO_12H(hour) (((hour) > 12) ? ((hour) - 12) : \
|
---|
652 | (((hour) == 0) ? 12 : (hour)))
|
---|
653 |
|
---|
654 | while (*format != '\0') {
|
---|
655 | if (*format != '%') {
|
---|
656 | append("%c", *format);
|
---|
657 | format++;
|
---|
658 | continue;
|
---|
659 | }
|
---|
660 |
|
---|
661 | format++;
|
---|
662 | if (*format == '0' || *format == '+') {
|
---|
663 | // TODO: padding
|
---|
664 | format++;
|
---|
665 | }
|
---|
666 | while (isdigit(*format)) {
|
---|
667 | // TODO: padding
|
---|
668 | format++;
|
---|
669 | }
|
---|
670 | if (*format == 'O' || *format == 'E') {
|
---|
671 | // TODO: locale's alternative format
|
---|
672 | format++;
|
---|
673 | }
|
---|
674 |
|
---|
675 | switch (*format) {
|
---|
676 | case 'a':
|
---|
677 | append("%s", wday_abbr[tm->tm_wday]); break;
|
---|
678 | case 'A':
|
---|
679 | append("%s", wday[tm->tm_wday]); break;
|
---|
680 | case 'b':
|
---|
681 | append("%s", mon_abbr[tm->tm_mon]); break;
|
---|
682 | case 'B':
|
---|
683 | append("%s", mon[tm->tm_mon]); break;
|
---|
684 | case 'c':
|
---|
685 | // TODO: locale-specific datetime format
|
---|
686 | recurse("%Y-%m-%d %H:%M:%S"); break;
|
---|
687 | case 'C':
|
---|
688 | append("%02d", (1900 + tm->tm_year) / 100); break;
|
---|
689 | case 'd':
|
---|
690 | append("%02d", tm->tm_mday); break;
|
---|
691 | case 'D':
|
---|
692 | recurse("%m/%d/%y"); break;
|
---|
693 | case 'e':
|
---|
694 | append("%2d", tm->tm_mday); break;
|
---|
695 | case 'F':
|
---|
696 | recurse("%+4Y-%m-%d"); break;
|
---|
697 | case 'g':
|
---|
698 | append("%02d", _wbyear(tm) % 100); break;
|
---|
699 | case 'G':
|
---|
700 | append("%d", _wbyear(tm)); break;
|
---|
701 | case 'h':
|
---|
702 | recurse("%b"); break;
|
---|
703 | case 'H':
|
---|
704 | append("%02d", tm->tm_hour); break;
|
---|
705 | case 'I':
|
---|
706 | append("%02d", TO_12H(tm->tm_hour)); break;
|
---|
707 | case 'j':
|
---|
708 | append("%03d", tm->tm_yday); break;
|
---|
709 | case 'k':
|
---|
710 | append("%2d", tm->tm_hour); break;
|
---|
711 | case 'l':
|
---|
712 | append("%2d", TO_12H(tm->tm_hour)); break;
|
---|
713 | case 'm':
|
---|
714 | append("%02d", tm->tm_mon); break;
|
---|
715 | case 'M':
|
---|
716 | append("%02d", tm->tm_min); break;
|
---|
717 | case 'n':
|
---|
718 | append("\n"); break;
|
---|
719 | case 'p':
|
---|
720 | append("%s", tm->tm_hour < 12 ? "AM" : "PM"); break;
|
---|
721 | case 'P':
|
---|
722 | append("%s", tm->tm_hour < 12 ? "am" : "PM"); break;
|
---|
723 | case 'r':
|
---|
724 | recurse("%I:%M:%S %p"); break;
|
---|
725 | case 'R':
|
---|
726 | recurse("%H:%M"); break;
|
---|
727 | case 's':
|
---|
728 | append("%ld", _secs_since_epoch(tm)); break;
|
---|
729 | case 'S':
|
---|
730 | append("%02d", tm->tm_sec); break;
|
---|
731 | case 't':
|
---|
732 | append("\t"); break;
|
---|
733 | case 'T':
|
---|
734 | recurse("%H:%M:%S"); break;
|
---|
735 | case 'u':
|
---|
736 | append("%d", (tm->tm_wday == 0) ? 7 : tm->tm_wday);
|
---|
737 | break;
|
---|
738 | case 'U':
|
---|
739 | append("%02d", _sun_week_number(tm)); break;
|
---|
740 | case 'V':
|
---|
741 | append("%02d", _iso_week_number(tm)); break;
|
---|
742 | case 'w':
|
---|
743 | append("%d", tm->tm_wday); break;
|
---|
744 | case 'W':
|
---|
745 | append("%02d", _mon_week_number(tm)); break;
|
---|
746 | case 'x':
|
---|
747 | // TODO: locale-specific date format
|
---|
748 | recurse("%Y-%m-%d"); break;
|
---|
749 | case 'X':
|
---|
750 | // TODO: locale-specific time format
|
---|
751 | recurse("%H:%M:%S"); break;
|
---|
752 | case 'y':
|
---|
753 | append("%02d", tm->tm_year % 100); break;
|
---|
754 | case 'Y':
|
---|
755 | append("%d", 1900 + tm->tm_year); break;
|
---|
756 | case 'z':
|
---|
757 | // TODO: timezone
|
---|
758 | break;
|
---|
759 | case 'Z':
|
---|
760 | // TODO: timezone
|
---|
761 | break;
|
---|
762 | case '%':
|
---|
763 | append("%%");
|
---|
764 | break;
|
---|
765 | default:
|
---|
766 | /* Invalid specifier, print verbatim. */
|
---|
767 | while (*format != '%') {
|
---|
768 | format--;
|
---|
769 | }
|
---|
770 | append("%%");
|
---|
771 | break;
|
---|
772 | }
|
---|
773 | format++;
|
---|
774 | }
|
---|
775 |
|
---|
776 | #undef append
|
---|
777 | #undef recurse
|
---|
778 |
|
---|
779 | return maxsize - remaining;
|
---|
780 | }
|
---|
781 |
|
---|
782 | struct tm *gmtime(const time_t *timer)
|
---|
783 | {
|
---|
784 | assert(timer != NULL);
|
---|
785 |
|
---|
786 | static struct tm result;
|
---|
787 |
|
---|
788 | /* Set result to epoch. */
|
---|
789 | result.tm_sec = 0;
|
---|
790 | result.tm_min = 0;
|
---|
791 | result.tm_hour = 0;
|
---|
792 | result.tm_mday = 1;
|
---|
793 | result.tm_mon = 0;
|
---|
794 | result.tm_year = 70; /* 1970 */
|
---|
795 |
|
---|
796 | if (_normalize_time(&result, *timer) == -1) {
|
---|
797 | errno = EOVERFLOW;
|
---|
798 | return NULL;
|
---|
799 | }
|
---|
800 |
|
---|
801 | return &result;
|
---|
802 | }
|
---|
803 |
|
---|
804 | /**
|
---|
805 | * Converts broken-down time to a string in format
|
---|
806 | * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
|
---|
807 | *
|
---|
808 | * @param timeptr Broken-down time structure.
|
---|
809 | * @return Pointer to a statically allocated string.
|
---|
810 | */
|
---|
811 | char *asctime(const struct tm *timeptr)
|
---|
812 | {
|
---|
813 | static char buf[ASCTIME_BUF_LEN];
|
---|
814 |
|
---|
815 | assert(timeptr != NULL);
|
---|
816 |
|
---|
817 | static const char *wday[] = {
|
---|
818 | "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
---|
819 | };
|
---|
820 | static const char *mon[] = {
|
---|
821 | "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
---|
822 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
---|
823 | };
|
---|
824 |
|
---|
825 | snprintf(buf, ASCTIME_BUF_LEN, "%s %s %2d %02d:%02d:%02d %d\n",
|
---|
826 | wday[timeptr->tm_wday],
|
---|
827 | mon[timeptr->tm_mon],
|
---|
828 | timeptr->tm_mday, timeptr->tm_hour,
|
---|
829 | timeptr->tm_min, timeptr->tm_sec,
|
---|
830 | 1900 + timeptr->tm_year);
|
---|
831 |
|
---|
832 | return buf;
|
---|
833 |
|
---|
834 | }
|
---|
835 |
|
---|
836 | /**
|
---|
837 | * Converts a time value to a broken-down local time.
|
---|
838 | *
|
---|
839 | * @param timer Time to convert.
|
---|
840 | * @return Normalized broken-down time in local timezone, NULL on overflow.
|
---|
841 | */
|
---|
842 | struct tm *localtime(const time_t *timer)
|
---|
843 | {
|
---|
844 | // TODO: deal with timezone
|
---|
845 | // currently assumes system and all times are in GMT
|
---|
846 |
|
---|
847 | static struct tm result;
|
---|
848 |
|
---|
849 | /* Set result to epoch. */
|
---|
850 | result.tm_sec = 0;
|
---|
851 | result.tm_min = 0;
|
---|
852 | result.tm_hour = 0;
|
---|
853 | result.tm_mday = 1;
|
---|
854 | result.tm_mon = 0;
|
---|
855 | result.tm_year = 70; /* 1970 */
|
---|
856 |
|
---|
857 | if (_normalize_time(&result, *timer) == -1) {
|
---|
858 | errno = EOVERFLOW;
|
---|
859 | return NULL;
|
---|
860 | }
|
---|
861 |
|
---|
862 | return &result;
|
---|
863 | }
|
---|
864 |
|
---|
865 | /**
|
---|
866 | * Equivalent to asctime(localtime(clock)).
|
---|
867 | *
|
---|
868 | * @param timer Time to convert.
|
---|
869 | * @return Pointer to a statically allocated string holding the date.
|
---|
870 | */
|
---|
871 | char *ctime(const time_t *timer)
|
---|
872 | {
|
---|
873 | struct tm *loctime = localtime(timer);
|
---|
874 | if (loctime == NULL) {
|
---|
875 | return NULL;
|
---|
876 | }
|
---|
877 | return asctime(loctime);
|
---|
878 | }
|
---|
879 |
|
---|
880 | /**
|
---|
881 | * Calculate the difference between two times, in seconds.
|
---|
882 | *
|
---|
883 | * @param time1 First time.
|
---|
884 | * @param time0 Second time.
|
---|
885 | * @return Time in seconds.
|
---|
886 | */
|
---|
887 | double difftime(time_t time1, time_t time0)
|
---|
888 | {
|
---|
889 | return (double) (time1 - time0);
|
---|
890 | }
|
---|
891 |
|
---|
892 | /** @}
|
---|
893 | */
|
---|