• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 25409762285

05 May 2026 08:45PM UTC coverage: 72.658% (-0.02%) from 72.674%
25409762285

push

github

web-flow
Couple of coverity fixes (#41951)

0 of 11 new or added lines in 2 files covered. (0.0%)

2705 existing lines in 63 files now uncovered.

326249 of 449021 relevant lines covered (72.66%)

1212712.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.12
/src/basic/time-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <stdlib.h>
4
#include <sys/timerfd.h>
5
#include <threads.h>
6
#include <unistd.h>
7

8
#include "alloc-util.h"
9
#include "env-util.h"
10
#include "errno-util.h"
11
#include "extract-word.h"
12
#include "hexdecoct.h"          /* IWYU pragma: keep */
13
#include "fd-util.h"
14
#include "fileio.h"
15
#include "fs-util.h"
16
#include "io-util.h"
17
#include "log.h"
18
#include "parse-util.h"
19
#include "path-util.h"
20
#include "stat-util.h"
21
#include "stdio-util.h"
22
#include "string-table.h"
23
#include "string-util.h"
24
#include "strv.h"
25
#include "time-util.h"
26

27
static clockid_t map_clock_id(clockid_t c) {
32,968,137✔
28

29
        /* Some more exotic archs (s390, ppc, …) lack the "ALARM" flavour of the clocks. Thus,
30
         * clock_gettime() will fail for them. Since they are essentially the same as their non-ALARM
31
         * pendants (their only difference is when timers are set on them), let's just map them
32
         * accordingly. This way, we can get the correct time even on those archs. */
33

34
        switch (c) {
32,968,137✔
35

36
        case CLOCK_BOOTTIME_ALARM:
37
                return CLOCK_BOOTTIME;
38

39
        case CLOCK_REALTIME_ALARM:
8✔
40
                return CLOCK_REALTIME;
8✔
41

42
        default:
32,968,128✔
43
                return c;
32,968,128✔
44
        }
45
}
46

47
usec_t now(clockid_t clock_id) {
32,965,114✔
48
        struct timespec ts;
32,965,114✔
49

50
        assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
32,965,114✔
51

52
        usec_t n = timespec_load(&ts);
32,965,114✔
53

54
        /* We use both 0 and USEC_INFINITY as niche values. If the current time collides with either, things are
55
         * really weird and really broken. Let's not allow this to go through, it would break too many of our
56
         * assumptions in code. */
57
        assert(n > 0);
32,965,114✔
58
        assert(n < USEC_INFINITY);
32,965,114✔
59

60
        return n;
32,965,114✔
61
}
62

63
nsec_t now_nsec(clockid_t clock_id) {
639✔
64
        struct timespec ts;
639✔
65

66
        assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
639✔
67

68
        nsec_t n = timespec_load_nsec(&ts);
639✔
69

70
        assert(n > 0);
639✔
71
        assert(n < NSEC_INFINITY);
639✔
72

73
        return n;
639✔
74
}
75

76
dual_timestamp* dual_timestamp_now(dual_timestamp *ts) {
2,030,150✔
77
        assert(ts);
2,030,150✔
78

79
        ts->realtime = now(CLOCK_REALTIME);
2,030,150✔
80
        ts->monotonic = now(CLOCK_MONOTONIC);
2,030,150✔
81

82
        return ts;
2,030,150✔
83
}
84

85
triple_timestamp* triple_timestamp_now(triple_timestamp *ts) {
3,730,523✔
86
        assert(ts);
3,730,523✔
87

88
        ts->realtime = now(CLOCK_REALTIME);
3,730,523✔
89
        ts->monotonic = now(CLOCK_MONOTONIC);
3,730,523✔
90
        ts->boottime = now(CLOCK_BOOTTIME);
3,730,523✔
91

92
        return ts;
3,730,523✔
93
}
94

95
usec_t map_clock_usec_raw(usec_t from, usec_t from_base, usec_t to_base) {
11,729✔
96

97
        /* Maps the time 'from' between two clocks, based on a common reference point where the first clock
98
         * is at 'from_base' and the second clock at 'to_base'. Basically calculates:
99
         *
100
         *         from - from_base + to_base
101
         *
102
         * But takes care of overflows/underflows and avoids signed operations. */
103

104
        if (from >= from_base) { /* In the future */
11,729✔
105
                usec_t delta = from - from_base;
67✔
106

107
                if (to_base >= USEC_INFINITY - delta) /* overflow? */
67✔
108
                        return USEC_INFINITY;
109

110
                return to_base + delta;
67✔
111

112
        } else { /* In the past */
113
                usec_t delta = from_base - from;
11,662✔
114

115
                if (to_base <= delta) /* underflow? */
11,662✔
116
                        return 0;
117

118
                return to_base - delta;
11,655✔
119
        }
120
}
121

122
usec_t map_clock_usec(usec_t from, clockid_t from_clock, clockid_t to_clock) {
495✔
123

124
        /* Try to avoid any inaccuracy needlessly added in case we convert from effectively the same clock
125
         * onto itself */
126
        if (map_clock_id(from_clock) == map_clock_id(to_clock))
495✔
127
                return from;
128

129
        /* Keep infinity as is */
130
        if (from == USEC_INFINITY)
495✔
131
                return from;
132

133
        return map_clock_usec_raw(from, now(from_clock), now(to_clock));
449✔
134
}
135

136
dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) {
12✔
137
        assert(ts);
12✔
138

139
        if (!timestamp_is_set(u)) {
12✔
140
                ts->realtime = ts->monotonic = u;
×
141
                return ts;
×
142
        }
143

144
        ts->realtime = u;
12✔
145
        ts->monotonic = map_clock_usec(u, CLOCK_REALTIME, CLOCK_MONOTONIC);
12✔
146
        return ts;
12✔
147
}
148

149
triple_timestamp* triple_timestamp_from_realtime(triple_timestamp *ts, usec_t u) {
346✔
150
        usec_t nowr;
346✔
151

152
        assert(ts);
346✔
153

154
        if (!timestamp_is_set(u)) {
346✔
155
                ts->realtime = ts->monotonic = ts->boottime = u;
×
156
                return ts;
×
157
        }
158

159
        nowr = now(CLOCK_REALTIME);
346✔
160

161
        ts->realtime = u;
346✔
162
        ts->monotonic = map_clock_usec_raw(u, nowr, now(CLOCK_MONOTONIC));
346✔
163
        ts->boottime = map_clock_usec_raw(u, nowr, now(CLOCK_BOOTTIME));
346✔
164

165
        return ts;
346✔
166
}
167

168
triple_timestamp* triple_timestamp_from_boottime(triple_timestamp *ts, usec_t u) {
×
169
        usec_t nowb;
×
170

171
        assert(ts);
×
172

173
        if (u == USEC_INFINITY) {
×
174
                ts->realtime = ts->monotonic = ts->boottime = u;
×
175
                return ts;
×
176
        }
177

178
        nowb = now(CLOCK_BOOTTIME);
×
179

180
        ts->boottime = u;
×
181
        ts->monotonic = map_clock_usec_raw(u, nowb, now(CLOCK_MONOTONIC));
×
182
        ts->realtime = map_clock_usec_raw(u, nowb, now(CLOCK_REALTIME));
×
183

184
        return ts;
×
185
}
186

187
dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) {
252✔
188
        assert(ts);
252✔
189

190
        if (u == USEC_INFINITY) {
252✔
191
                ts->realtime = ts->monotonic = USEC_INFINITY;
×
192
                return ts;
×
193
        }
194

195
        ts->monotonic = u;
252✔
196
        ts->realtime = map_clock_usec(u, CLOCK_MONOTONIC, CLOCK_REALTIME);
252✔
197
        return ts;
252✔
198
}
199

200
dual_timestamp* dual_timestamp_from_boottime(dual_timestamp *ts, usec_t u) {
1,764✔
201
        usec_t nowm;
1,764✔
202

203
        assert(ts);
1,764✔
204

205
        if (u == USEC_INFINITY) {
1,764✔
206
                ts->realtime = ts->monotonic = USEC_INFINITY;
×
207
                return ts;
×
208
        }
209

210
        nowm = now(CLOCK_BOOTTIME);
1,764✔
211
        ts->monotonic = map_clock_usec_raw(u, nowm, now(CLOCK_MONOTONIC));
1,764✔
212
        ts->realtime = map_clock_usec_raw(u, nowm, now(CLOCK_REALTIME));
1,764✔
213
        return ts;
1,764✔
214
}
215

216
usec_t triple_timestamp_by_clock(triple_timestamp *ts, clockid_t clock) {
4,381,184✔
217
        assert(ts);
4,381,184✔
218

219
        switch (clock) {
4,381,184✔
220

221
        case CLOCK_REALTIME:
2,319,693✔
222
        case CLOCK_REALTIME_ALARM:
223
                return ts->realtime;
2,319,693✔
224

225
        case CLOCK_MONOTONIC:
2,009,727✔
226
                return ts->monotonic;
2,009,727✔
227

228
        case CLOCK_BOOTTIME:
51,764✔
229
        case CLOCK_BOOTTIME_ALARM:
230
                return ts->boottime;
51,764✔
231

232
        default:
233
                return USEC_INFINITY;
234
        }
235
}
236

