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