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