237
usec_t timespec_load(const struct timespec *ts) {
33,630,246✔
238
        assert(ts);
33,630,246✔
239

240
        if (ts->tv_sec < 0 || ts->tv_nsec < 0)
33,630,246✔
241
                return USEC_INFINITY;
242

243
        if ((usec_t) ts->tv_sec > (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC)
33,630,246✔
244
                return USEC_INFINITY;
245

246
        return
33,630,246✔
247
                (usec_t) ts->tv_sec * USEC_PER_SEC +
33,630,246✔
248
                (usec_t) ts->tv_nsec / NSEC_PER_USEC;
249
}
250

251
nsec_t timespec_load_nsec(const struct timespec *ts) {
1,463✔
252
        assert(ts);
1,463✔
253

254
        if (ts->tv_sec < 0 || ts->tv_nsec < 0)
1,463✔
255
                return NSEC_INFINITY;
256

257
        if ((nsec_t) ts->tv_sec >= (UINT64_MAX - ts->tv_nsec) / NSEC_PER_SEC)
1,463✔
258
                return NSEC_INFINITY;
259

260
        return (nsec_t) ts->tv_sec * NSEC_PER_SEC + (nsec_t) ts->tv_nsec;
1,463✔
261
}
262

263
struct timespec *timespec_store(struct timespec *ts, usec_t u) {
2,506,828✔
264
        assert(ts);
2,506,828✔
265

266
        if (u == USEC_INFINITY ||
2,506,828✔
267
            u / USEC_PER_SEC >= TIME_T_MAX) {
268
                ts->tv_sec = (time_t) -1;
×
269
                ts->tv_nsec = -1L;
×
270
                return ts;
×
271
        }
272

273
        ts->tv_sec = (time_t) (u / USEC_PER_SEC);
2,506,828✔
274
        ts->tv_nsec = (long) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
2,506,828✔
275

276
        return ts;
2,506,828✔
277
}
278

279
struct timespec *timespec_store_nsec(struct timespec *ts, nsec_t n) {
56✔
280
        assert(ts);
56✔
281

282
        if (n == NSEC_INFINITY ||
56✔
283
            n / NSEC_PER_SEC >= TIME_T_MAX) {
284
                ts->tv_sec = (time_t) -1;
×
285
                ts->tv_nsec = -1L;
×
286
                return ts;
×
287
        }
288

289
        ts->tv_sec = (time_t) (n / NSEC_PER_SEC);
56✔
290
        ts->tv_nsec = (long) (n % NSEC_PER_SEC);
56✔
291

292
        return ts;
56✔
293
}
294

295
usec_t timeval_load(const struct timeval *tv) {
1,157,420✔
296
        assert(tv);
1,157,420✔
297

298
        if (tv->tv_sec < 0 || tv->tv_usec < 0)
1,157,420✔
299
                return USEC_INFINITY;
300

301
        if ((usec_t) tv->tv_sec > (UINT64_MAX - tv->tv_usec) / USEC_PER_SEC)
1,157,420✔
302
                return USEC_INFINITY;
303

304
        return
1,157,420✔
305
                (usec_t) tv->tv_sec * USEC_PER_SEC +
1,157,420✔
306
                (usec_t) tv->tv_usec;
307
}
308

309
struct timeval *timeval_store(struct timeval *tv, usec_t u) {
173,461✔
310
        assert(tv);
173,461✔
311

312
        if (u == USEC_INFINITY ||
173,461✔
313
            u / USEC_PER_SEC > TIME_T_MAX) {
314
                tv->tv_sec = (time_t) -1;
×
315
                tv->tv_usec = (suseconds_t) -1;
×
316
        } else {
317
                tv->tv_sec = (time_t) (u / USEC_PER_SEC);
173,461✔
318
                tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
173,461✔
319
        }
320

321
        return tv;
173,461✔
322
}
323

324
char* format_timestamp_style(
7,997✔
325
                char *buf,
326
                size_t l,
327
                usec_t t,
328
                TimestampStyle style) {
329

330
        /* The weekdays in non-localized (English) form. We use this instead of the localized form, so that
331
         * our generated timestamps may be parsed with parse_timestamp(), and always read the same. */
332
        static const char * const weekdays[] = {
7,997✔
333
                [0] = "Sun",
334
                [1] = "Mon",
335
                [2] = "Tue",
336
                [3] = "Wed",
337
                [4] = "Thu",
338
                [5] = "Fri",
339
                [6] = "Sat",
340
        };
341

342
        struct tm tm;
7,997✔
343
        bool utc, us;
7,997✔
344
        size_t n;
7,997✔
345

346
        assert(buf);
7,997✔
347
        assert(style >= 0);
7,997✔
348
        assert(style < _TIMESTAMP_STYLE_MAX);
7,997✔
349

350
        if (!timestamp_is_set(t))
7,997✔
351
                return NULL; /* Timestamp is unset */
7,997✔
352

353
        if (style == TIMESTAMP_UNIX) {
5,464✔
354
                if (l < (size_t) (1 + 1 + 1))
102✔
355
                        return NULL; /* not enough space for even the shortest of forms */
356

357
                return snprintf_ok(buf, l, "@" USEC_FMT, t / USEC_PER_SEC);  /* round down μs → s */
102✔
358
        }
359

360
        utc = IN_SET(style, TIMESTAMP_UTC, TIMESTAMP_US_UTC, TIMESTAMP_DATE);
5,362✔
361
        us = IN_SET(style, TIMESTAMP_US, TIMESTAMP_US_UTC);
5,362✔
362

363
        if (l < (size_t) (3 +                   /* week day */
4,860✔
364
                          1 + 10 +              /* space and date */
365
                          style == TIMESTAMP_DATE ? 0 :
366
                          (1 + 8 +              /* space and time */
367
                           (us ? 1 + 6 : 0) +   /* "." and microsecond part */
5,362✔
368
                           1 + (utc ? 3 : 1)) + /* space and shortest possible zone */
10,382✔
369
                          1))
370
                return NULL; /* Not enough space even for the shortest form. */
371

372
        /* Let's not format times with years > 9999 */
373
        if (t > USEC_TIMESTAMP_FORMATTABLE_MAX) {
5,362✔
374
                static const char* const xxx[_TIMESTAMP_STYLE_MAX] = {
4✔
375
                        [TIMESTAMP_PRETTY] = "--- XXXX-XX-XX XX:XX:XX",
376
                        [TIMESTAMP_US]     = "--- XXXX-XX-XX XX:XX:XX.XXXXXX",
377
                        [TIMESTAMP_UTC]    = "--- XXXX-XX-XX XX:XX:XX UTC",
378
                        [TIMESTAMP_US_UTC] = "--- XXXX-XX-XX XX:XX:XX.XXXXXX UTC",
379
                        [TIMESTAMP_DATE]   = "--- XXXX-XX-XX",
380
                };
381

382
                assert(l >= strlen(xxx[style]) + 1);
4✔
383
                return strcpy(buf, xxx[style]);
4✔
384
        }
385

386
        if (localtime_or_gmtime_usec(t, utc, &tm) < 0)
5,358✔
387
                return NULL;
388

389
        /* Start with the week day */
390
        assert((size_t) tm.tm_wday < ELEMENTSOF(weekdays));
5,358✔
391
        memcpy(buf, weekdays[tm.tm_wday], 4);
5,358✔
392

393
        if (style == TIMESTAMP_DATE) {
5,358✔
394
                /* Special format string if only date should be shown. */
395
                if (strftime(buf + 3, l - 3, " %Y-%m-%d", &tm) <= 0)
105✔
396
                        return NULL; /* Doesn't fit */
397

398
                return buf;
105✔
399
        }
400

401
        /* Add the main components */
402
        if (strftime(buf + 3, l - 3, " %Y-%m-%d %H:%M:%S", &tm) <= 0)
5,253✔
403
                return NULL; /* Doesn't fit */
404

405
        /* Append the microseconds part, if that's requested */
406
        if (us) {
5,253✔
407
                n = strlen(buf);
501✔
408
                if (n + 8 > l)
501✔
409
                        return NULL; /* Microseconds part doesn't fit. */
410

411
                sprintf(buf + n, ".%06"PRI_USEC, t % USEC_PER_SEC);
501✔
412
        }
413

414
        /* Append the timezone */
415
        n = strlen(buf);
5,253✔
416
        if (utc) {
5,253✔
417
                /* If this is UTC then let's explicitly use the "UTC" string here, because gmtime_r()
418
                 * normally uses the obsolete "GMT" instead. */
419
                if (n + 5 > l)
234✔
420
                        return NULL; /* "UTC" doesn't fit. */
421

422
                strcpy(buf + n, " UTC");
234✔
423

424
        } else if (!isempty(tm.tm_zone)) {
5,019✔
425
                size_t tn;
5,019✔
426

427
                /* An explicit timezone is specified, let's use it, if it fits */
428
                tn = strlen(tm.tm_zone);
5,019✔
429
                if (n + 1 + tn + 1 > l) {
5,019✔
430
                        /* The full time zone does not fit in. Yuck. */
431

432
                        if (n + 1 + _POSIX_TZNAME_MAX + 1 > l)
×
433
                                return NULL; /* Not even enough space for the POSIX minimum (of 6)? In that
×
434
                                              * case, complain that it doesn't fit. */
435

436
                        /* So the time zone doesn't fit in fully, but the caller passed enough space for the
437
                         * POSIX minimum time zone length. In this case suppress the timezone entirely, in
438
                         * order not to dump an overly long, hard to read string on the user. This should be
439
                         * safe, because the user will assume the local timezone anyway if none is shown. And
440
                         * so does parse_timestamp(). */
441
                } else {
442
                        buf[n++] = ' ';
5,019✔
443
                        strcpy(buf + n, tm.tm_zone);
5,019✔
444
                }
445
        }
446

447
        return buf;
448
}
449

450
char* format_timestamp_relative_full(char *buf, size_t l, usec_t t, clockid_t clock, bool implicit_left) {
1,444✔
451
        const char *s;
1,444✔
452
        usec_t n, d;
1,444✔
453

454
        assert(buf);
1,444✔
455

456
        if (!timestamp_is_set(t))
1,444✔
457
                return NULL;
458

459
        n = now(clock);
1,438✔
460
        if (n > t) {
1,438✔
461
                d = n - t;
1,242✔
462
                s = " ago";
1,242✔
463
        } else {
464
                d = t - n;
196✔
465
                s = implicit_left ? "" : " left";
196✔
466
        }
467

468
        if (d >= USEC_PER_YEAR) {
1,438✔
469
                usec_t years = d / USEC_PER_YEAR;
742✔
470
                usec_t months = (d % USEC_PER_YEAR) / USEC_PER_MONTH;
742✔
471

472
                (void) snprintf(buf, l, USEC_FMT " %s " USEC_FMT " %s%s",
2,194✔
473
                                years,
474
                                years == 1 ? "year" : "years",
475
                                months,
476
                                months == 1 ? "month" : "months",
477
                                s);
478
        } else if (d >= USEC_PER_MONTH) {
696✔
479
                usec_t months = d / USEC_PER_MONTH;
13✔
480
                usec_t days = (d % USEC_PER_MONTH) / USEC_PER_DAY;
13✔
481

482
                (void) snprintf(buf, l, USEC_FMT " %s " USEC_FMT " %s%s",
27✔
483
                                months,
484
                                months == 1 ? "month" : "months",
485
                                days,
486
                                days == 1 ? "day" : "days",
487
                                s);
488
        } else if (d >= USEC_PER_WEEK) {
683✔
489
                usec_t weeks = d / USEC_PER_WEEK;
8✔
490
                usec_t days = (d % USEC_PER_WEEK) / USEC_PER_DAY;
8✔
491

492
                (void) snprintf(buf, l, USEC_FMT " %s " USEC_FMT " %s%s",
16✔
493
                                weeks,
494
                                weeks == 1 ? "week" : "weeks",
495
                                days,
496
                                days == 1 ? "day" : "days",
497
                                s);
498
        } else if (d >= 2*USEC_PER_DAY)
675✔
499
                (void) snprintf(buf, l, USEC_FMT " days%s", d / USEC_PER_DAY,s);
×
500
        else if (d >= 25*USEC_PER_HOUR)
675✔
501
                (void) snprintf(buf, l, "1 day " USEC_FMT "h%s",
11✔
502
                                (d - USEC_PER_DAY) / USEC_PER_HOUR, s);
11✔
503
        else if (d >= 6*USEC_PER_HOUR)
664✔
504
                (void) snprintf(buf, l, USEC_FMT "h%s",
21✔
505
                                d / USEC_PER_HOUR, s);
506
        else if (d >= USEC_PER_HOUR)
643✔
507
                (void) snprintf(buf, l, USEC_FMT "h " USEC_FMT "min%s",
6✔
508
                                d / USEC_PER_HOUR,
509
                                (d % USEC_PER_HOUR) / USEC_PER_MINUTE, s);
6✔
510
        else if (d >= 5*USEC_PER_MINUTE)
637✔
511
                (void) snprintf(buf, l, USEC_FMT "min%s",
8✔
512
                                d / USEC_PER_MINUTE, s);
513
        else if (d >= USEC_PER_MINUTE)
629✔
514
                (void) snprintf(buf, l, USEC_FMT "min " USEC_FMT "s%s",
1✔
515
                                d / USEC_PER_MINUTE,
516
                                (d % USEC_PER_MINUTE) / USEC_PER_SEC, s);
1✔
517
        else if (d >= USEC_PER_SEC)
628✔
518
                (void) snprintf(buf, l, USEC_FMT "s%s",
524✔
519
                                d / USEC_PER_SEC, s);
520
        else if (d >= USEC_PER_MSEC)
104✔
521
                (void) snprintf(buf, l, USEC_FMT "ms%s",
101✔
522
                                d / USEC_PER_MSEC, s);
523
        else if (d > 0)
3✔
524
                (void) snprintf(buf, l, USEC_FMT"us%s",
3✔
525
                                d, s);
526
        else
527
                (void) snprintf(buf, l, "now");
×
528

529
        buf[l-1] = 0;
1,438✔
530
        return buf;
1,438✔
531
}
532

533
char* format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
18,982✔
534
        static const struct {
18,982✔
535
                const char *suffix;
536
                usec_t usec;
537
        } table[] = {
538
                { "y",     USEC_PER_YEAR   },
539
                { "month", USEC_PER_MONTH  },
540
                { "w",     USEC_PER_WEEK   },
541
                { "d",     USEC_PER_DAY    },
542
                { "h",     USEC_PER_HOUR   },
543
                { "min",   USEC_PER_MINUTE },
544
                { "s",     USEC_PER_SEC    },
545
                { "ms",    USEC_PER_MSEC   },
546
                { "us",    1               },
547
        };
548

549
        char *p = ASSERT_PTR(buf);
18,982✔
550
        bool something = false;
18,982✔
551

552
        assert(l > 0);
18,982✔
553

554
        if (t == USEC_INFINITY) {
18,982✔
555
                strncpy(p, "infinity", l-1);
1,872✔
556
                p[l-1] = 0;
1,872✔
557
                return p;
1,872✔
558
        }
559

560
        if (t <= 0) {
17,110✔
561
                strncpy(p, "0", l-1);
770✔
562
                p[l-1] = 0;
770✔
563
                return p;
770✔
564
        }
565

566
        /* The result of this function can be parsed with parse_sec */
567

568
        FOREACH_ELEMENT(i, table) {
137,305✔
569
                int k = 0;
137,210✔
570
                size_t n;
137,210✔
571
                bool done = false;
137,210✔
572
                usec_t a, b;
137,210✔
573

574
                if (t <= 0)
137,210✔
575
                        break;
576

577
                if (t < accuracy && something)
121,005✔
578
                        break;
579

580
                if (t < i->usec)
120,965✔
581
                        continue;
99,177✔
582

583
                if (l <= 1)
21,788✔
584
                        break;
585

586
                a = t / i->usec;
21,788✔
587
                b = t % i->usec;
21,788✔
588

589
                /* Let's see if we should shows this in dot notation */
590
                if (t < USEC_PER_MINUTE && b > 0) {
21,788✔
591
                        signed char j = 0;
592

593
                        for (usec_t cc = i->usec; cc > 1; cc /= 10)
66,837✔
594
                                j++;
54,834✔
595

596
                        for (usec_t cc = accuracy; cc > 1; cc /= 10) {
52,584✔
597
                                b /= 10;
40,581✔
598
                                j--;
40,581✔
599
                        }
600

601
                        if (j > 0) {
12,003✔
602
                                k = snprintf(p, l,
7,586✔
603
                                             "%s"USEC_FMT".%0*"PRI_USEC"%s",
604
                                             p > buf ? " " : "",
605
                                             a,
606
                                             j,
607
                                             b,
608
                                             i->suffix);
3,793✔
609

610
                                t = 0;
3,793✔
611
                                done = true;
3,793✔
612
                        }
613
                }
614

615
                /* No? Then let's show it normally */
616
                if (!done) {
3,793✔
617
                        k = snprintf(p, l,
35,990✔
618
                                     "%s"USEC_FMT"%s",
619
                                     p > buf ? " " : "",
620
                                     a,
621
                                     i->suffix);
17,995✔
622

623
                        t = b;
17,995✔
624
                }
625

626
                n = MIN((size_t) k, l-1);
21,788✔
627

628
                l -= n;
21,788✔
629
                p += n;
21,788✔
630

631
                something = true;
21,788✔
632
        }
633

634
        *p = 0;
16,340✔
635

636
        return buf;
16,340✔
637
}
638

