source: mainline/uspace/lib/c/generic/time.c@ 378d349

Last change on this file since 378d349 was 378d349, checked in by Jakub Jermar <jakub@…>, 7 years ago

Provide a dummy implementation of clock()

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