]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/time-util.c
Merge pull request #2915 from vinaykul/master
[thirdparty/systemd.git] / src / basic / time-util.c
CommitLineData
9a98c7a1
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
11c3a366
TA
20#include <errno.h>
21#include <limits.h>
22#include <stdlib.h>
9a98c7a1 23#include <string.h>
11c3a366
TA
24#include <sys/stat.h>
25#include <sys/time.h>
77ff2de9 26#include <sys/timerfd.h>
07630cea 27#include <sys/timex.h>
11c3a366
TA
28#include <sys/types.h>
29#include <unistd.h>
9a98c7a1 30
b5efdb8a 31#include "alloc-util.h"
3ffd4af2 32#include "fd-util.h"
0d39fa9c 33#include "fileio.h"
f4f15635 34#include "fs-util.h"
11c3a366
TA
35#include "log.h"
36#include "macro.h"
93cc7779
TA
37#include "parse-util.h"
38#include "path-util.h"
07630cea 39#include "string-util.h"
75683450 40#include "strv.h"
07630cea 41#include "time-util.h"
9a98c7a1 42
aaea9db8
AK
43static nsec_t timespec_load_nsec(const struct timespec *ts);
44
32c1f5a5
LP
45static clockid_t map_clock_id(clockid_t c) {
46
47 /* Some more exotic archs (s390, ppc, …) lack the "ALARM" flavour of the clocks. Thus, clock_gettime() will
48 * fail for them. Since they are essentially the same as their non-ALARM pendants (their only difference is
49 * when timers are set on them), let's just map them accordingly. This way, we can get the correct time even on
2abd5b5a
LR
50 * those archs.
51 *
52 * Also, older kernels don't support CLOCK_BOOTTIME: fall back to CLOCK_MONOTONIC. */
32c1f5a5
LP
53
54 switch (c) {
55
2abd5b5a 56 case CLOCK_BOOTTIME:
32c1f5a5 57 case CLOCK_BOOTTIME_ALARM:
2abd5b5a 58 return clock_boottime_or_monotonic ();
32c1f5a5
LP
59
60 case CLOCK_REALTIME_ALARM:
61 return CLOCK_REALTIME;
62
63 default:
64 return c;
65 }
66}
67
9a98c7a1
LP
68usec_t now(clockid_t clock_id) {
69 struct timespec ts;
70
32c1f5a5 71 assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
9a98c7a1
LP
72
73 return timespec_load(&ts);
74}
75
45d7a8bb
LP
76nsec_t now_nsec(clockid_t clock_id) {
77 struct timespec ts;
78
32c1f5a5 79 assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
45d7a8bb
LP
80
81 return timespec_load_nsec(&ts);
82}
83
9a98c7a1
LP
84dual_timestamp* dual_timestamp_get(dual_timestamp *ts) {
85 assert(ts);
86
87 ts->realtime = now(CLOCK_REALTIME);
88 ts->monotonic = now(CLOCK_MONOTONIC);
89
90 return ts;
91}
92
93dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) {
94 int64_t delta;
95 assert(ts);
96
75a5f1d8
LP
97 if (u == USEC_INFINITY || u <= 0) {
98 ts->realtime = ts->monotonic = u;
cae0c5e0
LP
99 return ts;
100 }
101
9a98c7a1
LP
102 ts->realtime = u;
103
75a5f1d8 104 delta = (int64_t) now(CLOCK_REALTIME) - (int64_t) u;
5d634ca8 105 ts->monotonic = usec_sub(now(CLOCK_MONOTONIC), delta);
9a98c7a1
LP
106
107 return ts;
108}
109
cae0c5e0
LP
110dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) {
111 int64_t delta;
112 assert(ts);
113
3a43da28
KS
114 if (u == USEC_INFINITY) {
115 ts->realtime = ts->monotonic = USEC_INFINITY;
cae0c5e0
LP
116 return ts;
117 }
118
119 ts->monotonic = u;
120 delta = (int64_t) now(CLOCK_MONOTONIC) - (int64_t) u;
5d634ca8 121 ts->realtime = usec_sub(now(CLOCK_REALTIME), delta);
cae0c5e0
LP
122
123 return ts;
124}
125
fbe55073
LP
126dual_timestamp* dual_timestamp_from_boottime_or_monotonic(dual_timestamp *ts, usec_t u) {
127 int64_t delta;
128
129 if (u == USEC_INFINITY) {
130 ts->realtime = ts->monotonic = USEC_INFINITY;
131 return ts;
132 }
fbe55073 133
0345d252 134 dual_timestamp_get(ts);
fbe55073 135 delta = (int64_t) now(clock_boottime_or_monotonic()) - (int64_t) u;
5d634ca8
AK
136 ts->realtime = usec_sub(ts->realtime, delta);
137 ts->monotonic = usec_sub(ts->monotonic, delta);
fbe55073
LP
138
139 return ts;
140}
141
9a98c7a1
LP
142usec_t timespec_load(const struct timespec *ts) {
143 assert(ts);
144
a2daa2f0 145 if (ts->tv_sec == (time_t) -1 && ts->tv_nsec == (long) -1)
3a43da28 146 return USEC_INFINITY;
9a98c7a1
LP
147
148 if ((usec_t) ts->tv_sec > (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC)
3a43da28 149 return USEC_INFINITY;
9a98c7a1
LP
150
151 return
152 (usec_t) ts->tv_sec * USEC_PER_SEC +
153 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
154}
155
aaea9db8 156static nsec_t timespec_load_nsec(const struct timespec *ts) {
45d7a8bb
LP
157 assert(ts);
158
a2daa2f0 159 if (ts->tv_sec == (time_t) -1 && ts->tv_nsec == (long) -1)
45d7a8bb
LP
160 return NSEC_INFINITY;
161
a2daa2f0
ZJS
162 if ((nsec_t) ts->tv_sec >= (UINT64_MAX - ts->tv_nsec) / NSEC_PER_SEC)
163 return NSEC_INFINITY;
164
165 return (nsec_t) ts->tv_sec * NSEC_PER_SEC + (nsec_t) ts->tv_nsec;
45d7a8bb
LP
166}
167
9a98c7a1
LP
168struct timespec *timespec_store(struct timespec *ts, usec_t u) {
169 assert(ts);
170
3a43da28 171 if (u == USEC_INFINITY) {
9a98c7a1
LP
172 ts->tv_sec = (time_t) -1;
173 ts->tv_nsec = (long) -1;
174 return ts;
175 }
176
177 ts->tv_sec = (time_t) (u / USEC_PER_SEC);
178 ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
179
180 return ts;
181}
182
183usec_t timeval_load(const struct timeval *tv) {
184 assert(tv);
185
186 if (tv->tv_sec == (time_t) -1 &&
187 tv->tv_usec == (suseconds_t) -1)
3a43da28 188 return USEC_INFINITY;
9a98c7a1
LP
189
190 if ((usec_t) tv->tv_sec > (UINT64_MAX - tv->tv_usec) / USEC_PER_SEC)
3a43da28 191 return USEC_INFINITY;
9a98c7a1
LP
192
193 return
194 (usec_t) tv->tv_sec * USEC_PER_SEC +
195 (usec_t) tv->tv_usec;
196}
197
198struct timeval *timeval_store(struct timeval *tv, usec_t u) {
199 assert(tv);
200
3a43da28 201 if (u == USEC_INFINITY) {
9a98c7a1
LP
202 tv->tv_sec = (time_t) -1;
203 tv->tv_usec = (suseconds_t) -1;
4d89874a
ZJS
204 } else {
205 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
206 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
9a98c7a1
LP
207 }
208
9a98c7a1
LP
209 return tv;
210}
211
0056086a
AK
212static char *format_timestamp_internal(char *buf, size_t l, usec_t t,
213 bool utc, bool us) {
9a98c7a1
LP
214 struct tm tm;
215 time_t sec;
0056086a 216 int k;
9a98c7a1
LP
217
218 assert(buf);
219 assert(l > 0);
220
3a43da28 221 if (t <= 0 || t == USEC_INFINITY)
9a98c7a1
LP
222 return NULL;
223
224 sec = (time_t) (t / USEC_PER_SEC);
7c67c79c 225 localtime_or_gmtime_r(&sec, &tm, utc);
9a98c7a1 226
0056086a
AK
227 if (us)
228 k = strftime(buf, l, "%a %Y-%m-%d %H:%M:%S", &tm);
229 else
230 k = strftime(buf, l, "%a %Y-%m-%d %H:%M:%S %Z", &tm);
231
232 if (k <= 0)
9a98c7a1 233 return NULL;
0056086a
AK
234 if (us) {
235 snprintf(buf + strlen(buf), l - strlen(buf), ".%06llu", (unsigned long long) (t % USEC_PER_SEC));
236 if (strftime(buf + strlen(buf), l - strlen(buf), " %Z", &tm) <= 0)
237 return NULL;
238 }
9a98c7a1
LP
239
240 return buf;
241}
242
a62e83b4 243char *format_timestamp(char *buf, size_t l, usec_t t) {
0056086a 244 return format_timestamp_internal(buf, l, t, false, false);
a62e83b4
JS
245}
246
5ab99e07 247char *format_timestamp_utc(char *buf, size_t l, usec_t t) {
0056086a 248 return format_timestamp_internal(buf, l, t, true, false);
f02d8367
ZJS
249}
250
5ab99e07 251char *format_timestamp_us(char *buf, size_t l, usec_t t) {
0056086a 252 return format_timestamp_internal(buf, l, t, false, true);
5ab99e07
LP
253}
254
255char *format_timestamp_us_utc(char *buf, size_t l, usec_t t) {
0056086a 256 return format_timestamp_internal(buf, l, t, true, true);
5ab99e07
LP
257}
258
bbb8486e 259char *format_timestamp_relative(char *buf, size_t l, usec_t t) {
1fcf71f5 260 const char *s;
9a98c7a1
LP
261 usec_t n, d;
262
65de0395 263 if (t <= 0 || t == USEC_INFINITY)
9a98c7a1
LP
264 return NULL;
265
65de0395 266 n = now(CLOCK_REALTIME);
1fcf71f5
LP
267 if (n > t) {
268 d = n - t;
269 s = "ago";
270 } else {
271 d = t - n;
272 s = "left";
273 }
9a98c7a1
LP
274
275 if (d >= USEC_PER_YEAR)
609e002e 276 snprintf(buf, l, USEC_FMT " years " USEC_FMT " months %s",
de0671ee
ZJS
277 d / USEC_PER_YEAR,
278 (d % USEC_PER_YEAR) / USEC_PER_MONTH, s);
9a98c7a1 279 else if (d >= USEC_PER_MONTH)
609e002e 280 snprintf(buf, l, USEC_FMT " months " USEC_FMT " days %s",
de0671ee
ZJS
281 d / USEC_PER_MONTH,
282 (d % USEC_PER_MONTH) / USEC_PER_DAY, s);
9a98c7a1 283 else if (d >= USEC_PER_WEEK)
609e002e 284 snprintf(buf, l, USEC_FMT " weeks " USEC_FMT " days %s",
de0671ee
ZJS
285 d / USEC_PER_WEEK,
286 (d % USEC_PER_WEEK) / USEC_PER_DAY, s);
9a98c7a1 287 else if (d >= 2*USEC_PER_DAY)
609e002e 288 snprintf(buf, l, USEC_FMT " days %s", d / USEC_PER_DAY, s);
9a98c7a1 289 else if (d >= 25*USEC_PER_HOUR)
609e002e 290 snprintf(buf, l, "1 day " USEC_FMT "h %s",
de0671ee 291 (d - USEC_PER_DAY) / USEC_PER_HOUR, s);
9a98c7a1 292 else if (d >= 6*USEC_PER_HOUR)
609e002e 293 snprintf(buf, l, USEC_FMT "h %s",
de0671ee 294 d / USEC_PER_HOUR, s);
9a98c7a1 295 else if (d >= USEC_PER_HOUR)
609e002e 296 snprintf(buf, l, USEC_FMT "h " USEC_FMT "min %s",
de0671ee
ZJS
297 d / USEC_PER_HOUR,
298 (d % USEC_PER_HOUR) / USEC_PER_MINUTE, s);
9a98c7a1 299 else if (d >= 5*USEC_PER_MINUTE)
609e002e 300 snprintf(buf, l, USEC_FMT "min %s",
de0671ee 301 d / USEC_PER_MINUTE, s);
9a98c7a1 302 else if (d >= USEC_PER_MINUTE)
609e002e 303 snprintf(buf, l, USEC_FMT "min " USEC_FMT "s %s",
de0671ee
ZJS
304 d / USEC_PER_MINUTE,
305 (d % USEC_PER_MINUTE) / USEC_PER_SEC, s);
9a98c7a1 306 else if (d >= USEC_PER_SEC)
609e002e 307 snprintf(buf, l, USEC_FMT "s %s",
de0671ee 308 d / USEC_PER_SEC, s);
9a98c7a1 309 else if (d >= USEC_PER_MSEC)
609e002e 310 snprintf(buf, l, USEC_FMT "ms %s",
de0671ee 311 d / USEC_PER_MSEC, s);
9a98c7a1 312 else if (d > 0)
de0671ee
ZJS
313 snprintf(buf, l, USEC_FMT"us %s",
314 d, s);
9a98c7a1
LP
315 else
316 snprintf(buf, l, "now");
317
318 buf[l-1] = 0;
319 return buf;
320}
321
2fa4092c 322char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
9a98c7a1
LP
323 static const struct {
324 const char *suffix;
325 usec_t usec;
326 } table[] = {
eb55ec9f
LP
327 { "y", USEC_PER_YEAR },
328 { "month", USEC_PER_MONTH },
329 { "w", USEC_PER_WEEK },
330 { "d", USEC_PER_DAY },
331 { "h", USEC_PER_HOUR },
332 { "min", USEC_PER_MINUTE },
333 { "s", USEC_PER_SEC },
334 { "ms", USEC_PER_MSEC },
335 { "us", 1 },
9a98c7a1
LP
336 };
337
338 unsigned i;
339 char *p = buf;
2fa4092c 340 bool something = false;
9a98c7a1
LP
341
342 assert(buf);
343 assert(l > 0);
344
bb1fada8
LP
345 if (t == USEC_INFINITY) {
346 strncpy(p, "infinity", l-1);
347 p[l-1] = 0;
348 return p;
349 }
350
351 if (t <= 0) {
352 strncpy(p, "0", l-1);
7c537b2e
LP
353 p[l-1] = 0;
354 return p;
355 }
356
7f602784 357 /* The result of this function can be parsed with parse_sec */
9a98c7a1
LP
358
359 for (i = 0; i < ELEMENTSOF(table); i++) {
7759ecb2 360 int k = 0;
9a98c7a1 361 size_t n;
2fa4092c
LP
362 bool done = false;
363 usec_t a, b;
364
7c537b2e
LP
365 if (t <= 0)
366 break;
2fa4092c 367
7c537b2e 368 if (t < accuracy && something)
2fa4092c 369 break;
9a98c7a1
LP
370
371 if (t < table[i].usec)
372 continue;
373
374 if (l <= 1)
375 break;
376
2fa4092c
LP
377 a = t / table[i].usec;
378 b = t % table[i].usec;
379
380 /* Let's see if we should shows this in dot notation */
381 if (t < USEC_PER_MINUTE && b > 0) {
382 usec_t cc;
383 int j;
384
385 j = 0;
386 for (cc = table[i].usec; cc > 1; cc /= 10)
387 j++;
388
389 for (cc = accuracy; cc > 1; cc /= 10) {
390 b /= 10;
391 j--;
392 }
393
394 if (j > 0) {
395 k = snprintf(p, l,
de0671ee 396 "%s"USEC_FMT".%0*llu%s",
2fa4092c 397 p > buf ? " " : "",
de0671ee 398 a,
2fa4092c
LP
399 j,
400 (unsigned long long) b,
401 table[i].suffix);
402
403 t = 0;
404 done = true;
405 }
406 }
407
408 /* No? Then let's show it normally */
409 if (!done) {
410 k = snprintf(p, l,
de0671ee 411 "%s"USEC_FMT"%s",
2fa4092c 412 p > buf ? " " : "",
de0671ee 413 a,
2fa4092c
LP
414 table[i].suffix);
415
416 t = b;
417 }
418
9a98c7a1
LP
419 n = MIN((size_t) k, l);
420
421 l -= n;
422 p += n;
423
2fa4092c 424 something = true;
9a98c7a1
LP
425 }
426
427 *p = 0;
428
429 return buf;
430}
431
432void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t) {
433
434 assert(f);
435 assert(name);
436 assert(t);
437
438 if (!dual_timestamp_is_set(t))
439 return;
440
de0671ee 441 fprintf(f, "%s="USEC_FMT" "USEC_FMT"\n",
9a98c7a1 442 name,
de0671ee
ZJS
443 t->realtime,
444 t->monotonic);
9a98c7a1
LP
445}
446
e911de99 447int dual_timestamp_deserialize(const char *value, dual_timestamp *t) {
9a98c7a1
LP
448 unsigned long long a, b;
449
450 assert(value);
451 assert(t);
452
e911de99 453 if (sscanf(value, "%llu %llu", &a, &b) != 2) {
b895a735 454 log_debug("Failed to parse dual timestamp value \"%s\": %m", value);
e911de99 455 return -EINVAL;
9a98c7a1 456 }
e911de99
LP
457
458 t->realtime = a;
459 t->monotonic = b;
460
461 return 0;
9a98c7a1
LP
462}
463
b895a735 464int timestamp_deserialize(const char *value, usec_t *timestamp) {
ebf30a08
AK
465 int r;
466
467 assert(value);
468
469 r = safe_atou64(value, timestamp);
ebf30a08 470 if (r < 0)
b895a735 471 return log_debug_errno(r, "Failed to parse timestamp value \"%s\": %m", value);
ebf30a08
AK
472
473 return r;
474}
475
9a98c7a1 476int parse_timestamp(const char *t, usec_t *usec) {
92134489
LP
477 static const struct {
478 const char *name;
479 const int nr;
480 } day_nr[] = {
481 { "Sunday", 0 },
482 { "Sun", 0 },
483 { "Monday", 1 },
484 { "Mon", 1 },
485 { "Tuesday", 2 },
486 { "Tue", 2 },
487 { "Wednesday", 3 },
488 { "Wed", 3 },
489 { "Thursday", 4 },
490 { "Thu", 4 },
491 { "Friday", 5 },
492 { "Fri", 5 },
493 { "Saturday", 6 },
494 { "Sat", 6 },
495 };
496
9a98c7a1 497 const char *k;
078efddd 498 const char *utc;
9a98c7a1
LP
499 struct tm tm, copy;
500 time_t x;
e4eaf99a 501 usec_t x_usec, plus = 0, minus = 0, ret;
92134489
LP
502 int r, weekday = -1;
503 unsigned i;
9a98c7a1
LP
504
505 /*
506 * Allowed syntaxes:
507 *
508 * 2012-09-22 16:34:22
509 * 2012-09-22 16:34 (seconds will be set to 0)
510 * 2012-09-22 (time will be set to 00:00:00)
511 * 16:34:22 (date will be set to today)
512 * 16:34 (date will be set to today, seconds to 0)
513 * now
514 * yesterday (time is set to 00:00:00)
515 * today (time is set to 00:00:00)
516 * tomorrow (time is set to 00:00:00)
517 * +5min
518 * -5days
5ba6e094 519 * @2147483647 (seconds since epoch)
9a98c7a1
LP
520 *
521 */
522
523 assert(t);
524 assert(usec);
525
e4eaf99a
HV
526 if (t[0] == '@')
527 return parse_sec(t + 1, usec);
9a98c7a1 528
e4eaf99a 529 ret = now(CLOCK_REALTIME);
9a98c7a1 530
e4eaf99a 531 if (streq(t, "now"))
9a98c7a1
LP
532 goto finish;
533
e4eaf99a 534 else if (t[0] == '+') {
7f602784 535 r = parse_sec(t+1, &plus);
9a98c7a1
LP
536 if (r < 0)
537 return r;
538
539 goto finish;
9a98c7a1 540
5ba6e094 541 } else if (t[0] == '-') {
7f602784 542 r = parse_sec(t+1, &minus);
9a98c7a1
LP
543 if (r < 0)
544 return r;
545
546 goto finish;
decad910 547
078efddd
HV
548 } else if ((k = endswith(t, " ago"))) {
549 t = strndupa(t, k - t);
decad910 550
e4eaf99a 551 r = parse_sec(t, &minus);
decad910
LP
552 if (r < 0)
553 return r;
554
1fcf71f5 555 goto finish;
1fcf71f5 556
078efddd
HV
557 } else if ((k = endswith(t, " left"))) {
558 t = strndupa(t, k - t);
1fcf71f5 559
e4eaf99a 560 r = parse_sec(t, &plus);
1fcf71f5
LP
561 if (r < 0)
562 return r;
563
decad910 564 goto finish;
9a98c7a1
LP
565 }
566
e4eaf99a
HV
567 utc = endswith_no_case(t, " UTC");
568 if (utc)
078efddd 569 t = strndupa(t, utc - t);
e4eaf99a
HV
570
571 x = ret / USEC_PER_SEC;
572 x_usec = 0;
573
574 assert_se(localtime_or_gmtime_r(&x, &tm, utc));
575 tm.tm_isdst = -1;
576
577 if (streq(t, "today")) {
578 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
579 goto from_tm;
580
581 } else if (streq(t, "yesterday")) {
313cefa1 582 tm.tm_mday--;
e4eaf99a
HV
583 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
584 goto from_tm;
585
586 } else if (streq(t, "tomorrow")) {
313cefa1 587 tm.tm_mday++;
e4eaf99a
HV
588 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
589 goto from_tm;
590 }
591
592
92134489
LP
593 for (i = 0; i < ELEMENTSOF(day_nr); i++) {
594 size_t skip;
595
596 if (!startswith_no_case(t, day_nr[i].name))
597 continue;
598
599 skip = strlen(day_nr[i].name);
600 if (t[skip] != ' ')
601 continue;
602
603 weekday = day_nr[i].nr;
604 t += skip + 1;
605 break;
606 }
607
9a98c7a1
LP
608 copy = tm;
609 k = strptime(t, "%y-%m-%d %H:%M:%S", &tm);
e4eaf99a
HV
610 if (k) {
611 if (*k == '.')
612 goto parse_usec;
613 else if (*k == 0)
614 goto from_tm;
615 }
9a98c7a1
LP
616
617 tm = copy;
618 k = strptime(t, "%Y-%m-%d %H:%M:%S", &tm);
e4eaf99a
HV
619 if (k) {
620 if (*k == '.')
621 goto parse_usec;
622 else if (*k == 0)
623 goto from_tm;
624 }
9a98c7a1
LP
625
626 tm = copy;
627 k = strptime(t, "%y-%m-%d %H:%M", &tm);
628 if (k && *k == 0) {
629 tm.tm_sec = 0;
e4eaf99a 630 goto from_tm;
9a98c7a1
LP
631 }
632
633 tm = copy;
634 k = strptime(t, "%Y-%m-%d %H:%M", &tm);
635 if (k && *k == 0) {
636 tm.tm_sec = 0;
e4eaf99a 637 goto from_tm;
9a98c7a1
LP
638 }
639
640 tm = copy;
641 k = strptime(t, "%y-%m-%d", &tm);
642 if (k && *k == 0) {
643 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
e4eaf99a 644 goto from_tm;
9a98c7a1
LP
645 }
646
647 tm = copy;
648 k = strptime(t, "%Y-%m-%d", &tm);
649 if (k && *k == 0) {
650 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
e4eaf99a 651 goto from_tm;
9a98c7a1
LP
652 }
653
654 tm = copy;
655 k = strptime(t, "%H:%M:%S", &tm);
e4eaf99a
HV
656 if (k) {
657 if (*k == '.')
658 goto parse_usec;
659 else if (*k == 0)
660 goto from_tm;
661 }
9a98c7a1
LP
662
663 tm = copy;
664 k = strptime(t, "%H:%M", &tm);
665 if (k && *k == 0) {
666 tm.tm_sec = 0;
e4eaf99a 667 goto from_tm;
9a98c7a1
LP
668 }
669
670 return -EINVAL;
671
e4eaf99a
HV
672parse_usec:
673 {
436dd70f 674 unsigned add;
e4eaf99a
HV
675
676 k++;
436dd70f
HV
677 r = parse_fractional_part_u(&k, 6, &add);
678 if (r < 0)
e4eaf99a
HV
679 return -EINVAL;
680
436dd70f 681 if (*k)
e4eaf99a
HV
682 return -EINVAL;
683
436dd70f 684 x_usec = add;
e4eaf99a 685
e4eaf99a
HV
686 }
687
688from_tm:
689 x = mktime_or_timegm(&tm, utc);
9a98c7a1
LP
690 if (x == (time_t) -1)
691 return -EINVAL;
692
92134489
LP
693 if (weekday >= 0 && tm.tm_wday != weekday)
694 return -EINVAL;
695
e4eaf99a 696 ret = (usec_t) x * USEC_PER_SEC + x_usec;
9a98c7a1 697
e4eaf99a 698finish:
9a98c7a1
LP
699 ret += plus;
700 if (ret > minus)
701 ret -= minus;
702 else
703 ret = 0;
704
705 *usec = ret;
706
707 return 0;
708}
709
240a7ba9 710static char* extract_multiplier(char *p, usec_t *multiplier) {
9a98c7a1
LP
711 static const struct {
712 const char *suffix;
713 usec_t usec;
714 } table[] = {
eb55ec9f
LP
715 { "seconds", USEC_PER_SEC },
716 { "second", USEC_PER_SEC },
717 { "sec", USEC_PER_SEC },
718 { "s", USEC_PER_SEC },
9a98c7a1 719 { "minutes", USEC_PER_MINUTE },
eb55ec9f
LP
720 { "minute", USEC_PER_MINUTE },
721 { "min", USEC_PER_MINUTE },
722 { "months", USEC_PER_MONTH },
723 { "month", USEC_PER_MONTH },
724 { "M", USEC_PER_MONTH },
725 { "msec", USEC_PER_MSEC },
726 { "ms", USEC_PER_MSEC },
727 { "m", USEC_PER_MINUTE },
728 { "hours", USEC_PER_HOUR },
729 { "hour", USEC_PER_HOUR },
730 { "hr", USEC_PER_HOUR },
731 { "h", USEC_PER_HOUR },
732 { "days", USEC_PER_DAY },
733 { "day", USEC_PER_DAY },
734 { "d", USEC_PER_DAY },
735 { "weeks", USEC_PER_WEEK },
736 { "week", USEC_PER_WEEK },
737 { "w", USEC_PER_WEEK },
738 { "years", USEC_PER_YEAR },
739 { "year", USEC_PER_YEAR },
740 { "y", USEC_PER_YEAR },
741 { "usec", 1ULL },
742 { "us", 1ULL },
9a98c7a1 743 };
240a7ba9
ZJS
744 unsigned i;
745
746 for (i = 0; i < ELEMENTSOF(table); i++) {
747 char *e;
748
749 e = startswith(p, table[i].suffix);
750 if (e) {
751 *multiplier = table[i].usec;
752 return e;
753 }
754 }
9a98c7a1 755
240a7ba9
ZJS
756 return p;
757}
758
759int parse_time(const char *t, usec_t *usec, usec_t default_unit) {
b1d6dcf5 760 const char *p, *s;
9a98c7a1 761 usec_t r = 0;
cb0dac05 762 bool something = false;
9a98c7a1
LP
763
764 assert(t);
765 assert(usec);
519cffec 766 assert(default_unit > 0);
9a98c7a1
LP
767
768 p = t;
b1d6dcf5
ZJS
769
770 p += strspn(p, WHITESPACE);
771 s = startswith(p, "infinity");
772 if (s) {
773 s += strspn(s, WHITESPACE);
774 if (*s != 0)
775 return -EINVAL;
776
777 *usec = USEC_INFINITY;
778 return 0;
779 }
780
cb0dac05
LP
781 for (;;) {
782 long long l, z = 0;
9a98c7a1 783 char *e;
240a7ba9
ZJS
784 unsigned n = 0;
785 usec_t multiplier = default_unit, k;
cb0dac05
LP
786
787 p += strspn(p, WHITESPACE);
788
789 if (*p == 0) {
790 if (!something)
791 return -EINVAL;
792
793 break;
794 }
9a98c7a1
LP
795
796 errno = 0;
797 l = strtoll(p, &e, 10);
8333c77e 798 if (errno > 0)
9a98c7a1 799 return -errno;
9a98c7a1
LP
800 if (l < 0)
801 return -ERANGE;
802
cb0dac05
LP
803 if (*e == '.') {
804 char *b = e + 1;
805
806 errno = 0;
807 z = strtoll(b, &e, 10);
808 if (errno > 0)
809 return -errno;
810
811 if (z < 0)
812 return -ERANGE;
813
814 if (e == b)
815 return -EINVAL;
816
817 n = e - b;
818
819 } else if (e == p)
9a98c7a1
LP
820 return -EINVAL;
821
822 e += strspn(e, WHITESPACE);
240a7ba9 823 p = extract_multiplier(e, &multiplier);
9a98c7a1 824
519cffec
LP
825 something = true;
826
827 k = (usec_t) z * multiplier;
828
829 for (; n > 0; n--)
830 k /= 10;
831
832 r += (usec_t) l * multiplier + k;
cb0dac05 833 }
9a98c7a1
LP
834
835 *usec = r;
836
837 return 0;
838}
839
519cffec
LP
840int parse_sec(const char *t, usec_t *usec) {
841 return parse_time(t, usec, USEC_PER_SEC);
842}
843
9a98c7a1
LP
844int parse_nsec(const char *t, nsec_t *nsec) {
845 static const struct {
846 const char *suffix;
847 nsec_t nsec;
848 } table[] = {
849 { "seconds", NSEC_PER_SEC },
850 { "second", NSEC_PER_SEC },
851 { "sec", NSEC_PER_SEC },
852 { "s", NSEC_PER_SEC },
853 { "minutes", NSEC_PER_MINUTE },
854 { "minute", NSEC_PER_MINUTE },
855 { "min", NSEC_PER_MINUTE },
856 { "months", NSEC_PER_MONTH },
857 { "month", NSEC_PER_MONTH },
858 { "msec", NSEC_PER_MSEC },
859 { "ms", NSEC_PER_MSEC },
860 { "m", NSEC_PER_MINUTE },
861 { "hours", NSEC_PER_HOUR },
862 { "hour", NSEC_PER_HOUR },
863 { "hr", NSEC_PER_HOUR },
864 { "h", NSEC_PER_HOUR },
865 { "days", NSEC_PER_DAY },
866 { "day", NSEC_PER_DAY },
867 { "d", NSEC_PER_DAY },
868 { "weeks", NSEC_PER_WEEK },
869 { "week", NSEC_PER_WEEK },
870 { "w", NSEC_PER_WEEK },
871 { "years", NSEC_PER_YEAR },
872 { "year", NSEC_PER_YEAR },
873 { "y", NSEC_PER_YEAR },
874 { "usec", NSEC_PER_USEC },
875 { "us", NSEC_PER_USEC },
876 { "nsec", 1ULL },
877 { "ns", 1ULL },
878 { "", 1ULL }, /* default is nsec */
879 };
880
e73c78c2 881 const char *p, *s;
9a98c7a1 882 nsec_t r = 0;
cb0dac05 883 bool something = false;
9a98c7a1
LP
884
885 assert(t);
886 assert(nsec);
887
888 p = t;
e73c78c2
LP
889
890 p += strspn(p, WHITESPACE);
891 s = startswith(p, "infinity");
892 if (s) {
893 s += strspn(s, WHITESPACE);
8e8933ca 894 if (*s != 0)
e73c78c2
LP
895 return -EINVAL;
896
897 *nsec = NSEC_INFINITY;
898 return 0;
899 }
900
cb0dac05
LP
901 for (;;) {
902 long long l, z = 0;
9a98c7a1 903 char *e;
cb0dac05
LP
904 unsigned i, n = 0;
905
906 p += strspn(p, WHITESPACE);
907
908 if (*p == 0) {
909 if (!something)
910 return -EINVAL;
911
912 break;
913 }
9a98c7a1
LP
914
915 errno = 0;
916 l = strtoll(p, &e, 10);
917
8333c77e 918 if (errno > 0)
9a98c7a1
LP
919 return -errno;
920
921 if (l < 0)
922 return -ERANGE;
923
cb0dac05
LP
924 if (*e == '.') {
925 char *b = e + 1;
926
927 errno = 0;
928 z = strtoll(b, &e, 10);
929 if (errno > 0)
930 return -errno;
931
932 if (z < 0)
933 return -ERANGE;
934
935 if (e == b)
936 return -EINVAL;
937
938 n = e - b;
939
940 } else if (e == p)
9a98c7a1
LP
941 return -EINVAL;
942
943 e += strspn(e, WHITESPACE);
944
945 for (i = 0; i < ELEMENTSOF(table); i++)
946 if (startswith(e, table[i].suffix)) {
cb0dac05
LP
947 nsec_t k = (nsec_t) z * table[i].nsec;
948
949 for (; n > 0; n--)
950 k /= 10;
951
952 r += (nsec_t) l * table[i].nsec + k;
9a98c7a1 953 p = e + strlen(table[i].suffix);
cb0dac05
LP
954
955 something = true;
9a98c7a1
LP
956 break;
957 }
958
959 if (i >= ELEMENTSOF(table))
960 return -EINVAL;
961
cb0dac05 962 }
9a98c7a1
LP
963
964 *nsec = r;
965
966 return 0;
967}
03cc26dd
LP
968
969bool ntp_synced(void) {
970 struct timex txc = {};
971
972 if (adjtimex(&txc) < 0)
973 return false;
974
975 if (txc.status & STA_UNSYNC)
976 return false;
977
978 return true;
979}
75683450
LP
980
981int get_timezones(char ***ret) {
982 _cleanup_fclose_ FILE *f = NULL;
983 _cleanup_strv_free_ char **zones = NULL;
984 size_t n_zones = 0, n_allocated = 0;
985
986 assert(ret);
987
988 zones = strv_new("UTC", NULL);
989 if (!zones)
990 return -ENOMEM;
991
992 n_allocated = 2;
993 n_zones = 1;
994
995 f = fopen("/usr/share/zoneinfo/zone.tab", "re");
996 if (f) {
997 char l[LINE_MAX];
998
999 FOREACH_LINE(l, f, return -errno) {
1000 char *p, *w;
1001 size_t k;
1002
1003 p = strstrip(l);
1004
1005 if (isempty(p) || *p == '#')
1006 continue;
1007
1008 /* Skip over country code */
1009 p += strcspn(p, WHITESPACE);
1010 p += strspn(p, WHITESPACE);
1011
1012 /* Skip over coordinates */
1013 p += strcspn(p, WHITESPACE);
1014 p += strspn(p, WHITESPACE);
1015
1016 /* Found timezone name */
1017 k = strcspn(p, WHITESPACE);
1018 if (k <= 0)
1019 continue;
1020
1021 w = strndup(p, k);
1022 if (!w)
1023 return -ENOMEM;
1024
1025 if (!GREEDY_REALLOC(zones, n_allocated, n_zones + 2)) {
1026 free(w);
1027 return -ENOMEM;
1028 }
1029
1030 zones[n_zones++] = w;
1031 zones[n_zones] = NULL;
1032 }
1033
1034 strv_sort(zones);
1035
1036 } else if (errno != ENOENT)
1037 return -errno;
1038
1039 *ret = zones;
1040 zones = NULL;
1041
1042 return 0;
1043}
1044
1045bool timezone_is_valid(const char *name) {
1046 bool slash = false;
1047 const char *p, *t;
1048 struct stat st;
1049
5c904ba5
LP
1050 if (isempty(name))
1051 return false;
1052
1053 if (name[0] == '/')
75683450
LP
1054 return false;
1055
1056 for (p = name; *p; p++) {
1057 if (!(*p >= '0' && *p <= '9') &&
1058 !(*p >= 'a' && *p <= 'z') &&
1059 !(*p >= 'A' && *p <= 'Z') &&
1060 !(*p == '-' || *p == '_' || *p == '+' || *p == '/'))
1061 return false;
1062
1063 if (*p == '/') {
1064
1065 if (slash)
1066 return false;
1067
1068 slash = true;
1069 } else
1070 slash = false;
1071 }
1072
1073 if (slash)
1074 return false;
1075
63c372cb 1076 t = strjoina("/usr/share/zoneinfo/", name);
75683450
LP
1077 if (stat(t, &st) < 0)
1078 return false;
1079
1080 if (!S_ISREG(st.st_mode))
1081 return false;
1082
1083 return true;
1084}
77ff2de9
TG
1085
1086clockid_t clock_boottime_or_monotonic(void) {
1087 static clockid_t clock = -1;
1088 int fd;
1089
1090 if (clock != -1)
1091 return clock;
1092
1093 fd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK|TFD_CLOEXEC);
1094 if (fd < 0)
1095 clock = CLOCK_MONOTONIC;
1096 else {
1097 safe_close(fd);
1098 clock = CLOCK_BOOTTIME;
1099 }
1100
1101 return clock;
1102}
5c904ba5 1103
64d6c229 1104int get_timezone(char **tz) {
5c904ba5
LP
1105 _cleanup_free_ char *t = NULL;
1106 const char *e;
1107 char *z;
1108 int r;
1109
1110 r = readlink_malloc("/etc/localtime", &t);
1111 if (r < 0)
1112 return r; /* returns EINVAL if not a symlink */
1113
1114 e = path_startswith(t, "/usr/share/zoneinfo/");
1115 if (!e)
1116 e = path_startswith(t, "../usr/share/zoneinfo/");
1117 if (!e)
1118 return -EINVAL;
1119
1120 if (!timezone_is_valid(e))
1121 return -EINVAL;
1122
1123 z = strdup(e);
1124 if (!z)
1125 return -ENOMEM;
1126
64d6c229 1127 *tz = z;
5c904ba5
LP
1128 return 0;
1129}
7c67c79c
HV
1130
1131time_t mktime_or_timegm(struct tm *tm, bool utc) {
1132 return utc ? timegm(tm) : mktime(tm);
1133}
1134
1135struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc) {
1136 return utc ? gmtime_r(t, tm) : localtime_r(t, tm);
1137}
87b8ce69
SS
1138
1139unsigned long usec_to_jiffies(usec_t u) {
1140 static thread_local unsigned long hz = 0;
1141 long r;
1142
1143 if (hz == 0) {
1144 r = sysconf(_SC_CLK_TCK);
1145
1146 assert(r > 0);
1147 hz = (unsigned long) r;
1148 }
1149
1150 return DIV_ROUND_UP(u , USEC_PER_SEC / hz);
1151}