639
const char* get_tzname(bool dst) {
3,330✔
640
        /* musl leaves the DST timezone name unset if there is no DST, map this back to no DST */
641
        if (dst && isempty(tzname[1]))
3,331✔
642
                dst = false;
643

644
        return empty_to_null(tzname[dst]);
3,330✔
645
}
646

647
int parse_gmtoff(const char *t, long *ret) {
1,240✔
648
        assert(t);
1,240✔
649

650
        struct tm tm;
1,240✔
651
        const char *k = strptime(t, "%z", &tm);
1,240✔
652
        if (k && *k == '\0') {
1,240✔
653
                /* Success! */
654
                if (ret)
172✔
655
                        *ret = tm.tm_gmtoff;
172✔
656
                return 0;
1,240✔
657
        }
658

659
#ifdef __GLIBC__
660
        return -EINVAL;
661
#else
662
        int r;
663

664
        /* musl v1.2.5 does not support %z specifier in strptime(). Since
665
         * https://github.com/kraj/musl/commit/fced99e93daeefb0192fd16304f978d4401d1d77
666
         * %z is supported, but it only supports strict RFC-822/ISO 8601 format, that is, 4 digits with sign
667
         * (e.g. +0900 or -1400), but does not support extended format: 2 digits or colon separated 4 digits
668
         * (e.g. +09 or -14:00). Let's add fallback logic to make it support the extended timezone spec. */
669

670
        bool positive;
671
        switch (*t) {
672
        case '+':
673
                positive = true;
674
                break;
675
        case '-':
676
                positive = false;
677
                break;
678
        default:
679
                return -EINVAL;
680
        }
681

682
        t++;
683
        r = undecchar(*t);
684
        if (r < 0)
685
                return r;
686

687
        usec_t u = r * 10 * USEC_PER_HOUR;
688

689
        t++;
690
        r = undecchar(*t);
691
        if (r < 0)
692
                return r;
693
        u += r * USEC_PER_HOUR;
694

695
        t++;
696
        if (*t == '\0') /* 2 digits case */
697
                goto finalize;
698

699
        if (*t == ':') /* skip colon */
700
                t++;
701

702
        r = undecchar(*t);
703
        if (r < 0)
704
                return r;
705
        if (r >= 6) /* refuse minutes equal to or larger than 60 */
706
                return -EINVAL;
707

708
        u += r * 10 * USEC_PER_MINUTE;
709

710
        t++;
711
        r = undecchar(*t);
712
        if (r < 0)
713
                return r;
714

715
        u += r * USEC_PER_MINUTE;
716

717
        t++;
718
        if (*t != '\0')
719
                return -EINVAL;
720

721
finalize:
722
        if (u > USEC_PER_DAY) /* refuse larger than one day */
723
                return -EINVAL;
724

725
        if (ret) {
726
                long gmtoff = u / USEC_PER_SEC;
727
                *ret = positive ? gmtoff : -gmtoff;
728
        }
729

730
        return 0;
731
#endif
732
}
733

