source: mainline/uspace/lib/posix/time.c@ 4cf8ca6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4cf8ca6 was 4cf8ca6, checked in by Petr Koupy <petr.koupy@…>, 14 years ago

Various minor commenting (no change in functionality).

  • Property mode set to 100644
File size: 20.2 KB
Line 
1/*
2 * Copyright (c) 2011 Petr Koupy
3 * Copyright (c) 2011 Jiri Zarevucky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libposix
31 * @{
32 */
33/** @file Time measurement support.
34 */
35
36#define LIBPOSIX_INTERNAL
37
38/* Must be first. */
39#include "stdbool.h"
40
41#include "internal/common.h"
42#include "time.h"
43
44#include "ctype.h"
45#include "errno.h"
46#include "signal.h"
47
48#include "libc/malloc.h"
49#include "libc/task.h"
50#include "libc/stats.h"
51#include "libc/sys/time.h"
52
53// TODO: documentation
54// TODO: test everything in this file
55
56/* Helper functions ***********************************************************/
57
58#define HOURS_PER_DAY (24)
59#define MINS_PER_HOUR (60)
60#define SECS_PER_MIN (60)
61#define MINS_PER_DAY (MINS_PER_HOUR * HOURS_PER_DAY)
62#define SECS_PER_HOUR (SECS_PER_MIN * MINS_PER_HOUR)
63#define SECS_PER_DAY (SECS_PER_HOUR * HOURS_PER_DAY)
64
65/**
66 *
67 * @param year
68 * @return
69 */
70static bool _is_leap_year(time_t year)
71{
72 year += 1900;
73
74 if (year % 400 == 0)
75 return true;
76 if (year % 100 == 0)
77 return false;
78 if (year % 4 == 0)
79 return true;
80 return false;
81}
82
83/**
84 *
85 * @param year
86 * @param mon
87 * @return
88 */
89static int _days_in_month(time_t year, time_t mon)
90{
91 assert(mon >= 0 && mon <= 11);
92 year += 1900;
93
94 static int month_days[] =
95 { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
96
97 if (mon == 1) {
98 /* february */
99 return _is_leap_year(year) ? 29 : 28;
100 } else {
101 return month_days[mon];
102 }
103}
104
105/**
106 *
107 * @param year
108 * @param mon
109 * @param mday
110 * @return
111 */
112static int _day_of_year(time_t year, time_t mon, time_t mday)
113{
114 static int mdays[] =
115 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
116 static int leap_mdays[] =
117 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
118
119 return (_is_leap_year(year) ? leap_mdays[mon] : mdays[mon]) + mday - 1;
120}
121
122/**
123 * Integer division that rounds to negative infinity.
124 *
125 * @param op1
126 * @param op2
127 * @return
128 */
129static time_t _floor_div(time_t op1, time_t op2)
130{
131 if (op1 >= 0 || op1 % op2 == 0) {
132 return op1 / op2;
133 } else {
134 return op1 / op2 - 1;
135 }
136}
137
138/**
139 * Modulo that rounds to negative infinity.
140 *
141 * @param op1
142 * @param op2
143 * @return
144 */
145static time_t _floor_mod(time_t op1, time_t op2)
146{
147 int div = _floor_div(op1, op2);
148
149 /* (a / b) * b + a % b == a */
150 /* thus, a % b == a - (a / b) * b */
151
152 int result = op1 - div * op2;
153
154 /* Some paranoid checking to ensure I didn't make a mistake here. */
155 assert(result >= 0);
156 assert(result < op2);
157 assert(div * op2 + result == op1);
158
159 return result;
160}
161
162/**
163 *
164 * @param year
165 * @param mon
166 * @param mday
167 * @return
168 */
169static time_t _days_since_epoch(time_t year, time_t mon, time_t mday)
170{
171 return (year - 70) * 365 + _floor_div(year - 69, 4) -
172 _floor_div(year - 1, 100) + _floor_div(year + 299, 400) +
173 _day_of_year(year, mon, mday);
174}
175
176/**
177 * Assumes normalized broken-down time.
178 *
179 * @param tm
180 * @return
181 */
182static time_t _secs_since_epoch(const struct posix_tm *tm)
183{
184 return _days_since_epoch(tm->tm_year, tm->tm_mon, tm->tm_mday) *
185 SECS_PER_DAY + tm->tm_hour * SECS_PER_HOUR +
186 tm->tm_min * SECS_PER_MIN + tm->tm_sec;
187}
188
189/**
190 *
191 * @param year
192 * @param mon
193 * @param mday
194 * @return
195 */
196static int _day_of_week(time_t year, time_t mon, time_t mday)
197{
198 /* 1970-01-01 is Thursday */
199 return (_days_since_epoch(year, mon, mday) + 4) % 7;
200}
201
202struct _long_tm {
203 time_t tm_sec;
204 time_t tm_min;
205 time_t tm_hour;
206 time_t tm_mday;
207 time_t tm_mon;
208 time_t tm_year;
209 int tm_wday;
210 int tm_yday;
211 int tm_isdst;
212};
213
214/**
215 *
216 * @param ltm
217 * @param ptm
218 */
219static void _posix_to_long_tm(struct _long_tm *ltm, struct posix_tm *ptm)
220{
221 assert(ltm != NULL && ptm != NULL);
222 ltm->tm_sec = ptm->tm_sec;
223 ltm->tm_min = ptm->tm_min;
224 ltm->tm_hour = ptm->tm_hour;
225 ltm->tm_mday = ptm->tm_mday;
226 ltm->tm_mon = ptm->tm_mon;
227 ltm->tm_year = ptm->tm_year;
228 ltm->tm_wday = ptm->tm_wday;
229 ltm->tm_yday = ptm->tm_yday;
230 ltm->tm_isdst = ptm->tm_isdst;
231}
232
233/**
234 *
235 * @param ptm
236 * @param ltm
237 */
238static void _long_to_posix_tm(struct posix_tm *ptm, struct _long_tm *ltm)
239{
240 assert(ltm != NULL && ptm != NULL);
241 // FIXME: the cast should be unnecessary, libarch/common.h brain-damage
242 assert((ltm->tm_year >= (int) INT_MIN) && (ltm->tm_year <= (int) INT_MAX));
243
244 ptm->tm_sec = ltm->tm_sec;
245 ptm->tm_min = ltm->tm_min;
246 ptm->tm_hour = ltm->tm_hour;
247 ptm->tm_mday = ltm->tm_mday;
248 ptm->tm_mon = ltm->tm_mon;
249 ptm->tm_year = ltm->tm_year;
250 ptm->tm_wday = ltm->tm_wday;
251 ptm->tm_yday = ltm->tm_yday;
252 ptm->tm_isdst = ltm->tm_isdst;
253}
254
255/**
256 *
257 * @param tm
258 */
259static void _normalize_time(struct _long_tm *tm)
260{
261 // TODO: DST correction
262
263 /* Adjust time. */
264 tm->tm_min += _floor_div(tm->tm_sec, SECS_PER_MIN);
265 tm->tm_sec = _floor_mod(tm->tm_sec, SECS_PER_MIN);
266 tm->tm_hour += _floor_div(tm->tm_min, MINS_PER_HOUR);
267 tm->tm_min = _floor_mod(tm->tm_min, MINS_PER_HOUR);
268 tm->tm_mday += _floor_div(tm->tm_hour, HOURS_PER_DAY);
269 tm->tm_hour = _floor_mod(tm->tm_hour, HOURS_PER_DAY);
270
271 /* Adjust month. */
272 tm->tm_year += _floor_div(tm->tm_mon, 12);
273 tm->tm_mon = _floor_mod(tm->tm_mon, 12);
274
275 /* Now the difficult part - days of month. */
276 /* Slow, but simple. */
277 // FIXME: do this faster
278
279 while (tm->tm_mday < 1) {
280 tm->tm_mon--;
281 if (tm->tm_mon == -1) {
282 tm->tm_mon = 11;
283 tm->tm_year--;
284 }
285
286 tm->tm_mday += _days_in_month(tm->tm_year, tm->tm_mon);
287 }
288
289 while (tm->tm_mday > _days_in_month(tm->tm_year, tm->tm_mon)) {
290 tm->tm_mday -= _days_in_month(tm->tm_year, tm->tm_mon);
291
292 tm->tm_mon++;
293 if (tm->tm_mon == 12) {
294 tm->tm_mon = 0;
295 tm->tm_year++;
296 }
297 }
298
299 /* Calculate the remaining two fields. */
300 tm->tm_yday = _day_of_year(tm->tm_year, tm->tm_mon, tm->tm_mday);
301 tm->tm_wday = _day_of_week(tm->tm_year, tm->tm_mon, tm->tm_mday);
302}
303
304/**
305 * Which day the week-based year starts on relative to the first calendar day.
306 * E.g. if the year starts on December 31st, the return value is -1.
307 *
308 * @param year
309 * @return
310 */
311static int _wbyear_offset(int year)
312{
313 int start_wday = _day_of_week(year, 0, 1);
314 return _floor_mod(4 - start_wday, 7) - 3;
315}
316
317/**
318 * Returns week-based year of the specified time.
319 * Assumes normalized broken-down time.
320 *
321 * @param tm
322 * @return
323 */
324static int _wbyear(const struct posix_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 */
347static int _sun_week_number(const struct posix_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 */
363static int _iso_week_number(const struct posix_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 */
386static int _mon_week_number(const struct posix_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
394int posix_daylight;
395long posix_timezone;
396char *posix_tzname[2];
397
398/**
399 *
400 */
401void posix_tzset(void)
402{
403 // TODO: read environment
404 posix_tzname[0] = (char *) "GMT";
405 posix_tzname[1] = (char *) "GMT";
406 posix_daylight = 0;
407 posix_timezone = 0;
408}
409
410/**
411 *
412 * @param time1
413 * @param time0
414 * @return
415 */
416double posix_difftime(time_t time1, time_t time0)
417{
418 return (double) (time1 - time0);
419}
420
421/**
422 * This function first normalizes the provided broken-down time
423 * (moves all values to their proper bounds) and then tries to
424 * calculate the appropriate time_t representation.
425 *
426 * @param tm Broken-down time.
427 * @return time_t representation of the time, undefined value on overflow
428 */
429time_t posix_mktime(struct posix_tm *tm)
430{
431 // TODO: take DST flag into account
432 // TODO: detect overflow
433
434 struct _long_tm ltm;
435 _posix_to_long_tm(&ltm, tm);
436 _normalize_time(&ltm);
437 _long_to_posix_tm(tm, &ltm);
438
439 return _secs_since_epoch(tm);
440}
441
442/**
443 *
444 * @param timer
445 * @return
446 */
447struct posix_tm *posix_gmtime(const time_t *timer)
448{
449 static struct posix_tm result;
450 return posix_gmtime_r(timer, &result);
451}
452
453/**
454 *
455 * @param timer
456 * @param result
457 * @return
458 */
459struct posix_tm *posix_gmtime_r(const time_t *restrict timer,
460 struct posix_tm *restrict result)
461{
462 assert(timer != NULL);
463 assert(result != NULL);
464
465 /* Set epoch and seconds to _long_tm struct and normalize to get
466 * correct values.
467 */
468 struct _long_tm ltm = {
469 .tm_sec = *timer,
470 .tm_min = 0,
471 .tm_hour = 0, /* 00:00:xx */
472 .tm_mday = 1,
473 .tm_mon = 0, /* January 1st */
474 .tm_year = 70, /* 1970 */
475 };
476 _normalize_time(&ltm);
477
478 if (ltm.tm_year < (int) INT_MIN || ltm.tm_year > (int) INT_MAX) {
479 errno = EOVERFLOW;
480 return NULL;
481 }
482
483 _long_to_posix_tm(result, &ltm);
484 return result;
485}
486
487/**
488 *
489 * @param timer
490 * @return
491 */
492struct posix_tm *posix_localtime(const time_t *timer)
493{
494 static struct posix_tm result;
495 return posix_localtime_r(timer, &result);
496}
497
498/**
499 *
500 * @param timer
501 * @param result
502 * @return
503 */
504struct posix_tm *posix_localtime_r(const time_t *restrict timer,
505 struct posix_tm *restrict result)
506{
507 // TODO: deal with timezone
508 // currently assumes system and all times are in GMT
509 return posix_gmtime_r(timer, result);
510}
511
512/**
513 *
514 * @param timeptr
515 * @return
516 */
517char *posix_asctime(const struct posix_tm *timeptr)
518{
519 static char buf[ASCTIME_BUF_LEN];
520 return posix_asctime_r(timeptr, buf);
521}
522
523/**
524 *
525 * @param timeptr
526 * @param buf
527 * @return
528 */
529char *posix_asctime_r(const struct posix_tm *restrict timeptr,
530 char *restrict buf)
531{
532 assert(timeptr != NULL);
533 assert(buf != NULL);
534
535 static const char *wday[] = {
536 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
537 };
538 static const char *mon[] = {
539 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
540 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
541 };
542
543 snprintf(buf, ASCTIME_BUF_LEN, "%s %s %2d %02d:%02d:%02d %d\n",
544 wday[timeptr->tm_wday],
545 mon[timeptr->tm_mon],
546 timeptr->tm_mday, timeptr->tm_hour,
547 timeptr->tm_min, timeptr->tm_sec,
548 1900 + timeptr->tm_year);
549
550 return buf;
551}
552
553/**
554 *
555 * @param timer
556 * @return
557 */
558char *posix_ctime(const time_t *timer)
559{
560 struct posix_tm *loctime = posix_localtime(timer);
561 if (loctime == NULL) {
562 return NULL;
563 }
564 return posix_asctime(loctime);
565}
566
567/**
568 *
569 * @param timer
570 * @param buf
571 * @return
572 */
573char *posix_ctime_r(const time_t *timer, char *buf)
574{
575 struct posix_tm loctime;
576 if (posix_localtime_r(timer, &loctime) == NULL) {
577 return NULL;
578 }
579 return posix_asctime_r(&loctime, buf);
580}
581
582/**
583 *
584 * @param s
585 * @param maxsize
586 * @param format
587 * @param tm
588 * @return
589 */
590size_t posix_strftime(char *restrict s, size_t maxsize,
591 const char *restrict format, const struct posix_tm *restrict tm)
592{
593 // TODO: use locale
594 static const char *wday_abbr[] = {
595 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
596 };
597 static const char *wday[] = {
598 "Sunday", "Monday", "Tuesday", "Wednesday",
599 "Thursday", "Friday", "Saturday"
600 };
601 static const char *mon_abbr[] = {
602 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
603 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
604 };
605 static const char *mon[] = {
606 "January", "February", "March", "April", "May", "June", "July",
607 "August", "September", "October", "November", "December"
608 };
609
610 if (maxsize < 1) {
611 return 0;
612 }
613
614 char *ptr = s;
615 size_t consumed;
616 size_t remaining = maxsize;
617
618 #define append(...) { \
619 /* FIXME: this requires POSIX-correct snprintf */ \
620 /* otherwise it won't work with non-ascii chars */ \
621 consumed = snprintf(ptr, remaining, __VA_ARGS__); \
622 if (consumed >= remaining) { \
623 return 0; \
624 } \
625 ptr += consumed; \
626 remaining -= consumed; \
627 }
628
629 #define recurse(fmt) { \
630 consumed = posix_strftime(ptr, remaining, fmt, tm); \
631 if (consumed == 0) { \
632 return 0; \
633 } \
634 ptr += consumed; \
635 remaining -= consumed; \
636 }
637
638 #define TO_12H(hour) (((hour) > 12) ? ((hour) - 12) : \
639 (((hour) == 0) ? 12 : (hour)))
640
641 while (*format != '\0') {
642 if (*format != '%') {
643 append("%c", *format);
644 format++;
645 continue;
646 }
647
648 format++;
649 if (*format == '0' || *format == '+') {
650 // TODO: padding
651 format++;
652 }
653 while (isdigit(*format)) {
654 // TODO: padding
655 format++;
656 }
657 if (*format == 'O' || *format == 'E') {
658 // TODO: locale's alternative format
659 format++;
660 }
661
662 switch (*format) {
663 case 'a':
664 append("%s", wday_abbr[tm->tm_wday]); break;
665 case 'A':
666 append("%s", wday[tm->tm_wday]); break;
667 case 'b':
668 append("%s", mon_abbr[tm->tm_mon]); break;
669 case 'B':
670 append("%s", mon[tm->tm_mon]); break;
671 case 'c':
672 // TODO: locale-specific datetime format
673 recurse("%Y-%m-%d %H:%M:%S"); break;
674 case 'C':
675 append("%02d", (1900 + tm->tm_year) / 100); break;
676 case 'd':
677 append("%02d", tm->tm_mday); break;
678 case 'D':
679 recurse("%m/%d/%y"); break;
680 case 'e':
681 append("%2d", tm->tm_mday); break;
682 case 'F':
683 recurse("%+4Y-%m-%d"); break;
684 case 'g':
685 append("%02d", _wbyear(tm) % 100); break;
686 case 'G':
687 append("%d", _wbyear(tm)); break;
688 case 'h':
689 recurse("%b"); break;
690 case 'H':
691 append("%02d", tm->tm_hour); break;
692 case 'I':
693 append("%02d", TO_12H(tm->tm_hour)); break;
694 case 'j':
695 append("%03d", tm->tm_yday); break;
696 case 'k':
697 append("%2d", tm->tm_hour); break;
698 case 'l':
699 append("%2d", TO_12H(tm->tm_hour)); break;
700 case 'm':
701 append("%02d", tm->tm_mon); break;
702 case 'M':
703 append("%02d", tm->tm_min); break;
704 case 'n':
705 append("\n"); break;
706 case 'p':
707 append("%s", tm->tm_hour < 12 ? "AM" : "PM"); break;
708 case 'P':
709 append("%s", tm->tm_hour < 12 ? "am" : "PM"); break;
710 case 'r':
711 recurse("%I:%M:%S %p"); break;
712 case 'R':
713 recurse("%H:%M"); break;
714 case 's':
715 append("%ld", _secs_since_epoch(tm)); break;
716 case 'S':
717 append("%02d", tm->tm_sec); break;
718 case 't':
719 append("\t"); break;
720 case 'T':
721 recurse("%H:%M:%S"); break;
722 case 'u':
723 append("%d", (tm->tm_wday == 0) ? 7 : tm->tm_wday); break;
724 case 'U':
725 append("%02d", _sun_week_number(tm)); break;
726 case 'V':
727 append("%02d", _iso_week_number(tm)); break;
728 case 'w':
729 append("%d", tm->tm_wday); break;
730 case 'W':
731 append("%02d", _mon_week_number(tm)); break;
732 case 'x':
733 // TODO: locale-specific date format
734 recurse("%Y-%m-%d"); break;
735 case 'X':
736 // TODO: locale-specific time format
737 recurse("%H:%M:%S"); break;
738 case 'y':
739 append("%02d", tm->tm_year % 100); break;
740 case 'Y':
741 append("%d", 1900 + tm->tm_year); break;
742 case 'z':
743 // TODO: timezone
744 break;
745 case 'Z':
746 // TODO: timezone
747 break;
748 case '%':
749 append("%%");
750 break;
751 default:
752 /* Invalid specifier, print verbatim. */
753 while (*format != '%') {
754 format--;
755 }
756 append("%%");
757 break;
758 }
759 format++;
760 }
761
762 #undef append
763 #undef recurse
764
765 return maxsize - remaining;
766}
767
768/**
769 *
770 * @param s
771 * @param maxsize
772 * @param format
773 * @param tm
774 * @param loc
775 * @return
776 */
777extern size_t posix_strftime_l(char *restrict s, size_t maxsize,
778 const char *restrict format, const struct posix_tm *restrict tm,
779 posix_locale_t loc)
780{
781 // TODO
782 not_implemented();
783}
784
785/**
786 *
787 * @param clock_id
788 * @param res
789 * @return
790 */
791int posix_clock_getres(posix_clockid_t clock_id, struct posix_timespec *res)
792{
793 assert(res != NULL);
794
795 switch (clock_id) {
796 case CLOCK_REALTIME:
797 res->tv_sec = 0;
798 res->tv_nsec = 1000; /* Microsecond resolution. */
799 return 0;
800 default:
801 errno = EINVAL;
802 return -1;
803 }
804}
805
806/**
807 *
808 * @param clock_id
809 * @param tp
810 * @return
811 */
812int posix_clock_gettime(posix_clockid_t clock_id, struct posix_timespec *tp)
813{
814 assert(tp != NULL);
815
816 switch (clock_id) {
817 case CLOCK_REALTIME:
818 ;
819 struct timeval tv;
820 gettimeofday(&tv, NULL);
821 tp->tv_sec = tv.tv_sec;
822 tp->tv_nsec = tv.tv_usec * 1000;
823 return 0;
824 default:
825 errno = EINVAL;
826 return -1;
827 }
828}
829
830/**
831 *
832 * @param clock_id
833 * @param tp
834 * @return
835 */
836int posix_clock_settime(posix_clockid_t clock_id,
837 const struct posix_timespec *tp)
838{
839 assert(tp != NULL);
840
841 switch (clock_id) {
842 case CLOCK_REALTIME:
843 // TODO: setting clock
844 // FIXME: HelenOS doesn't actually support hardware
845 // clock yet
846 errno = EPERM;
847 return -1;
848 default:
849 errno = EINVAL;
850 return -1;
851 }
852}
853
854/**
855 *
856 * @param clock_id
857 * @param flags
858 * @param rqtp
859 * @param rmtp
860 * @return
861 */
862int posix_clock_nanosleep(posix_clockid_t clock_id, int flags,
863 const struct posix_timespec *rqtp, struct posix_timespec *rmtp)
864{
865 assert(rqtp != NULL);
866 assert(rmtp != NULL);
867
868 switch (clock_id) {
869 case CLOCK_REALTIME:
870 // TODO: interruptible sleep
871 if (rqtp->tv_sec != 0) {
872 sleep(rqtp->tv_sec);
873 }
874 if (rqtp->tv_nsec != 0) {
875 usleep(rqtp->tv_nsec / 1000);
876 }
877 return 0;
878 default:
879 errno = EINVAL;
880 return -1;
881 }
882}
883
884#if 0
885
886struct __posix_timer {
887 posix_clockid_t clockid;
888 struct posix_sigevent evp;
889};
890
891/**
892 *
893 * @param clockid
894 * @param evp
895 * @param timerid
896 * @return
897 */
898int posix_timer_create(posix_clockid_t clockid,
899 struct posix_sigevent *restrict evp,
900 posix_timer_t *restrict timerid)
901{
902 // TODO
903 not_implemented();
904}
905
906/**
907 *
908 * @param timerid
909 * @return
910 */
911int posix_timer_delete(posix_timer_t timerid)
912{
913 // TODO
914 not_implemented();
915}
916
917/**
918 *
919 * @param timerid
920 * @return
921 */
922int posix_timer_getoverrun(posix_timer_t timerid)
923{
924 // TODO
925 not_implemented();
926}
927
928/**
929 *
930 * @param timerid
931 * @param value
932 * @return
933 */
934int posix_timer_gettime(posix_timer_t timerid,
935 struct posix_itimerspec *value)
936{
937 // TODO
938 not_implemented();
939}
940
941/**
942 *
943 * @param timerid
944 * @param flags
945 * @param value
946 * @param ovalue
947 * @return
948 */
949int posix_timer_settime(posix_timer_t timerid, int flags,
950 const struct posix_itimerspec *restrict value,
951 struct posix_itimerspec *restrict ovalue)
952{
953 // TODO
954 not_implemented();
955}
956
957#endif
958
959/**
960 * Get CPU time used since the process invocation.
961 *
962 * @return Consumed CPU cycles by this process or -1 if not available.
963 */
964posix_clock_t posix_clock(void)
965{
966 posix_clock_t total_cycles = -1;
967 stats_task_t *task_stats = stats_get_task(task_get_id());
968 if (task_stats) {
969 total_cycles = (posix_clock_t) (task_stats->kcycles + task_stats->ucycles);
970 }
971 free(task_stats);
972 task_stats = 0;
973
974 return total_cycles;
975}
976
977/** @}
978 */
Note: See TracBrowser for help on using the repository browser.