source: mainline/uspace/lib/c/generic/time.c@ 58898d1d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 58898d1d was f9b2cb4c, checked in by Martin Decky <martin@…>, 10 years ago

unify interface API

  • introduce new interfaces
  • unify location service clients to always expect service ID as the second argument
  • remove obsolete methods that take explicit exchange management arguments (first phase)
  • use interfaces in device drivers, devman, location service, logger, inet
  • Property mode set to 100644
File size: 24.6 KB
RevLine 
[0b99e40]1/*
[df4ed85]2 * Copyright (c) 2006 Ondrej Palkovsky
[c2b0e10]3 * Copyright (c) 2011 Petr Koupy
4 * Copyright (c) 2011 Jiri Zarevucky
[0b99e40]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.
[b2951e2]29 */
30
[a46da63]31/** @addtogroup libc
[b2951e2]32 * @{
33 */
34/** @file
[22e6802]35 */
[0b99e40]36
[f25b73d6]37#include <sys/time.h>
[6119f24]38#include <time.h>
[3e6a98c5]39#include <stdbool.h>
[c0699467]40#include <libarch/barrier.h>
[2c577e0b]41#include <macros.h>
[6119f24]42#include <errno.h>
43#include <sysinfo.h>
44#include <as.h>
45#include <ddi.h>
[d9ece1cb]46#include <libc.h>
[c2b0e10]47#include <stdint.h>
48#include <stdio.h>
49#include <ctype.h>
[f7e69f5]50#include <assert.h>
[d4d74dc]51#include <unistd.h>
[3a58347]52#include <loc.h>
53#include <device/clock_dev.h>
54#include <malloc.h>
[c61d34b]55
[1ab8539]56#define ASCTIME_BUF_LEN 26
57
58#define HOURS_PER_DAY 24
59#define MINS_PER_HOUR 60
60#define SECS_PER_MIN 60
[7f9d97f3]61#define USECS_PER_SEC 1000000
[1ab8539]62#define MINS_PER_DAY (MINS_PER_HOUR * HOURS_PER_DAY)
63#define SECS_PER_HOUR (SECS_PER_MIN * MINS_PER_HOUR)
64#define SECS_PER_DAY (SECS_PER_HOUR * HOURS_PER_DAY)
[8219eb9]65
[2c577e0b]66/** Pointer to kernel shared variables with time */
[0b99e40]67struct {
[2d1fde3b]68 volatile sysarg_t seconds1;
[0b99e40]69 volatile sysarg_t useconds;
[2d1fde3b]70 volatile sysarg_t seconds2;
[0b99e40]71} *ktime = NULL;
72
[1ab8539]73static async_sess_t *clock_conn = NULL;
[c2b0e10]74
[1ab8539]75/** Check whether the year is a leap year.
[c2b0e10]76 *
77 * @param year Year since 1900 (e.g. for 1970, the value is 70).
[1ab8539]78 *
[c2b0e10]79 * @return true if year is a leap year, false otherwise
[1ab8539]80 *
[c2b0e10]81 */
[1ab8539]82static bool is_leap_year(time_t year)
[c2b0e10]83{
84 year += 1900;
[1ab8539]85
[c2b0e10]86 if (year % 400 == 0)
87 return true;
[1ab8539]88
[c2b0e10]89 if (year % 100 == 0)
90 return false;
[1ab8539]91
[c2b0e10]92 if (year % 4 == 0)
93 return true;
[1ab8539]94
[c2b0e10]95 return false;
96}
97
[1ab8539]98/** How many days there are in the given month
99 *
100 * Return how many days there are in the given month of the given year.
[c2b0e10]101 * Note that year is only taken into account if month is February.
102 *
103 * @param year Year since 1900 (can be negative).
[1ab8539]104 * @param mon Month of the year. 0 for January, 11 for December.
105 *
[c2b0e10]106 * @return Number of days in the specified month.
[1ab8539]107 *
[c2b0e10]108 */
[1ab8539]109static int days_in_month(time_t year, time_t mon)
[c2b0e10]110{
[1ab8539]111 assert(mon >= 0);
112 assert(mon <= 11);
113
114 static int month_days[] = {
115 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
116 };
117
[c2b0e10]118 if (mon == 1) {
[1ab8539]119 /* February */
[c2b0e10]120 year += 1900;
[1ab8539]121 return is_leap_year(year) ? 29 : 28;
[c2b0e10]122 }
[1ab8539]123
124 return month_days[mon];
[c2b0e10]125}
126
[1ab8539]127/** Which day of that year it is.
128 *
129 * For specified year, month and day of month, return which day of that year
[c2b0e10]130 * it is.
131 *
132 * For example, given date 2011-01-03, the corresponding expression is:
[1ab8539]133 * day_of_year(111, 0, 3) == 2
[c2b0e10]134 *
135 * @param year Year (year 1900 = 0, can be negative).
[1ab8539]136 * @param mon Month (January = 0).
[c2b0e10]137 * @param mday Day of month (First day is 1).
[1ab8539]138 *
[c2b0e10]139 * @return Day of year (First day is 0).
[1ab8539]140 *
[c2b0e10]141 */
[1ab8539]142static int day_of_year(time_t year, time_t mon, time_t mday)
[c2b0e10]143{
[1ab8539]144 static int mdays[] = {
145 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
146 };
147
148 static int leap_mdays[] = {
149 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335
150 };
151
152 return (is_leap_year(year) ? leap_mdays[mon] : mdays[mon]) + mday - 1;
[c2b0e10]153}
154
[1ab8539]155/** Integer division that rounds to negative infinity.
156 *
157 * Used by some functions in this module.
[c2b0e10]158 *
159 * @param op1 Dividend.
160 * @param op2 Divisor.
[1ab8539]161 *
[c2b0e10]162 * @return Rounded quotient.
[1ab8539]163 *
[c2b0e10]164 */
[1ab8539]165static time_t floor_div(time_t op1, time_t op2)
[c2b0e10]166{
[1ab8539]167 if ((op1 >= 0) || (op1 % op2 == 0))
[c2b0e10]168 return op1 / op2;
[1ab8539]169
170 return op1 / op2 - 1;
[c2b0e10]171}
172
[1ab8539]173/** Modulo that rounds to negative infinity.
174 *
175 * Used by some functions in this module.
[c2b0e10]176 *
177 * @param op1 Dividend.
178 * @param op2 Divisor.
[1ab8539]179 *
[c2b0e10]180 * @return Remainder.
[1ab8539]181 *
[c2b0e10]182 */
[1ab8539]183static time_t floor_mod(time_t op1, time_t op2)
[c2b0e10]184{
[1ab8539]185 time_t div = floor_div(op1, op2);
186
187 /*
188 * (a / b) * b + a % b == a
189 * Thus: a % b == a - (a / b) * b
190 */
191
192 time_t result = op1 - div * op2;
[c2b0e10]193
[1ab8539]194 /* Some paranoid checking to ensure there is mistake here. */
[c2b0e10]195 assert(result >= 0);
196 assert(result < op2);
197 assert(div * op2 + result == op1);
198
199 return result;
200}
201
[1ab8539]202/** Number of days since the Epoch.
203 *
[c2b0e10]204 * Epoch is 1970-01-01, which is also equal to day 0.
205 *
206 * @param year Year (year 1900 = 0, may be negative).
[1ab8539]207 * @param mon Month (January = 0).
[c2b0e10]208 * @param mday Day of month (first day = 1).
[1ab8539]209 *
[c2b0e10]210 * @return Number of days since the Epoch.
[1ab8539]211 *
[c2b0e10]212 */
[1ab8539]213static time_t days_since_epoch(time_t year, time_t mon, time_t mday)
[c2b0e10]214{
[1ab8539]215 return (year - 70) * 365 + floor_div(year - 69, 4) -
216 floor_div(year - 1, 100) + floor_div(year + 299, 400) +
217 day_of_year(year, mon, mday);
[c2b0e10]218}
219
[1ab8539]220/** Seconds since the Epoch.
221 *
222 * See also days_since_epoch().
223 *
[c2b0e10]224 * @param tm Normalized broken-down time.
[1ab8539]225 *
[c2b0e10]226 * @return Number of seconds since the epoch, not counting leap seconds.
[1ab8539]227 *
[c2b0e10]228 */
[1ab8539]229static time_t secs_since_epoch(const struct tm *tm)
[c2b0e10]230{
[1ab8539]231 return days_since_epoch(tm->tm_year, tm->tm_mon, tm->tm_mday) *
[c2b0e10]232 SECS_PER_DAY + tm->tm_hour * SECS_PER_HOUR +
233 tm->tm_min * SECS_PER_MIN + tm->tm_sec;
234}
235
[1ab8539]236/** Which day of week the specified date is.
237 *
[c2b0e10]238 * @param year Year (year 1900 = 0).
[1ab8539]239 * @param mon Month (January = 0).
[c2b0e10]240 * @param mday Day of month (first = 1).
[1ab8539]241 *
[c2b0e10]242 * @return Day of week (Sunday = 0).
[1ab8539]243 *
[c2b0e10]244 */
[1ab8539]245static time_t day_of_week(time_t year, time_t mon, time_t mday)
[c2b0e10]246{
247 /* 1970-01-01 is Thursday */
[1ab8539]248 return floor_mod(days_since_epoch(year, mon, mday) + 4, 7);
[c2b0e10]249}
250
[1ab8539]251/** Normalize the broken-down time.
252 *
253 * Optionally add specified amount of seconds.
254 *
[7f9d97f3]255 * @param tm Broken-down time to normalize.
256 * @param tv Timeval to add.
[1ab8539]257 *
[c2b0e10]258 * @return 0 on success, -1 on overflow
[1ab8539]259 *
[c2b0e10]260 */
[7f9d97f3]261static int normalize_tm_tv(struct tm *tm, const struct timeval *tv)
[c2b0e10]262{
263 // TODO: DST correction
[1ab8539]264
[c2b0e10]265 /* Set initial values. */
[7f9d97f3]266 time_t usec = tm->tm_usec + tv->tv_usec;
267 time_t sec = tm->tm_sec + tv->tv_sec;
[c2b0e10]268 time_t min = tm->tm_min;
269 time_t hour = tm->tm_hour;
270 time_t day = tm->tm_mday - 1;
271 time_t mon = tm->tm_mon;
272 time_t year = tm->tm_year;
[1ab8539]273
[c2b0e10]274 /* Adjust time. */
[7f9d97f3]275 sec += floor_div(usec, USECS_PER_SEC);
276 usec = floor_mod(usec, USECS_PER_SEC);
[1ab8539]277 min += floor_div(sec, SECS_PER_MIN);
278 sec = floor_mod(sec, SECS_PER_MIN);
279 hour += floor_div(min, MINS_PER_HOUR);
280 min = floor_mod(min, MINS_PER_HOUR);
281 day += floor_div(hour, HOURS_PER_DAY);
282 hour = floor_mod(hour, HOURS_PER_DAY);
283
[c2b0e10]284 /* Adjust month. */
[1ab8539]285 year += floor_div(mon, 12);
286 mon = floor_mod(mon, 12);
287
[c2b0e10]288 /* Now the difficult part - days of month. */
289
290 /* First, deal with whole cycles of 400 years = 146097 days. */
[1ab8539]291 year += floor_div(day, 146097) * 400;
292 day = floor_mod(day, 146097);
[c2b0e10]293
294 /* Then, go in one year steps. */
295 if (mon <= 1) {
296 /* January and February. */
297 while (day > 365) {
[1ab8539]298 day -= is_leap_year(year) ? 366 : 365;
[c2b0e10]299 year++;
300 }
301 } else {
302 /* Rest of the year. */
303 while (day > 365) {
[1ab8539]304 day -= is_leap_year(year + 1) ? 366 : 365;
[c2b0e10]305 year++;
306 }
307 }
308
309 /* Finally, finish it off month per month. */
[1ab8539]310 while (day >= days_in_month(year, mon)) {
311 day -= days_in_month(year, mon);
[c2b0e10]312 mon++;
[1ab8539]313
[c2b0e10]314 if (mon >= 12) {
315 mon -= 12;
316 year++;
317 }
318 }
319
320 /* Calculate the remaining two fields. */
[1ab8539]321 tm->tm_yday = day_of_year(year, mon, day + 1);
322 tm->tm_wday = day_of_week(year, mon, day + 1);
[c2b0e10]323
324 /* And put the values back to the struct. */
[7f9d97f3]325 tm->tm_usec = (int) usec;
[c2b0e10]326 tm->tm_sec = (int) sec;
327 tm->tm_min = (int) min;
328 tm->tm_hour = (int) hour;
329 tm->tm_mday = (int) day + 1;
330 tm->tm_mon = (int) mon;
331
[1ab8539]332 /* Casts to work around POSIX brain-damage. */
333 if (year > ((int) INT_MAX) || year < ((int) INT_MIN)) {
334 tm->tm_year = (year < 0) ? ((int) INT_MIN) : ((int) INT_MAX);
[c2b0e10]335 return -1;
336 }
337
338 tm->tm_year = (int) year;
339 return 0;
340}
341
[7f9d97f3]342static int normalize_tm_time(struct tm *tm, time_t time)
343{
344 struct timeval tv = {
345 .tv_sec = time,
346 .tv_usec = 0
347 };
348
349 return normalize_tm_tv(tm, &tv);
350}
351
352
[1ab8539]353/** Which day the week-based year starts on.
354 *
355 * Relative to the first calendar day. E.g. if the year starts
356 * on December 31st, the return value is -1.
[c2b0e10]357 *
358 * @param Year since 1900.
[1ab8539]359 *
[c2b0e10]360 * @return Offset of week-based year relative to calendar year.
[1ab8539]361 *
[c2b0e10]362 */
[1ab8539]363static int wbyear_offset(int year)
[c2b0e10]364{
[1ab8539]365 int start_wday = day_of_week(year, 0, 1);
366
367 return floor_mod(4 - start_wday, 7) - 3;
[c2b0e10]368}
369
[1ab8539]370/** Week-based year of the specified time.
[c2b0e10]371 *
372 * @param tm Normalized broken-down time.
[1ab8539]373 *
[c2b0e10]374 * @return Week-based year.
[1ab8539]375 *
[c2b0e10]376 */
[1ab8539]377static int wbyear(const struct tm *tm)
[c2b0e10]378{
[1ab8539]379 int day = tm->tm_yday - wbyear_offset(tm->tm_year);
380
[c2b0e10]381 if (day < 0) {
382 /* Last week of previous year. */
383 return tm->tm_year - 1;
384 }
[1ab8539]385
386 if (day > 364 + is_leap_year(tm->tm_year)) {
[c2b0e10]387 /* First week of next year. */
388 return tm->tm_year + 1;
389 }
[1ab8539]390
[c2b0e10]391 /* All the other days are in the calendar year. */
392 return tm->tm_year;
393}
394
[1ab8539]395/** Week number of the year (assuming weeks start on Sunday).
396 *
[c2b0e10]397 * The first Sunday of January is the first day of week 1;
398 * days in the new year before this are in week 0.
399 *
400 * @param tm Normalized broken-down time.
[1ab8539]401 *
[c2b0e10]402 * @return The week number (0 - 53).
[1ab8539]403 *
[c2b0e10]404 */
[1ab8539]405static int sun_week_number(const struct tm *tm)
[c2b0e10]406{
[1ab8539]407 int first_day = (7 - day_of_week(tm->tm_year, 0, 1)) % 7;
408
[c2b0e10]409 return (tm->tm_yday - first_day + 7) / 7;
410}
411
[1ab8539]412/** Week number of the year (assuming weeks start on Monday).
413 *
414 * If the week containing January 1st has four or more days
415 * in the new year, then it is considered week 1. Otherwise,
416 * it is the last week of the previous year, and the next week
417 * is week 1. Both January 4th and the first Thursday
[c2b0e10]418 * of January are always in week 1.
419 *
420 * @param tm Normalized broken-down time.
[1ab8539]421 *
[c2b0e10]422 * @return The week number (1 - 53).
[1ab8539]423 *
[c2b0e10]424 */
[1ab8539]425static int iso_week_number(const struct tm *tm)
[c2b0e10]426{
[1ab8539]427 int day = tm->tm_yday - wbyear_offset(tm->tm_year);
428
[c2b0e10]429 if (day < 0) {
430 /* Last week of previous year. */
431 return 53;
432 }
[1ab8539]433
434 if (day > 364 + is_leap_year(tm->tm_year)) {
[c2b0e10]435 /* First week of next year. */
436 return 1;
437 }
[1ab8539]438
[c2b0e10]439 /* All the other days give correct answer. */
440 return (day / 7 + 1);
441}
442
[1ab8539]443/** Week number of the year (assuming weeks start on Monday).
444 *
[c2b0e10]445 * The first Monday of January is the first day of week 1;
[1ab8539]446 * days in the new year before this are in week 0.
[c2b0e10]447 *
448 * @param tm Normalized broken-down time.
[1ab8539]449 *
[c2b0e10]450 * @return The week number (0 - 53).
[1ab8539]451 *
[c2b0e10]452 */
[1ab8539]453static int mon_week_number(const struct tm *tm)
[c2b0e10]454{
[1ab8539]455 int first_day = (1 - day_of_week(tm->tm_year, 0, 1)) % 7;
456
[c2b0e10]457 return (tm->tm_yday - first_day + 7) / 7;
458}
459
[7f9d97f3]460static void tv_normalize(struct timeval *tv)
461{
462 while (tv->tv_usec > USECS_PER_SEC) {
463 tv->tv_sec++;
464 tv->tv_usec -= USECS_PER_SEC;
465 }
466 while (tv->tv_usec < 0) {
467 tv->tv_sec--;
468 tv->tv_usec += USECS_PER_SEC;
469 }
470}
471
[daa90e8]472/** Add microseconds to given timeval.
473 *
[2c577e0b]474 * @param tv Destination timeval.
475 * @param usecs Number of microseconds to add.
476 *
[daa90e8]477 */
[7f9d97f3]478void tv_add_diff(struct timeval *tv, suseconds_t usecs)
[daa90e8]479{
[7f9d97f3]480 tv->tv_sec += usecs / USECS_PER_SEC;
481 tv->tv_usec += usecs % USECS_PER_SEC;
482 tv_normalize(tv);
483}
484
485/** Add two timevals.
486 *
487 * @param tv1 First timeval.
488 * @param tv2 Second timeval.
489 */
490void tv_add(struct timeval *tv1, struct timeval *tv2)
491{
492 tv1->tv_sec += tv2->tv_sec;
493 tv1->tv_usec += tv2->tv_usec;
494 tv_normalize(tv1);
[daa90e8]495}
496
497/** Subtract two timevals.
498 *
[2c577e0b]499 * @param tv1 First timeval.
500 * @param tv2 Second timeval.
501 *
502 * @return Difference between tv1 and tv2 (tv1 - tv2) in
503 * microseconds.
[daa90e8]504 *
505 */
[7f9d97f3]506suseconds_t tv_sub_diff(struct timeval *tv1, struct timeval *tv2)
[daa90e8]507{
[2c577e0b]508 return (tv1->tv_usec - tv2->tv_usec) +
[7f9d97f3]509 ((tv1->tv_sec - tv2->tv_sec) * USECS_PER_SEC);
510}
511
512/** Subtract two timevals.
513 *
514 * @param tv1 First timeval.
515 * @param tv2 Second timeval.
516 *
517 */
518void tv_sub(struct timeval *tv1, struct timeval *tv2)
519{
520 tv1->tv_sec -= tv2->tv_sec;
521 tv1->tv_usec -= tv2->tv_usec;
522 tv_normalize(tv1);
[daa90e8]523}
524
525/** Decide if one timeval is greater than the other.
526 *
[2c577e0b]527 * @param t1 First timeval.
528 * @param t2 Second timeval.
529 *
530 * @return True if tv1 is greater than tv2.
531 * @return False otherwise.
[daa90e8]532 *
533 */
534int tv_gt(struct timeval *tv1, struct timeval *tv2)
535{
536 if (tv1->tv_sec > tv2->tv_sec)
[2c577e0b]537 return true;
538
539 if ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec > tv2->tv_usec))
540 return true;
541
542 return false;
[daa90e8]543}
544
545/** Decide if one timeval is greater than or equal to the other.
546 *
[2c577e0b]547 * @param tv1 First timeval.
548 * @param tv2 Second timeval.
549 *
550 * @return True if tv1 is greater than or equal to tv2.
551 * @return False otherwise.
[daa90e8]552 *
553 */
554int tv_gteq(struct timeval *tv1, struct timeval *tv2)
555{
556 if (tv1->tv_sec > tv2->tv_sec)
[2c577e0b]557 return true;
558
559 if ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec >= tv2->tv_usec))
560 return true;
561
562 return false;
[daa90e8]563}
564
[1ab8539]565/** Get time of day.
[2c577e0b]566 *
567 * The time variables are memory mapped (read-only) from kernel which
568 * updates them periodically.
569 *
570 * As it is impossible to read 2 values atomically, we use a trick:
571 * First we read the seconds, then we read the microseconds, then we
572 * read the seconds again. If a second elapsed in the meantime, set
573 * the microseconds to zero.
574 *
575 * This assures that the values returned by two subsequent calls
576 * to gettimeofday() are monotonous.
577 *
[0b99e40]578 */
[1ab8539]579void gettimeofday(struct timeval *tv, struct timezone *tz)
[3a58347]580{
581 if (tz) {
582 tz->tz_minuteswest = 0;
583 tz->tz_dsttime = DST_NONE;
584 }
[1ab8539]585
[3a58347]586 if (clock_conn == NULL) {
[1ab8539]587 category_id_t cat_id;
588 int rc = loc_category_get_id("clock", &cat_id, IPC_FLAG_BLOCKING);
[3a58347]589 if (rc != EOK)
[1ab8539]590 goto fallback;
591
592 service_id_t *svc_ids;
593 size_t svc_cnt;
[3a58347]594 rc = loc_category_get_svcs(cat_id, &svc_ids, &svc_cnt);
595 if (rc != EOK)
[1ab8539]596 goto fallback;
597
[3a58347]598 if (svc_cnt == 0)
[1ab8539]599 goto fallback;
600
601 char *svc_name;
[3a58347]602 rc = loc_service_get_name(svc_ids[0], &svc_name);
[1ab8539]603 free(svc_ids);
[3a58347]604 if (rc != EOK)
[1ab8539]605 goto fallback;
606
607 service_id_t svc_id;
[3a58347]608 rc = loc_service_get_id(svc_name, &svc_id, 0);
[1ab8539]609 free(svc_name);
[3a58347]610 if (rc != EOK)
[1ab8539]611 goto fallback;
612
[f9b2cb4c]613 clock_conn = loc_service_connect(svc_id, INTERFACE_DDF,
614 IPC_FLAG_BLOCKING);
[3a58347]615 if (!clock_conn)
[1ab8539]616 goto fallback;
[3a58347]617 }
[1ab8539]618
619 struct tm time;
620 int rc = clock_dev_time_get(clock_conn, &time);
[3a58347]621 if (rc != EOK)
[1ab8539]622 goto fallback;
623
[7f9d97f3]624 tv->tv_usec = time.tm_usec;
[1ab8539]625 tv->tv_sec = mktime(&time);
626
627 return;
628
629fallback:
630 getuptime(tv);
[3a58347]631}
632
[1ab8539]633void getuptime(struct timeval *tv)
[0b99e40]634{
[6119f24]635 if (ktime == NULL) {
636 uintptr_t faddr;
637 int rc = sysinfo_get_value("clock.faddr", &faddr);
638 if (rc != EOK) {
639 errno = rc;
[1ab8539]640 goto fallback;
[6119f24]641 }
642
[bf9cb2f]643 void *addr = AS_AREA_ANY;
[8442d10]644 rc = physmem_map(faddr, 1, AS_AREA_READ | AS_AREA_CACHEABLE,
645 &addr);
[6119f24]646 if (rc != EOK) {
647 as_area_destroy(addr);
648 errno = rc;
[1ab8539]649 goto fallback;
[6119f24]650 }
651
652 ktime = addr;
[0b99e40]653 }
[2c577e0b]654
655 sysarg_t s2 = ktime->seconds2;
656
[5bd03eb]657 read_barrier();
[0b99e40]658 tv->tv_usec = ktime->useconds;
[2c577e0b]659
[0b99e40]660 read_barrier();
[2c577e0b]661 sysarg_t s1 = ktime->seconds1;
662
[2d1fde3b]663 if (s1 != s2) {
[2c577e0b]664 tv->tv_sec = max(s1, s2);
[2d1fde3b]665 tv->tv_usec = 0;
666 } else
667 tv->tv_sec = s1;
[1ab8539]668
669 return;
670
671fallback:
672 tv->tv_sec = 0;
673 tv->tv_usec = 0;
[0b99e40]674}
[44c6d88d]675
[813a703]676time_t time(time_t *tloc)
677{
678 struct timeval tv;
[1ab8539]679 gettimeofday(&tv, NULL);
[2c577e0b]680
[813a703]681 if (tloc)
682 *tloc = tv.tv_sec;
[2c577e0b]683
[813a703]684 return tv.tv_sec;
685}
686
[2c577e0b]687/** Wait unconditionally for specified number of microseconds
688 *
689 */
[22e6802]690int usleep(useconds_t usec)
[44c6d88d]691{
[22e6802]692 (void) __SYSCALL1(SYS_THREAD_USLEEP, usec);
[0c09f2b]693 return 0;
[44c6d88d]694}
[b2951e2]695
[5fd3f2d]696void udelay(useconds_t time)
697{
698 (void) __SYSCALL1(SYS_THREAD_UDELAY, (sysarg_t) time);
699}
700
701
[2c577e0b]702/** Wait unconditionally for specified number of seconds
703 *
704 */
[22e6802]705unsigned int sleep(unsigned int sec)
[dd655970]706{
[2c577e0b]707 /*
708 * Sleep in 1000 second steps to support
709 * full argument range
710 */
711
[22e6802]712 while (sec > 0) {
713 unsigned int period = (sec > 1000) ? 1000 : sec;
[2c577e0b]714
[d9ece1cb]715 usleep(period * 1000000);
[22e6802]716 sec -= period;
[dd655970]717 }
[2c577e0b]718
[0c09f2b]719 return 0;
[dd655970]720}
721
[1ab8539]722/** Get time from broken-down time.
723 *
724 * First normalize the provided broken-down time
725 * (moves all values to their proper bounds) and
726 * then try to calculate the appropriate time_t
727 * representation.
[c2b0e10]728 *
729 * @param tm Broken-down time.
[1ab8539]730 *
731 * @return time_t representation of the time.
732 * @return Undefined value on overflow.
733 *
[c2b0e10]734 */
735time_t mktime(struct tm *tm)
736{
737 // TODO: take DST flag into account
738 // TODO: detect overflow
[1ab8539]739
[7f9d97f3]740 normalize_tm_time(tm, 0);
[1ab8539]741 return secs_since_epoch(tm);
[c2b0e10]742}
743
[1ab8539]744/*
745 * FIXME: This requires POSIX-correct snprintf.
746 * Otherwise it won't work with non-ASCII chars.
747 */
748#define APPEND(...) \
749 { \
750 consumed = snprintf(ptr, remaining, __VA_ARGS__); \
751 if (consumed >= remaining) \
752 return 0; \
753 \
754 ptr += consumed; \
755 remaining -= consumed; \
756 }
757
758#define RECURSE(fmt) \
759 { \
760 consumed = strftime(ptr, remaining, fmt, tm); \
761 if (consumed == 0) \
762 return 0; \
763 \
764 ptr += consumed; \
765 remaining -= consumed; \
766 }
767
768#define TO_12H(hour) \
769 (((hour) > 12) ? ((hour) - 12) : \
770 (((hour) == 0) ? 12 : (hour)))
771
772/** Convert time and date to a string.
773 *
774 * @param s Buffer to write string to.
[c2b0e10]775 * @param maxsize Size of the buffer.
[1ab8539]776 * @param format Format of the output.
777 * @param tm Broken-down time to format.
778 *
[c2b0e10]779 * @return Number of bytes written.
[1ab8539]780 *
[c2b0e10]781 */
782size_t strftime(char *restrict s, size_t maxsize,
783 const char *restrict format, const struct tm *restrict tm)
784{
785 assert(s != NULL);
786 assert(format != NULL);
787 assert(tm != NULL);
[1ab8539]788
[c2b0e10]789 // TODO: use locale
[1ab8539]790
[c2b0e10]791 static const char *wday_abbr[] = {
792 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
793 };
[1ab8539]794
[c2b0e10]795 static const char *wday[] = {
796 "Sunday", "Monday", "Tuesday", "Wednesday",
797 "Thursday", "Friday", "Saturday"
798 };
[1ab8539]799
[c2b0e10]800 static const char *mon_abbr[] = {
801 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
802 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
803 };
[1ab8539]804
[c2b0e10]805 static const char *mon[] = {
806 "January", "February", "March", "April", "May", "June", "July",
807 "August", "September", "October", "November", "December"
808 };
809
[1ab8539]810 if (maxsize < 1)
[c2b0e10]811 return 0;
812
813 char *ptr = s;
814 size_t consumed;
815 size_t remaining = maxsize;
816
817 while (*format != '\0') {
818 if (*format != '%') {
[1ab8539]819 APPEND("%c", *format);
[c2b0e10]820 format++;
821 continue;
822 }
823
824 format++;
[1ab8539]825 if ((*format == '0') || (*format == '+')) {
[c2b0e10]826 // TODO: padding
827 format++;
828 }
[1ab8539]829
[c2b0e10]830 while (isdigit(*format)) {
831 // TODO: padding
832 format++;
833 }
[1ab8539]834
835 if ((*format == 'O') || (*format == 'E')) {
[c2b0e10]836 // TODO: locale's alternative format
837 format++;
838 }
839
840 switch (*format) {
841 case 'a':
[1ab8539]842 APPEND("%s", wday_abbr[tm->tm_wday]);
843 break;
[c2b0e10]844 case 'A':
[1ab8539]845 APPEND("%s", wday[tm->tm_wday]);
846 break;
[c2b0e10]847 case 'b':
[1ab8539]848 APPEND("%s", mon_abbr[tm->tm_mon]);
849 break;
[c2b0e10]850 case 'B':
[1ab8539]851 APPEND("%s", mon[tm->tm_mon]);
852 break;
[c2b0e10]853 case 'c':
854 // TODO: locale-specific datetime format
[1ab8539]855 RECURSE("%Y-%m-%d %H:%M:%S");
856 break;
[c2b0e10]857 case 'C':
[1ab8539]858 APPEND("%02d", (1900 + tm->tm_year) / 100);
859 break;
[c2b0e10]860 case 'd':
[1ab8539]861 APPEND("%02d", tm->tm_mday);
862 break;
[c2b0e10]863 case 'D':
[1ab8539]864 RECURSE("%m/%d/%y");
865 break;
[c2b0e10]866 case 'e':
[1ab8539]867 APPEND("%2d", tm->tm_mday);
868 break;
[c2b0e10]869 case 'F':
[1ab8539]870 RECURSE("%+4Y-%m-%d");
871 break;
[c2b0e10]872 case 'g':
[1ab8539]873 APPEND("%02d", wbyear(tm) % 100);
874 break;
[c2b0e10]875 case 'G':
[1ab8539]876 APPEND("%d", wbyear(tm));
877 break;
[c2b0e10]878 case 'h':
[1ab8539]879 RECURSE("%b");
880 break;
[c2b0e10]881 case 'H':
[1ab8539]882 APPEND("%02d", tm->tm_hour);
883 break;
[c2b0e10]884 case 'I':
[1ab8539]885 APPEND("%02d", TO_12H(tm->tm_hour));
886 break;
[c2b0e10]887 case 'j':
[1ab8539]888 APPEND("%03d", tm->tm_yday);
889 break;
[c2b0e10]890 case 'k':
[1ab8539]891 APPEND("%2d", tm->tm_hour);
892 break;
[c2b0e10]893 case 'l':
[1ab8539]894 APPEND("%2d", TO_12H(tm->tm_hour));
895 break;
[c2b0e10]896 case 'm':
[1ab8539]897 APPEND("%02d", tm->tm_mon);
898 break;
[c2b0e10]899 case 'M':
[1ab8539]900 APPEND("%02d", tm->tm_min);
901 break;
[c2b0e10]902 case 'n':
[1ab8539]903 APPEND("\n");
904 break;
[c2b0e10]905 case 'p':
[1ab8539]906 APPEND("%s", tm->tm_hour < 12 ? "AM" : "PM");
907 break;
[c2b0e10]908 case 'P':
[1ab8539]909 APPEND("%s", tm->tm_hour < 12 ? "am" : "PM");
910 break;
[c2b0e10]911 case 'r':
[1ab8539]912 RECURSE("%I:%M:%S %p");
913 break;
[c2b0e10]914 case 'R':
[1ab8539]915 RECURSE("%H:%M");
916 break;
[c2b0e10]917 case 's':
[1ab8539]918 APPEND("%ld", secs_since_epoch(tm));
919 break;
[c2b0e10]920 case 'S':
[1ab8539]921 APPEND("%02d", tm->tm_sec);
922 break;
[c2b0e10]923 case 't':
[1ab8539]924 APPEND("\t");
925 break;
[c2b0e10]926 case 'T':
[1ab8539]927 RECURSE("%H:%M:%S");
928 break;
[c2b0e10]929 case 'u':
[1ab8539]930 APPEND("%d", (tm->tm_wday == 0) ? 7 : tm->tm_wday);
[c2b0e10]931 break;
932 case 'U':
[1ab8539]933 APPEND("%02d", sun_week_number(tm));
934 break;
[c2b0e10]935 case 'V':
[1ab8539]936 APPEND("%02d", iso_week_number(tm));
937 break;
[c2b0e10]938 case 'w':
[1ab8539]939 APPEND("%d", tm->tm_wday);
940 break;
[c2b0e10]941 case 'W':
[1ab8539]942 APPEND("%02d", mon_week_number(tm));
943 break;
[c2b0e10]944 case 'x':
945 // TODO: locale-specific date format
[1ab8539]946 RECURSE("%Y-%m-%d");
947 break;
[c2b0e10]948 case 'X':
949 // TODO: locale-specific time format
[1ab8539]950 RECURSE("%H:%M:%S");
951 break;
[c2b0e10]952 case 'y':
[1ab8539]953 APPEND("%02d", tm->tm_year % 100);
954 break;
[c2b0e10]955 case 'Y':
[1ab8539]956 APPEND("%d", 1900 + tm->tm_year);
957 break;
[c2b0e10]958 case 'z':
959 // TODO: timezone
960 break;
961 case 'Z':
962 // TODO: timezone
963 break;
964 case '%':
[1ab8539]965 APPEND("%%");
[c2b0e10]966 break;
967 default:
968 /* Invalid specifier, print verbatim. */
[1ab8539]969 while (*format != '%')
[c2b0e10]970 format--;
[1ab8539]971
972 APPEND("%%");
[c2b0e10]973 break;
974 }
[1ab8539]975
[c2b0e10]976 format++;
977 }
978
979 return maxsize - remaining;
980}
981
[1ab8539]982/** Convert a time value to a broken-down UTC time/
[f7ea5400]983 *
[1ab8539]984 * @param time Time to convert
985 * @param result Structure to store the result to
986 *
987 * @return EOK or a negative error code
[f7ea5400]988 *
989 */
[664fc031]990int time_utc2tm(const time_t time, struct tm *restrict result)
[f7ea5400]991{
992 assert(result != NULL);
[1ab8539]993
[5b3394c]994 /* Set result to epoch. */
[7f9d97f3]995 result->tm_usec = 0;
[f7ea5400]996 result->tm_sec = 0;
997 result->tm_min = 0;
998 result->tm_hour = 0;
999 result->tm_mday = 1;
1000 result->tm_mon = 0;
1001 result->tm_year = 70; /* 1970 */
[1ab8539]1002
[7f9d97f3]1003 if (normalize_tm_time(result, time) == -1)
[f7ea5400]1004 return EOVERFLOW;
[1ab8539]1005
[f7ea5400]1006 return EOK;
[5b3394c]1007}
1008
[1ab8539]1009/** Convert a time value to a NULL-terminated string.
1010 *
1011 * The format is "Wed Jun 30 21:49:08 1993\n" expressed in UTC.
1012 *
1013 * @param time Time to convert.
1014 * @param buf Buffer to store the string to, must be at least
1015 * ASCTIME_BUF_LEN bytes long.
[f7ea5400]1016 *
[1ab8539]1017 * @return EOK or a negative error code.
[f7ea5400]1018 *
1019 */
[664fc031]1020int time_utc2str(const time_t time, char *restrict buf)
[f7ea5400]1021{
[1ab8539]1022 struct tm tm;
1023 int ret = time_utc2tm(time, &tm);
1024 if (ret != EOK)
1025 return ret;
1026
1027 time_tm2str(&tm, buf);
[f7ea5400]1028 return EOK;
1029}
1030
[1ab8539]1031/** Convert broken-down time to a NULL-terminated string.
1032 *
1033 * The format is "Sun Jan 1 00:00:00 1970\n". (Obsolete)
[8219eb9]1034 *
1035 * @param timeptr Broken-down time structure.
[1ab8539]1036 * @param buf Buffer to store string to, must be at least
1037 * ASCTIME_BUF_LEN bytes long.
1038 *
[8219eb9]1039 */
[664fc031]1040void time_tm2str(const struct tm *restrict timeptr, char *restrict buf)
[8219eb9]1041{
1042 assert(timeptr != NULL);
[f7ea5400]1043 assert(buf != NULL);
[1ab8539]1044
[8219eb9]1045 static const char *wday[] = {
1046 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
1047 };
[1ab8539]1048
[8219eb9]1049 static const char *mon[] = {
1050 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1051 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
1052 };
[1ab8539]1053
[8219eb9]1054 snprintf(buf, ASCTIME_BUF_LEN, "%s %s %2d %02d:%02d:%02d %d\n",
1055 wday[timeptr->tm_wday],
1056 mon[timeptr->tm_mon],
1057 timeptr->tm_mday, timeptr->tm_hour,
1058 timeptr->tm_min, timeptr->tm_sec,
1059 1900 + timeptr->tm_year);
1060}
1061
[1ab8539]1062/** Converts a time value to a broken-down local time.
1063 *
1064 * Time is expressed relative to the user's specified timezone.
[f7ea5400]1065 *
[7f9d97f3]1066 * @param tv Timeval to convert.
[1ab8539]1067 * @param result Structure to store the result to.
1068 *
1069 * @return EOK on success or a negative error code.
[f6cb995]1070 *
1071 */
[7f9d97f3]1072int time_tv2tm(const struct timeval *tv, struct tm *restrict result)
[f6cb995]1073{
[1ab8539]1074 // TODO: Deal with timezones.
1075 // Currently assumes system and all times are in UTC
1076
[f6cb995]1077 /* Set result to epoch. */
[7f9d97f3]1078 result->tm_usec = 0;
[f7ea5400]1079 result->tm_sec = 0;
1080 result->tm_min = 0;
1081 result->tm_hour = 0;
1082 result->tm_mday = 1;
1083 result->tm_mon = 0;
1084 result->tm_year = 70; /* 1970 */
[1ab8539]1085
[7f9d97f3]1086 if (normalize_tm_tv(result, tv) == -1)
[f7ea5400]1087 return EOVERFLOW;
[1ab8539]1088
[f7ea5400]1089 return EOK;
[f6cb995]1090}
[c2b0e10]1091
[7f9d97f3]1092/** Converts a time value to a broken-down local time.
1093 *
1094 * Time is expressed relative to the user's specified timezone.
1095 *
1096 * @param timer Time to convert.
1097 * @param result Structure to store the result to.
1098 *
1099 * @return EOK on success or a negative error code.
1100 *
1101 */
1102int time_local2tm(const time_t time, struct tm *restrict result)
1103{
1104 struct timeval tv = {
1105 .tv_sec = time,
1106 .tv_usec = 0
1107 };
1108
1109 return time_tv2tm(&tv, result);
1110}
1111
[1ab8539]1112/** Convert the calendar time to a NULL-terminated string.
1113 *
1114 * The format is "Wed Jun 30 21:49:08 1993\n" expressed relative to the
[f7ea5400]1115 * user's specified timezone.
1116 *
[1ab8539]1117 * @param timer Time to convert.
1118 * @param buf Buffer to store the string to. Must be at least
1119 * ASCTIME_BUF_LEN bytes long.
1120 *
1121 * @return EOK on success or a negative error code.
1122 *
[56b308e]1123 */
[664fc031]1124int time_local2str(const time_t time, char *buf)
[56b308e]1125{
[f7ea5400]1126 struct tm loctime;
[1ab8539]1127 int ret = time_local2tm(time, &loctime);
1128 if (ret != EOK)
1129 return ret;
1130
[664fc031]1131 time_tm2str(&loctime, buf);
[f7ea5400]1132 return EOK;
[56b308e]1133}
1134
[1ab8539]1135/** Calculate the difference between two times, in seconds.
1136 *
[d3e3a71]1137 * @param time1 First time.
1138 * @param time0 Second time.
[1ab8539]1139 *
1140 * @return Time difference in seconds.
1141 *
[d3e3a71]1142 */
1143double difftime(time_t time1, time_t time0)
1144{
1145 return (double) (time1 - time0);
1146}
1147
[a46da63]1148/** @}
[b2951e2]1149 */
Note: See TracBrowser for help on using the repository browser.