734
static int parse_timestamp_impl(
2,142✔
735
                const char *t,
736
                size_t max_len,
737
                bool utc,
738
                int isdst,
739
                long gmtoff,
740
                usec_t *ret) {
741

742
        static const struct {
2,142✔
743
                const char *name;
744
                const int nr;
745
        } day_nr[] = {
746
                { "Sunday",    0 },
747
                { "Sun",       0 },
748
                { "Monday",    1 },
749
                { "Mon",       1 },
750
                { "Tuesday",   2 },
751
                { "Tue",       2 },
752
                { "Wednesday", 3 },
753
                { "Wed",       3 },
754
                { "Thursday",  4 },
755
                { "Thu",       4 },
756
                { "Friday",    5 },
757
                { "Fri",       5 },
758
                { "Saturday",  6 },
759
                { "Sat",       6 },
760
        };
761

762
        _cleanup_free_ char *t_alloc = NULL;
2,142✔
763
        usec_t usec, plus = 0, minus = 0;
2,142✔
764
        bool with_tz = false;
2,142✔
765
        int r, weekday = -1;
2,142✔
766
        unsigned fractional = 0;
2,142✔
767
        const char *k;
2,142✔
768
        struct tm tm, copy;
2,142✔
769

770
        /* Allowed syntaxes:
771
         *
772
         *   2012-09-22 16:34:22.1[2[3[4[5[6]]]]]
773
         *   2012-09-22 16:34:22  (µsec will be set to 0)
774
         *   2012-09-22 16:34     (seconds will be set to 0)
775
         *   2012-09-22T16:34:22.1[2[3[4[5[6]]]]]
776
         *   2012-09-22T16:34:22  (µsec will be set to 0)
777
         *   2012-09-22T16:34     (seconds will be set to 0)
778
         *   2012-09-22           (time will be set to 00:00:00)
779
         *   16:34:22             (date will be set to today)
780
         *   16:34                (date will be set to today, seconds to 0)
781
         *   now
782
         *   yesterday            (time is set to 00:00:00)
783
         *   today                (time is set to 00:00:00)
784
         *   tomorrow             (time is set to 00:00:00)
785
         *   +5min
786
         *   -5days
787
         *   @2147483647          (seconds since epoch)
788
         *
789
         * Note, on DST change, 00:00:00 may not exist and in that case the time part may be shifted.
790
         * E.g. "Sun 2023-03-13 America/Havana" is parsed as "Sun 2023-03-13 01:00:00 CDT".
791
         *
792
         * A simplified strptime-spelled RFC3339 ABNF looks like
793
         *   "%Y-%m-%d" "T" "%H" ":" "%M" ":" "%S" [".%N"] ("Z" / (("+" / "-") "%H:%M"))
794
         * We additionally allow no seconds and inherited timezone
795
         * for symmetry with our other syntaxes and improved interactive usability:
796
         *   "%Y-%m-%d" "T" "%H" ":" "%M" ":" ["%S" [".%N"]] ["Z" / (("+" / "-") "%H:%M")]
797
         * RFC3339 defines time-secfrac to as "." 1*DIGIT, but we limit to 6 digits,
798
         * since we're limited to 1µs resolution.
799
         * We also accept "Sat 2012-09-22T16:34:22", RFC3339 warns against it.
800
         */
801

802
        assert(t);
2,142✔
803

804
        if (max_len != SIZE_MAX) {
2,142✔
805
                /* If the input string contains timezone, then cut it here. */
806

807
                if (max_len == 0) /* Can't be the only field */
1,692✔
808
                        return -EINVAL;
809

810
                t_alloc = strndup(t, max_len);
1,692✔
811
                if (!t_alloc)
1,692✔
812
                        return -ENOMEM;
813

814
                t = t_alloc;
815
                with_tz = true;
816
        }
817

818
        if (utc) {
2,142✔
819
                /* glibc accepts gmtoff more than 24 hours, but we refuse it. */
820
                if ((usec_t) labs(gmtoff) * USEC_PER_SEC > USEC_PER_DAY)
993✔
821
                        return -EINVAL;
822
        } else {
823
                if (gmtoff != 0)
1,149✔
824
                        return -EINVAL;
825
        }
826

827
        if (t[0] == '@' && !with_tz)
2,142✔
828
                return parse_sec(t + 1, ret);
107✔
829

830
        usec = now(CLOCK_REALTIME);
2,035✔
831

832
        if (!with_tz) {
2,035✔
833
                if (streq(t, "now"))
343✔
834
                        goto finish;
5✔
835

836
                if (t[0] == '+') {
338✔
837
                        r = parse_sec(t+1, &plus);
6✔
838
                        if (r < 0)
6✔
839
                                return r;
840

841
                        goto finish;
6✔
842
                }
843

844
                if (t[0] == '-') {
332✔
845
                        r = parse_sec(t+1, &minus);
6✔
846
                        if (r < 0)
6✔
847
                                return r;
848

849
                        goto finish;
6✔
850
                }
851

852
                if ((k = endswith(t, " ago"))) {
326✔
853
                        _cleanup_free_ char *buf = NULL;
3✔
854

855
                        buf = strndup(t, k - t);
3✔
856
                        if (!buf)
3✔
857
                                return -ENOMEM;
858

859
                        r = parse_sec(buf, &minus);
3✔
860
                        if (r < 0)
3✔
861
                                return r;
862

863
                        goto finish;
3✔
864
                }
865

866
                if ((k = endswith(t, " left"))) {
323✔
867
                        _cleanup_free_ char *buf = NULL;
102✔
868

869
                        buf = strndup(t, k - t);
102✔
870
                        if (!buf)
102✔
871
                                return -ENOMEM;
872

873
                        r = parse_sec(buf, &plus);
102✔
874
                        if (r < 0)
102✔
875
                                return r;
876

877
                        goto finish;
102✔
878
                }
879
        }
880

881
        r = localtime_or_gmtime_usec(usec, utc, &tm);
1,913✔
882
        if (r < 0)
1,913✔
883
                return r;
884

885
        tm.tm_isdst = isdst;
1,913✔
886

887
        if (streq(t, "today")) {
1,913✔
888
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
13✔
889
                goto from_tm;
13✔
890

891
        } else if (streq(t, "yesterday")) {
1,900✔
892
                tm.tm_mday--;
10✔
893
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
10✔
894
                goto from_tm;
10✔
895

896
        } else if (streq(t, "tomorrow")) {
1,890✔
897
                tm.tm_mday++;
8✔
898
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
8✔
899
                goto from_tm;
8✔
900
        }
901

902
        FOREACH_ELEMENT(day, day_nr) {
19,752✔
903
                k = startswith_no_case(t, day->name);
19,424✔
904
                if (!k || *k != ' ')
19,424✔
905
                        continue;
17,870✔
906

907
#ifdef __GLIBC__
908
                /* musl does not set tm_wday field and set 0 unless it is explicitly requested by %w or so.
909
                 * In the below, let's only check tm_wday field only when built with glibc. */
910
                weekday = day->nr;
1,554✔
911
#endif
912
                t = k + 1;
1,554✔
913
                break;
1,554✔
914
        }
915

916
        copy = tm;
1,882✔
917
        k = strptime(t, "%y-%m-%d %H:%M:%S", &tm);
1,882✔
918
        if (k) {
1,882✔
919
                if (*k == '.')
100✔
920
                        goto parse_usec;
64✔
921
                else if (*k == 0)
36✔
922
                        goto from_tm;
32✔
923
        }
924

925
        /* Our "canonical" RFC3339 syntax variant */
926
        tm = copy;
1,786✔
927
        k = strptime(t, "%Y-%m-%d %H:%M:%S", &tm);
1,786✔
928
        if (k) {
1,786✔
929
                if (*k == '.')
1,482✔
930
                        goto parse_usec;
345✔
931
                else if (*k == 0)
1,137✔
932
                        goto from_tm;
1,128✔
933
        }
934

935
        /* RFC3339 syntax */
936
        tm = copy;
313✔
937
        k = strptime(t, "%Y-%m-%dT%H:%M:%S", &tm);
313✔
938
        if (k) {
313✔
939
                if (*k == '.')
42✔
940
                        goto parse_usec;
20✔
941
                else if (*k == 0)
22✔
942
                        goto from_tm;
22✔
943
        }
944

945
        /* Support OUTPUT_SHORT and OUTPUT_SHORT_PRECISE formats */
946
        tm = copy;
271✔
947
        k = strptime(t, "%b %d %H:%M:%S", &tm);
271✔
948
        if (k) {
271✔
949
                if (*k == '.')
4✔
950
                        goto parse_usec;
2✔
951
                else if (*k == 0)
2✔
952
                        goto from_tm;
2✔
953
        }
954

955
        tm = copy;
267✔
956
        k = strptime(t, "%y-%m-%d %H:%M", &tm);
267✔
957
        if (k && *k == 0) {
267✔
958
                tm.tm_sec = 0;
30✔
959
                goto from_tm;
30✔
960
        }
961

962
        /* Our "canonical" RFC3339 syntax variant without seconds */
963
        tm = copy;
237✔
964
        k = strptime(t, "%Y-%m-%d %H:%M", &tm);
237✔
965
        if (k && *k == 0) {
237✔
966
                tm.tm_sec = 0;
31✔
967
                goto from_tm;
31✔
968
        }
969

970
        /* RFC3339 syntax without seconds */
971
        tm = copy;
206✔
972
        k = strptime(t, "%Y-%m-%dT%H:%M", &tm);
206✔
973
        if (k && *k == 0) {
206✔
974
                tm.tm_sec = 0;
8✔
975
                goto from_tm;
8✔
976
        }
977

978
        tm = copy;
198✔
979
        k = strptime(t, "%y-%m-%d", &tm);
198✔
980
        if (k && *k == 0) {
198✔
981
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
×
982
                goto from_tm;
×
983
        }
984

985
        tm = copy;
198✔
986
        k = strptime(t, "%Y-%m-%d", &tm);
198✔
987
        if (k && *k == 0) {
198✔
988
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
106✔
989
                goto from_tm;
106✔
990
        }
991

992
        tm = copy;
92✔
993
        k = strptime(t, "%H:%M:%S", &tm);
92✔
994
        if (k) {
92✔
995
                if (*k == '.')
34✔
996
                        goto parse_usec;
8✔
997
                else if (*k == 0)
26✔
998
                        goto from_tm;
26✔
999
        }
1000

1001
        tm = copy;
58✔
1002
        k = strptime(t, "%H:%M", &tm);
58✔
1003
        if (k && *k == 0) {
58✔
1004
                tm.tm_sec = 0;
6✔
1005
                goto from_tm;
6✔
1006
        }
1007

1008
        return -EINVAL;
1009

1010
parse_usec:
439✔
1011
        k++;
439✔
1012
        r = parse_fractional_part_u(&k, 6, &fractional);
439✔
1013
        if (r < 0)
439✔
1014
                return -EINVAL;
1015
        if (*k != '\0')
439✔
1016
                return -EINVAL;
1017

1018
from_tm:
423✔
1019
        assert(plus == 0);
1,845✔
1020
        assert(minus == 0);
1,845✔
1021

1022
        if (weekday >= 0 && tm.tm_wday != weekday)
1,845✔
1023
                return -EINVAL;
1024

1025
        if (gmtoff < 0) {
1,845✔
1026
                plus = -gmtoff * USEC_PER_SEC;
110✔
1027

1028
                /* If gmtoff is negative, the string may be too old to be parsed as UTC.
1029
                 * E.g. 1969-12-31 23:00:00 -06 == 1970-01-01 05:00:00 UTC
1030
                 * We assumed that gmtoff is in the range of -24:00…+24:00, hence the only date we need to
1031
                 * handle here is 1969-12-31. So, let's shift the date with one day, then subtract the shift
1032
                 * later. */
1033
                if (tm.tm_year == 69 && tm.tm_mon == 11 && tm.tm_mday == 31) {
110✔
1034
                        /* Thu 1970-01-01-00:00:00 */
1035
                        tm.tm_year = 70;
96✔
1036
                        tm.tm_mon = 0;
96✔
1037
                        tm.tm_mday = 1;
96✔
1038
                        tm.tm_wday = 4;
96✔
1039
                        tm.tm_yday = 0;
96✔
1040
                        minus = USEC_PER_DAY;
96✔
1041
                }
1042
        } else
1043
                minus = gmtoff * USEC_PER_SEC;
1,735✔
1044

1045
        r = mktime_or_timegm_usec(&tm, utc, &usec);
1,845✔
1046
        if (r < 0)
1,845✔
1047
                return r;
1048

1049
        usec = usec_add(usec, fractional);
3,684✔
1050

1051
finish:
1,964✔
1052
        usec = usec_add(usec, plus);
1,964✔
1053

1054
        if (usec < minus)
1,964✔
1055
                return -EINVAL;
1056

1057
        usec = usec_sub_unsigned(usec, minus);
1,963✔
1058

1059
        if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX)
1,963✔
1060
                return -EINVAL;
1061

1062
        if (ret)
1,962✔
1063
                *ret = usec;
1,960✔
1064
        return 0;
1065
}
1066

1067
int parse_timestamp(const char *t, usec_t *ret) {
2,109✔
1068
        long gmtoff;
2,109✔
1069
        int r;
2,109✔
1070

1071
        assert(t);
2,109✔
1072

1073
        size_t t_len = strlen(t);
2,109✔
1074
        if (t_len > 2 && t[t_len - 1] == 'Z') {
2,109✔
1075
                /* Try to parse as RFC3339-style welded UTC: "1985-04-12T23:20:50.52Z" */
1076
                r = parse_timestamp_impl(t, t_len - 1, /* utc= */ true, /* isdst= */ -1, /* gmtoff= */ 0, ret);
67✔
1077
                if (r >= 0)
67✔
1078
                        return r;
2,109✔
1079
        }
1080

1081
        /* RFC3339-style welded offset: "1990-12-31T15:59:60-08:00" */
1082
        if (t_len > 7 && IN_SET(t[t_len - 6], '+', '-') && t[t_len - 7] != ' ' && parse_gmtoff(&t[t_len - 6], &gmtoff) >= 0)
2,061✔
1083
                return parse_timestamp_impl(t, t_len - 6, /* utc= */ true, /* isdst= */ -1, gmtoff, ret);
40✔
1084

1085
        const char *tz = strrchr(t, ' ');
2,035✔
1086
        if (!tz)
2,035✔
1087
                return parse_timestamp_impl(t, /* max_len= */ SIZE_MAX, /* utc= */ false, /* isdst= */ -1, /* gmtoff= */ 0, ret);
195✔
1088

1089
        size_t max_len = tz - t;
1,840✔
1090
        tz++;
1,840✔
1091

1092
        /* Shortcut, parse the string as UTC. */
1093
        if (streq(tz, "UTC"))
1,840✔
1094
                return parse_timestamp_impl(t, max_len, /* utc= */ true, /* isdst= */ -1, /* gmtoff= */ 0, ret);
760✔
1095

1096
        /* If the timezone is compatible with RFC-822/ISO 8601 (e.g. +06, or -03:00) then parse the string as
1097
         * UTC and shift the result. Note, this must be earlier than the timezone check with tzname[], as
1098
         * tzname[] may be in the same format. */
1099
        if (parse_gmtoff(tz, &gmtoff) >= 0)
1,080✔
1100
                return parse_timestamp_impl(t, max_len, /* utc= */ true, /* isdst= */ -1, gmtoff, ret);
126✔
1101

1102
        /* Check if the last word matches tzname[] of the local timezone. Note, this must be done earlier
1103
         * than the check by timezone_is_valid() below, as some short timezone specifications have their own
1104
         * timezone files (e.g. WET has its own timezone file, but JST does not), but using such files does
1105
         * not follow the timezone change in the current area. */
1106
        tzset();
954✔
1107
        for (int j = 0; j <= 1; j++) {
3,816✔
1108
                if (!streq_ptr(tz, get_tzname(j)))
1,908✔
1109
                        continue;
1,908✔
1110

1111
                /* The specified timezone matches tzname[] of the local timezone. */
1112
                return parse_timestamp_impl(t, max_len, /* utc= */ false, /* isdst= */ j, /* gmtoff= */ 0, ret);
×
1113
        }
1114

1115
        /* If the last word is a valid timezone file (e.g. Asia/Tokyo), then save the current timezone, apply
1116
         * the specified timezone, and parse the remaining string as a local time. */
1117
        if (timezone_is_valid(tz, LOG_DEBUG)) {
954✔
1118
                SAVE_TIMEZONE;
1,398✔
1119

1120
                if (setenv("TZ", tz, /* overwrite= */ true) < 0)
699✔
1121
                        return negative_errno();
×
1122

1123
                return parse_timestamp_impl(t, max_len, /* utc= */ false, /* isdst= */ -1, /* gmtoff= */ 0, ret);
699✔
1124
        }
1125

1126
        /* Otherwise, assume that the last word is a part of the time and try to parse the whole string as a
1127
         * local time. */
1128
        return parse_timestamp_impl(t, SIZE_MAX, /* utc= */ false, /* isdst= */ -1, /* gmtoff= */ 0, ret);
255✔
1129
}
1130

1131
static const char* extract_multiplier(const char *p, usec_t *ret) {
14,967✔
1132
        static const struct {
14,967✔
1133
                const char *suffix;
1134
                usec_t usec;
1135
        } table[] = {
1136
                { "seconds", USEC_PER_SEC    },
1137
                { "second",  USEC_PER_SEC    },
1138
                { "sec",     USEC_PER_SEC    },
1139
                { "s",       USEC_PER_SEC    },
1140
                { "minutes", USEC_PER_MINUTE },
1141
                { "minute",  USEC_PER_MINUTE },
1142
                { "min",     USEC_PER_MINUTE },
1143
                { "months",  USEC_PER_MONTH  },
1144
                { "month",   USEC_PER_MONTH  },
1145
                { "M",       USEC_PER_MONTH  },
1146
                { "msec",    USEC_PER_MSEC   },
1147
                { "ms",      USEC_PER_MSEC   },
1148
                { "m",       USEC_PER_MINUTE },
1149
                { "hours",   USEC_PER_HOUR   },
1150
                { "hour",    USEC_PER_HOUR   },
1151
                { "hr",      USEC_PER_HOUR   },
1152
                { "h",       USEC_PER_HOUR   },
1153
                { "days",    USEC_PER_DAY    },
1154
                { "day",     USEC_PER_DAY    },
1155
                { "d",       USEC_PER_DAY    },
1156
                { "weeks",   USEC_PER_WEEK   },
1157
                { "week",    USEC_PER_WEEK   },
1158
                { "w",       USEC_PER_WEEK   },
1159
                { "years",   USEC_PER_YEAR   },
1160
                { "year",    USEC_PER_YEAR   },
1161
                { "y",       USEC_PER_YEAR   },
1162
                { "usec",    1ULL            },
1163
                { "us",      1ULL            },
1164
                { "μs",      1ULL            }, /* U+03bc (aka GREEK SMALL LETTER MU) */
1165
                { "µs",      1ULL            }, /* U+b5 (aka MICRO SIGN) */
1166
        };
1167

1168
        assert(p);
14,967✔
1169
        assert(ret);
14,967✔
1170

1171
        FOREACH_ELEMENT(i, table) {
244,223✔
1172
                const char *e = startswith(p, i->suffix);
239,195✔
1173
                if (e) {
239,195✔
1174
                        *ret = i->usec;
9,939✔
1175
                        return e;
9,939✔
1176
                }
1177
        }
1178

1179
        return p;
1180
}
1181

1182
int parse_time(const char *t, usec_t *ret, usec_t default_unit) {
15,231✔
1183
        const char *p, *s;
15,231✔
1184

1185
        assert(t);
15,231✔
1186
        assert(default_unit > 0);
15,231✔
1187

1188
        p = skip_leading_chars(t, /* bad= */ NULL);
15,231✔
1189
        s = startswith(p, "infinity");
15,231✔
1190
        if (s) {
15,231✔
1191
                if (!in_charset(s, WHITESPACE))
484✔
1192
                        return -EINVAL;
1193

1194
                if (ret)
483✔
1195
                        *ret = USEC_INFINITY;
483✔
1196
                return 0;
1197
        }
1198

1199
        usec_t usec = 0;
1200

1201
        for (bool something = false;;) {
14,951✔
1202
                usec_t multiplier = default_unit, k;
29,698✔
1203
                long long l;
29,698✔
1204
                char *e;
29,698✔
1205

1206
                p = skip_leading_chars(p, /* bad= */ NULL);
29,698✔
1207
                if (*p == 0) {
29,698✔
1208
                        if (!something)
14,709✔
1209
                                return -EINVAL;
43✔
1210

1211
                        break;
14,704✔
1212
                }
1213

1214
                if (*p == '-') /* Don't allow "-0" */
14,989✔
1215
                        return -ERANGE;
1216

1217
                errno = 0;
14,981✔
1218
                l = strtoll(p, &e, 10);
14,981✔
1219
                if (errno > 0)
14,981✔
1220
                        return -errno;
×
1221
                if (l < 0)
14,981✔
1222
                        return -ERANGE;
1223

1224
                if (*e == '.') {
14,981✔
1225
                        p = e + 1;
55✔
1226
                        p += strspn(p, DIGITS);
55✔
1227
                } else if (e == p)
14,926✔
1228
                        return -EINVAL;
1229
                else
1230
                        p = e;
1231

1232
                s = extract_multiplier(p + strspn(p, WHITESPACE), &multiplier);
14,967✔
1233
                if (s == p && *s != '\0')
14,967✔
1234
                        /* Don't allow '12.34.56', but accept '12.34 .56' or '12.34s.56' */
1235
                        return -EINVAL;
1236

1237
                p = s;
14,958✔
1238

1239
                if ((usec_t) l >= USEC_INFINITY / multiplier)
14,958✔
1240
                        return -ERANGE;
1241

1242
                k = (usec_t) l * multiplier;
14,956✔
1243
                if (k >= USEC_INFINITY - usec)
14,956✔
1244
                        return -ERANGE;
1245

1246
                usec += k;
14,956✔
1247

1248
                something = true;
14,956✔
1249

1250
                if (*e == '.') {
14,956✔
1251
                        usec_t m = multiplier / 10;
48✔
1252
                        const char *b;
48✔
1253

1254
                        for (b = e + 1; *b >= '0' && *b <= '9'; b++, m /= 10) {
188✔
1255
                                k = (usec_t) (*b - '0') * m;
140✔
1256
                                if (k >= USEC_INFINITY - usec)
140✔
1257
                                        return -ERANGE;
1258

1259
                                usec += k;
140✔
1260
                        }
1261

1262
                        /* Don't allow "0.-0", "3.+1", "3. 1", "3.sec" or "3.hoge" */
1263
                        if (b == e + 1)
48✔
1264
                                return -EINVAL;
1265
                }
1266
        }
1267

1268
        if (ret)
14,704✔
1269
                *ret = usec;
14,702✔
1270
        return 0;
1271
}
1272

1273
int parse_sec(const char *t, usec_t *ret) {
15,150✔
1274
        return parse_time(t, ret, USEC_PER_SEC);
15,150✔
1275
}
1276

1277
int parse_sec_fix_0(const char *t, usec_t *ret) {
3,311✔
1278
        usec_t k;
3,311✔
1279
        int r;
3,311✔
1280

1281
        assert(t);
3,311✔
1282
        assert(ret);
3,311✔
1283

1284
        r = parse_sec(t, &k);
3,311✔
1285
        if (r < 0)
3,311✔
1286
                return r;
3,311✔
1287

1288
        *ret = k == 0 ? USEC_INFINITY : k;
3,311✔
1289
        return r;
3,311✔
1290
}
1291

1292
int parse_sec_def_infinity(const char *t, usec_t *ret) {
7✔
1293
        assert(t);
7✔
1294
        assert(ret);
7✔
1295

1296
        t += strspn(t, WHITESPACE);
7✔
1297
        if (isempty(t)) {
7✔
1298
                *ret = USEC_INFINITY;
2✔
1299
                return 0;
2✔
1300
        }
1301
        return parse_sec(t, ret);
5✔
1302
}
1303

1304
static const char* extract_nsec_multiplier(const char *p, nsec_t *ret) {
44✔
1305
        static const struct {
44✔
1306
                const char *suffix;
1307
                nsec_t nsec;
1308
        } table[] = {
1309
                { "seconds", NSEC_PER_SEC    },
1310
                { "second",  NSEC_PER_SEC    },
1311
                { "sec",     NSEC_PER_SEC    },
1312
                { "s",       NSEC_PER_SEC    },
1313
                { "minutes", NSEC_PER_MINUTE },
1314
                { "minute",  NSEC_PER_MINUTE },
1315
                { "min",     NSEC_PER_MINUTE },
1316
                { "months",  NSEC_PER_MONTH  },
1317
                { "month",   NSEC_PER_MONTH  },
1318
                { "M",       NSEC_PER_MONTH  },
1319
                { "msec",    NSEC_PER_MSEC   },
1320
                { "ms",      NSEC_PER_MSEC   },
1321
                { "m",       NSEC_PER_MINUTE },
1322
                { "hours",   NSEC_PER_HOUR   },
1323
                { "hour",    NSEC_PER_HOUR   },
1324
                { "hr",      NSEC_PER_HOUR   },
1325
                { "h",       NSEC_PER_HOUR   },
1326
                { "days",    NSEC_PER_DAY    },
1327
                { "day",     NSEC_PER_DAY    },
1328
                { "d",       NSEC_PER_DAY    },
1329
                { "weeks",   NSEC_PER_WEEK   },
1330
                { "week",    NSEC_PER_WEEK   },
1331
                { "w",       NSEC_PER_WEEK   },
1332
                { "years",   NSEC_PER_YEAR   },
1333
                { "year",    NSEC_PER_YEAR   },
1334
                { "y",       NSEC_PER_YEAR   },
1335
                { "usec",    NSEC_PER_USEC   },
1336
                { "us",      NSEC_PER_USEC   },
1337
                { "μs",      NSEC_PER_USEC   }, /* U+03bc (aka GREEK LETTER MU) */
1338
                { "µs",      NSEC_PER_USEC   }, /* U+b5 (aka MICRO SIGN) */
1339
                { "nsec",    1ULL            },
1340
                { "ns",      1ULL            },
1341
                { "",        1ULL            }, /* default is nsec */
1342
        };
1343

1344
        assert(p);
44✔
1345
        assert(ret);
44✔
1346

1347
        FOREACH_ELEMENT(i, table) {
803✔
1348
                const char *e = startswith(p, i->suffix);
803✔
1349
                if (e) {
803✔
1350
                        *ret = i->nsec;
44✔
1351
                        return e;
44✔
1352
                }
1353
        }
1354

1355
        return p;
1356
}
1357

1358
int parse_nsec(const char *t, nsec_t *ret) {
48✔
1359
        const char *p, *s;
48✔
1360
        nsec_t nsec = 0;
48✔
1361
        bool something = false;
48✔
1362

1363
        assert(t);
48✔
1364
        assert(ret);
48✔
1365

1366
        p = t;
48✔
1367

1368
        p += strspn(p, WHITESPACE);
48✔
1369
        s = startswith(p, "infinity");
48✔
1370
        if (s) {
48✔
1371
                s += strspn(s, WHITESPACE);
4✔
1372
                if (*s != 0)
4✔
1373
                        return -EINVAL;
1374

1375
                *ret = NSEC_INFINITY;
2✔
1376
                return 0;
2✔
1377
        }
1378

1379
        for (;;) {
30✔
1380
                nsec_t multiplier = 1, k;
74✔
1381
                long long l;
74✔
1382
                char *e;
74✔
1383

1384
                p += strspn(p, WHITESPACE);
74✔
1385

1386
                if (*p == 0) {
74✔
1387
                        if (!something)
21✔
1388
                                return -EINVAL;
24✔
1389

1390
                        break;
20✔
1391
                }
1392

1393
                if (*p == '-') /* Don't allow "-0" */
53✔
1394
                        return -ERANGE;
1395

1396
                errno = 0;
48✔
1397
                l = strtoll(p, &e, 10);
48✔
1398
                if (errno > 0)
48✔
1399
                        return -errno;
×
1400
                if (l < 0)
48✔
1401
                        return -ERANGE;
1402

1403
                if (*e == '.') {
48✔
1404
                        p = e + 1;
31✔
1405
                        p += strspn(p, DIGITS);
31✔
1406
                } else if (e == p)
17✔
1407
                        return -EINVAL;
1408
                else
1409
                        p = e;
1410

1411
                s = extract_nsec_multiplier(p + strspn(p, WHITESPACE), &multiplier);
44✔
1412
                if (s == p && *s != '\0')
44✔
1413
                        /* Don't allow '12.34.56', but accept '12.34 .56' or '12.34s.56' */
1414
                        return -EINVAL;
1415

1416
                p = s;
36✔
1417

1418
                if ((nsec_t) l >= NSEC_INFINITY / multiplier)
36✔
1419
                        return -ERANGE;
1420

1421
                k = (nsec_t) l * multiplier;
35✔
1422
                if (k >= NSEC_INFINITY - nsec)
35✔
1423
                        return -ERANGE;
1424

1425
                nsec += k;
35✔
1426

1427
                something = true;
35✔
1428

1429
                if (*e == '.') {
35✔
1430
                        nsec_t m = multiplier / 10;
24✔
1431
                        const char *b;
24✔
1432

1433
                        for (b = e + 1; *b >= '0' && *b <= '9'; b++, m /= 10) {
56✔
1434
                                k = (nsec_t) (*b - '0') * m;
32✔
1435
                                if (k >= NSEC_INFINITY - nsec)
32✔
1436
                                        return -ERANGE;
1437

1438
                                nsec += k;
32✔
1439
                        }
1440

1441
                        /* Don't allow "0.-0", "3.+1", "3. 1", "3.sec" or "3.hoge" */
1442
                        if (b == e + 1)
24✔
1443
                                return -EINVAL;
1444
                }
1445
        }
1446

1447
        *ret = nsec;
20✔
1448

1449
        return 0;
20✔
1450
}
1451

1452
static int get_timezones_from_zone1970_tab(char ***ret) {
×
1453
        _cleanup_fclose_ FILE *f = NULL;
×
1454
        _cleanup_strv_free_ char **zones = NULL;
×
1455
        int r;
×
1456

1457
        assert(ret);
×
1458

1459
        f = fopen("/usr/share/zoneinfo/zone1970.tab", "re");
×
1460
        if (!f)
×
1461
                return -errno;
×
1462

1463
        for (;;) {
×
1464
                _cleanup_free_ char *line = NULL, *cc = NULL, *co = NULL, *tz = NULL;
×
1465

1466
                r = read_line(f, LONG_LINE_MAX, &line);
×
1467
                if (r < 0)
×
1468
                        return r;
1469
                if (r == 0)
×
1470
                        break;
1471

1472
                const char *p = line;
×
1473

1474
                /* Line format is:
1475
                 * 'country codes' 'coordinates' 'timezone' 'comments' */
1476
                r = extract_many_words(&p, NULL, 0, &cc, &co, &tz);
×
1477
                if (r < 0)
×
1478
                        continue;
×
1479

1480
                /* Lines that start with # are comments. */
1481
                if (*cc == '#')
×
1482
                        continue;
×
1483

1484
                if (!timezone_is_valid(tz, LOG_DEBUG))
×
1485
                        /* Don't list unusable timezones. */
1486
                        continue;
×
1487

1488
                r = strv_extend(&zones, tz);
×
1489
                if (r < 0)
×
1490
                        return r;
1491
        }
1492

1493
        *ret = TAKE_PTR(zones);
×
1494
        return 0;
×
1495
}
1496

1497
static int get_timezones_from_tzdata_zi(char ***ret) {
4✔
1498
        _cleanup_fclose_ FILE *f = NULL;
4✔
1499
        _cleanup_strv_free_ char **zones = NULL;
4✔
1500
        int r;
4✔
1501

1502
        assert(ret);
4✔
1503

1504
        f = fopen("/usr/share/zoneinfo/tzdata.zi", "re");
4✔
1505
        if (!f)
4✔
1506
                return -errno;
×
1507

1508
        for (;;) {
17,212✔
1509
                _cleanup_free_ char *line = NULL, *type = NULL, *f1 = NULL, *f2 = NULL;
17,208✔
1510

1511
                r = read_line(f, LONG_LINE_MAX, &line);
17,212✔
1512
                if (r < 0)
17,212✔
1513
                        return r;
1514
                if (r == 0)
17,212✔
1515
                        break;
1516

1517
                const char *p = line;
17,208✔
1518

1519
                /* The only lines we care about are Zone and Link lines.
1520
                 * Zone line format is:
1521
                 * 'Zone' 'timezone' ...
1522
                 * Link line format is:
1523
                 * 'Link' 'target' 'alias'
1524
                 * See 'man zic' for more detail. */
1525
                r = extract_many_words(&p, NULL, 0, &type, &f1, &f2);
17,208✔
1526
                if (r < 0)
17,208✔
1527
                        continue;
×
1528

1529
                char *tz;
17,208✔
1530
                if (IN_SET(*type, 'Z', 'z'))
17,208✔
1531
                        /* Zone lines have timezone in field 1. */
1532
                        tz = f1;
1,364✔
1533
                else if (IN_SET(*type, 'L', 'l'))
15,844✔
1534
                        /* Link lines have timezone in field 2. */
1535
                        tz = f2;
1,028✔
1536
                else
1537
                        /* Not a line we care about. */
1538
                        continue;
14,816✔
1539

1540
                if (!timezone_is_valid(tz, LOG_DEBUG))
2,392✔
1541
                        /* Don't list unusable timezones. */
1542
                        continue;
×
1543

1544
                r = strv_extend(&zones, tz);
2,392✔
1545
                if (r < 0)
2,392✔
1546
                        return r;
1547
        }
1548

1549
        *ret = TAKE_PTR(zones);
4✔
1550
        return 0;
4✔
1551
}
1552

1553
int get_timezones(char ***ret) {
4✔
1554
        _cleanup_strv_free_ char **zones = NULL;
4✔
1555
        int r;
4✔
1556

1557
        assert(ret);
4✔
1558

1559
        r = get_timezones_from_tzdata_zi(&zones);
4✔
1560
        if (r == -ENOENT) {
4✔
1561
                log_debug_errno(r, "Could not get timezone data from tzdata.zi, using zone1970.tab: %m");
×
1562
                r = get_timezones_from_zone1970_tab(&zones);
×
1563
                if (r == -ENOENT)
×
1564
                        log_debug_errno(r, "Could not get timezone data from zone1970.tab, using UTC: %m");
×
1565
        }
1566
        if (r < 0 && r != -ENOENT)
4✔
1567
                return r;
1568

1569
        /* Always include UTC */
1570
        r = strv_extend(&zones, "UTC");
4✔
1571
        if (r < 0)
4✔
1572
                return r;
1573

1574
        strv_sort_uniq(zones);
4✔
1575

1576
        *ret = TAKE_PTR(zones);
4✔
1577
        return 0;
4✔
1578
}
1579

1580
int verify_timezone(const char *name, int log_level) {
4,341✔
1581
        bool slash = false;
4,341✔
1582
        const char *p, *t;
4,341✔
1583
        _cleanup_close_ int fd = -EBADF;
4,341✔
1584
        char buf[4];
4,341✔
1585
        int r;
4,341✔
1586

1587
        if (isempty(name))
8,682✔
1588
                return -EINVAL;
1589

1590
        /* Always accept "UTC" as valid timezone, since it's the fallback, even if user has no timezones installed. */
1591
        if (streq(name, "UTC"))
4,339✔
1592
                return 0;
1593

1594
        if (name[0] == '/')
4,332✔
1595
                return -EINVAL;
1596

1597
        for (p = name; *p; p++) {
60,257✔
1598
                if (!ascii_isdigit(*p) &&
56,081✔
1599
                    !ascii_isalpha(*p) &&
54,769✔
1600
                    !IN_SET(*p, '-', '_', '+', '/'))
4,915✔
1601
                        return -EINVAL;
1602

1603
                if (*p == '/') {
55,926✔
1604

1605
                        if (slash)
3,828✔
1606
                                return -EINVAL;
1607

1608
                        slash = true;
1609
                } else
1610
                        slash = false;
1611
        }
1612

1613
        if (slash)
4,176✔
1614
                return -EINVAL;
1615

1616
        if (p - name >= PATH_MAX)
4,175✔
1617
                return -ENAMETOOLONG;
1618

1619
        t = strjoina("/usr/share/zoneinfo/", name);
20,875✔
1620

1621
        fd = open(t, O_RDONLY|O_CLOEXEC);
4,175✔
1622
        if (fd < 0)
4,175✔
1623
                return log_full_errno(log_level, errno, "Failed to open timezone file '%s': %m", t);
210✔
1624

1625
        r = fd_verify_regular(fd);
3,965✔
1626
        if (r < 0)
3,965✔
1627
                return log_full_errno(log_level, r, "Timezone file '%s' is not a regular file: %m", t);
×
1628

1629
        r = loop_read_exact(fd, buf, 4, false);
3,965✔
1630
        if (r < 0)
3,965✔
1631
                return log_full_errno(log_level, r, "Failed to read from timezone file '%s': %m", t);
×
1632

1633
        /* Magic from tzfile(5) */
1634
        if (memcmp(buf, "TZif", 4) != 0)
3,965✔
1635
                return log_full_errno(log_level, SYNTHETIC_ERRNO(EBADMSG),
×
1636
                                      "Timezone file '%s' has wrong magic bytes", t);
1637

1638
        return 0;
1639
}
1640

1641
void reset_timezonep(char **p) {
758✔
1642
        assert(p);
758✔
1643

1644
        (void) set_unset_env("TZ", *p, /* overwrite= */ true);
758✔
1645
        tzset();
758✔
1646
        *p = mfree(*p);
758✔
1647
}
758✔
1648

1649
char* save_timezone(void) {
758✔
1650
        const char *e = getenv("TZ");
758✔
1651
        if (!e)
758✔
1652
                return NULL;
1653

1654
        char *s = strdup(e);
57✔
1655
        if (!s)
57✔
1656
                log_debug("Failed to save $TZ=%s, unsetting the environment variable.", e);
×
1657

1658
        return s;
1659
}
1660

1661
bool clock_supported(clockid_t clock) {
273,376✔
1662
        struct timespec ts;
273,376✔
1663

1664
        switch (clock) {
273,376✔
1665

1666
        case CLOCK_MONOTONIC:
1667
        case CLOCK_REALTIME:
1668
        case CLOCK_BOOTTIME:
1669
                /* These three are always available in our baseline, and work in timerfd, as of kernel 3.15 */
1670
                return true;
1671

1672
        default:
1✔
1673
                /* For everything else, check properly */
1674
                return clock_gettime(clock, &ts) >= 0;
1✔
1675
        }
1676
}
1677

1678
int get_timezone(char **ret) {
65✔
1679
        _cleanup_free_ char *t = NULL;
65✔
1680
        int r;
65✔
1681

1682
        assert(ret);
65✔
1683

1684
        r = readlink_malloc(etc_localtime(), &t);
65✔
1685
        if (r == -ENOENT)
65✔
1686
                /* If the symlink does not exist, assume "UTC", like glibc does */
1687
                return strdup_to(ret, "UTC");
1✔
1688
        if (r < 0)
64✔
1689
                return r; /* Return EINVAL if not a symlink */
1690

1691
        const char *e = PATH_STARTSWITH_SET(t, "/usr/share/zoneinfo/", "../usr/share/zoneinfo/");
64✔
1692
        if (!e)
64✔
1693
                return -EINVAL;
1694
        if (!timezone_is_valid(e, LOG_DEBUG))
64✔
1695
                return -EINVAL;
1696

1697
        return strdup_to(ret, e);
64✔
1698
}
1699

UNCOV
1700
int get_timezone_prefer_env(char **ret) {
×
UNCOV
1701
        assert(ret);
×
1702

UNCOV
1703
        const char *e = getenv("TZ");
×
UNCOV
1704
        if (e && e[0] == ':' && timezone_is_valid(e + 1, LOG_DEBUG))
×
UNCOV
1705
                return strdup_to(ret, e + 1);
×
1706

UNCOV
1707
        return get_timezone(ret);
×
1708
}
1709

1710
const char* etc_localtime(void) {
1,018✔
1711
        static const char *cached = NULL;
1,018✔
1712

1713
        if (!cached)
1,018✔
1714
                cached = secure_getenv("SYSTEMD_ETC_LOCALTIME") ?: "/etc/localtime";
914✔
1715

1716
        return cached;
1,018✔
1717
}
1718

1719
int mktime_or_timegm_usec(
9,313✔
1720
                struct tm *tm, /* input + normalized output */
1721
                bool utc,
1722
                usec_t *ret) {
1723

1724
        time_t t;
9,313✔
1725

1726
        assert(tm);
9,313✔
1727

1728
        if (tm->tm_year < 69) /* early check for negative (i.e. before 1970) time_t (Note that in some timezones the epoch is in the year 1969!) */
9,313✔
1729
                return -ERANGE;
1730
        if ((usec_t) tm->tm_year > CONST_MIN(USEC_INFINITY / USEC_PER_YEAR, (usec_t) TIME_T_MAX / (365U * 24U * 60U * 60U)) - 1900) /* early check for possible overrun of usec_t or time_t */
9,310✔
1731
                return -ERANGE;
1732

1733
        /* timegm()/mktime() is a bit weird to use, since it returns -1 in two cases: on error as well as a
1734
         * valid time indicating one second before the UNIX epoch. Let's treat both cases the same here, and
1735
         * return -ERANGE for anything negative, since usec_t is unsigned, and we can thus not express
1736
         * negative times anyway. */
1737

1738
        t = utc ? timegm(tm) : mktime(tm);
9,310✔
1739
        if (t < 0) /* Refuse negative times and errors */
9,310✔
1740
                return -ERANGE;
1741
        if ((usec_t) t >= USEC_INFINITY / USEC_PER_SEC) /* Never return USEC_INFINITY by accident (or overflow) */
9,308✔
1742
                return -ERANGE;
1743

1744
        if (ret)
9,308✔
1745
                *ret = (usec_t) t * USEC_PER_SEC;
2,387✔
1746
        return 0;
1747
}
1748

1749
int localtime_or_gmtime_usec(
180,590✔
1750
                usec_t t,
1751
                bool utc,
1752
                struct tm *ret) {
1753

1754
        t /= USEC_PER_SEC; /* Round down */
180,590✔
1755
        if (t > (usec_t) TIME_T_MAX)
180,590✔
1756
                return -ERANGE;
1757
        time_t sec = (time_t) t;
180,590✔
1758

1759
        struct tm buf = {};
180,590✔
1760
        if (!(utc ? gmtime_r(&sec, &buf) : localtime_r(&sec, &buf)))
180,590✔
1761
                return -EINVAL;
1762

1763
        if (ret)
180,590✔
1764
                *ret = buf;
180,590✔
1765

1766
        return 0;
1767
}
1768

1769
static uint32_t sysconf_clock_ticks_cached(void) {
1,911✔
1770
        static thread_local uint32_t hz = 0;
1,911✔
1771
        long r;
1,911✔
1772

1773
        if (hz == 0) {
1,911✔
1774
                r = sysconf(_SC_CLK_TCK);
78✔
1775

1776
                assert(r > 0);
78✔
1777
                hz = r;
78✔
1778
        }
1779

1780
        return hz;
1,911✔
1781
}
1782

1783
uint32_t usec_to_jiffies(usec_t u) {
9✔
1784
        uint32_t hz = sysconf_clock_ticks_cached();
9✔
1785
        return DIV_ROUND_UP(u, USEC_PER_SEC / hz);
9✔
1786
}
1787

1788
usec_t jiffies_to_usec(uint32_t j) {
1,902✔
1789
        uint32_t hz = sysconf_clock_ticks_cached();
1,902✔
1790
        return DIV_ROUND_UP(j * USEC_PER_SEC, hz);
1,902✔
1791
}
1792

1793
usec_t usec_shift_clock(usec_t x, clockid_t from, clockid_t to) {
870✔
1794
        usec_t a, b;
870✔
1795

1796
        if (x == USEC_INFINITY)
870✔
1797
                return USEC_INFINITY;
1798
        if (map_clock_id(from) == map_clock_id(to))
697✔
1799
                return x;
1800

1801
        a = now(from);
8✔
1802
        b = now(to);
8✔
1803

1804
        if (x > a)
8✔
1805
                /* x lies in the future */
1806
                return usec_add(b, usec_sub_unsigned(x, a));
6✔
1807
        else
1808
                /* x lies in the past */
1809
                return usec_sub_unsigned(b, usec_sub_unsigned(a, x));
4✔
1810
}
1811

1812
bool in_utc_timezone(void) {
676✔
1813
        tzset();
676✔
1814

1815
        return timezone == 0 && daylight == 0;
676✔
1816
}
1817

1818
int usleep_safe(usec_t usec) {
344✔
1819
        int r;
344✔
1820

1821
        /* usleep() takes useconds_t that is (typically?) uint32_t. Also, usleep() may only support the
1822
         * range [0, 1000000]. See usleep(3). Let's override usleep() with clock_nanosleep().
1823
         *
1824
         * ⚠️ Note we are not using plain nanosleep() here, since that operates on CLOCK_REALTIME, not
1825
         *    CLOCK_MONOTONIC! */
1826

1827
        if (usec == 0)
344✔
1828
                return 0;
344✔
1829

1830
        if (usec == USEC_INFINITY)
344✔
UNCOV
1831
                return RET_NERRNO(pause());
×
1832

1833
        struct timespec t;
344✔
1834
        timespec_store(&t, usec);
344✔
1835

1836
        for (;;) {
344✔
1837
                struct timespec remaining;
344✔
1838

1839
                /* `clock_nanosleep()` does not use `errno`, but returns positive error codes. */
1840
                r = -clock_nanosleep(CLOCK_MONOTONIC, /* flags= */ 0, &t, &remaining);
344✔
1841
                if (r == -EINTR) {
344✔
1842
                        /* Interrupted. Continue sleeping for the remaining time. */
UNCOV
1843
                        t = remaining;
×
UNCOV
1844
                        continue;
×
1845
                }
1846

1847
                return r;
344✔
1848
        }
1849
}
1850

1851
int time_change_fd(void) {
466✔
1852

1853
        /* We only care for the cancellation event, hence we set the timeout to the latest possible value. */
1854
        static const struct itimerspec its = {
466✔
1855
                .it_value.tv_sec = TIME_T_MAX,
1856
        };
1857

1858
        _cleanup_close_ int fd = -EBADF;
932✔
1859

1860
        /* Uses TFD_TIMER_CANCEL_ON_SET to get notifications whenever CLOCK_REALTIME makes a jump relative to
1861
         * CLOCK_MONOTONIC. */
1862

1863
        fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
466✔
1864
        if (fd < 0)
466✔
UNCOV
1865
                return -errno;
×
1866

1867
        if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) >= 0)
466✔
1868
                return TAKE_FD(fd);
466✔
1869

1870
        /* So apparently there are systems where time_t is 64-bit, but the kernel actually doesn't support
1871
         * 64-bit time_t. In that case configuring a timer to TIME_T_MAX will fail with EOPNOTSUPP or a
1872
         * similar error. If that's the case let's try with INT32_MAX instead, maybe that works. It's a bit
1873
         * of a black magic thing though, but what can we do?
1874
         *
1875
         * We don't want this code on x86-64, hence let's conditionalize this for systems with 64-bit time_t
1876
         * but where "long" is shorter than 64-bit, i.e. 32-bit archs.
1877
         *
1878
         * See: https://github.com/systemd/systemd/issues/14362 */
1879

1880
#if SIZEOF_TIME_T == 8 && ULONG_MAX < UINT64_MAX
1881
        if (ERRNO_IS_NOT_SUPPORTED(errno) || errno == EOVERFLOW) {
1882
                static const struct itimerspec its32 = {
1883
                        .it_value.tv_sec = INT32_MAX,
1884
                };
1885

1886
                if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its32, NULL) >= 0)
1887
                        return TAKE_FD(fd);
1888
        }
1889
#endif
1890

UNCOV
1891
        return -errno;
×
1892
}
1893

1894
static const char* const timestamp_style_table[_TIMESTAMP_STYLE_MAX] = {
1895
        [TIMESTAMP_PRETTY] = "pretty",
1896
        [TIMESTAMP_US]     = "us",
1897
        [TIMESTAMP_UTC]    = "utc",
1898
        [TIMESTAMP_US_UTC] = "us+utc",
1899
        [TIMESTAMP_UNIX]   = "unix",
1900
};
1901

1902
/* Use the macro for enum → string to allow for aliases */
UNCOV
1903
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(timestamp_style, TimestampStyle);
×
1904

1905
/* For the string → enum mapping we use the generic implementation, but also support two aliases */
1906
TimestampStyle timestamp_style_from_string(const char *s) {
8✔
1907
        TimestampStyle t;
8✔
1908

1909
        t = (TimestampStyle) string_table_lookup_from_string(timestamp_style_table, ELEMENTSOF(timestamp_style_table), s);
8✔
1910
        if (t >= 0)
8✔
1911
                return t;
1912
        if (STRPTR_IN_SET(s, "µs", "μs")) /* accept both µ symbols in unicode, i.e. micro symbol + Greek small letter mu. */
2✔
1913
                return TIMESTAMP_US;
1✔
1914
        if (STRPTR_IN_SET(s, "µs+utc", "μs+utc"))
1✔
1915
                return TIMESTAMP_US_UTC;
1✔
1916
        return t;
1917
}
1918

1919
int parse_calendar_date_full(const char *s, bool allow_pre_epoch, usec_t *ret_usec, struct tm *ret_tm) {
64✔
1920
        struct tm parsed_tm = {}, copy_tm;
64✔
1921
        const char *k;
64✔
1922
        int r;
64✔
1923

1924
        assert(s);
64✔
1925

1926
        k = strptime(s, "%Y-%m-%d", &parsed_tm);
64✔
1927
        if (!k || *k)
64✔
1928
                return -EINVAL;
64✔
1929

1930
        copy_tm = parsed_tm;
54✔
1931

1932
        usec_t usec = USEC_INFINITY;
54✔
1933

1934
        if (allow_pre_epoch) {
54✔
1935
                /* For birth dates we use timegm() directly since we need to accept pre-epoch dates.
1936
                 * timegm() returns (time_t) -1 both on error and for one second before the epoch.
1937
                 * Initialize wday to -1 beforehand: if it remains -1 after the call, it's a genuine
1938
                 * error; if timegm() changed it, the date was successfully normalized. */
1939
                copy_tm.tm_wday = -1;
8✔
1940
                if (timegm(&copy_tm) == (time_t) -1 && copy_tm.tm_wday == -1)
8✔
1941
                        return -EINVAL;
1942
        } else {
1943
                r = mktime_or_timegm_usec(&copy_tm, /* utc= */ true, &usec);
46✔
1944
                if (r < 0)
46✔
1945
                        return r;
1946
        }
1947

1948
        /* Refuse non-normalized dates, e.g. Feb 30 */
1949
        if (copy_tm.tm_mday != parsed_tm.tm_mday ||
54✔
1950
            copy_tm.tm_mon  != parsed_tm.tm_mon  ||
50✔
1951
            copy_tm.tm_year != parsed_tm.tm_year)
50✔
1952
                return -EINVAL;
1953

1954
        if (ret_usec)
50✔
1955
                *ret_usec = usec;
43✔
1956
        if (ret_tm) {
50✔
1957
                /* Reset to unset, then fill in only the date fields we parsed and validated */
1958
                *ret_tm = BIRTH_DATE_UNSET;
5✔
1959
                ret_tm->tm_mday = parsed_tm.tm_mday;
5✔
1960
                ret_tm->tm_mon = parsed_tm.tm_mon;
5✔
1961
                ret_tm->tm_year = parsed_tm.tm_year;
5✔
1962
        }
1963

1964
        return 0;
1965
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc