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

systemd / systemd / 19315930715

12 Nov 2025 11:39PM UTC coverage: 72.251% (-0.2%) from 72.412%
19315930715

push

github

bluca
mkosi: update debian commit reference to efdd7a637

* efdd7a6377 Install new file for upstream build
* 9ebdc6099e d/rules: enable 10-systemd-logind-root-ignore-inhibitors.rules.example on Ubuntu
* 1255cc7663 initramfs-tools: only skip chzdev rules if zdev_early=0
* 4675b281ee d/t/boot-and-services: skip apparmor test on armhf
* 214d6e37b2 d/t/boot-and-services: run transient unit to check syslog messages
* f4e196aa26 d/t/boot-and-services: tweak test_rsyslog regex
* dbd366a43e Install new files for upstream build
* bb7f8ef532 Install new files for upstream build
* efa7cee8a7 Install new file for upstream build
* 95aa1d1685 Install new file for upstream build
* b770f0f01b kernel-install: skip 55-initrd.install when an initrd generator is configured
* af8d1e3134 Update changelog for 258.1-2 release
* 2d0e73cd14 d/libnss-systemd.postinst: Ensure module is enabled for all four databases

306471 of 424176 relevant lines covered (72.25%)

1239443.53 hits per line

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

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

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

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

29
static clockid_t map_clock_id(clockid_t c) {
29,024,757✔
30

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

36
        switch (c) {
29,024,757✔
37

38
        case CLOCK_BOOTTIME_ALARM:
39
                return CLOCK_BOOTTIME;
40

41
        case CLOCK_REALTIME_ALARM:
8✔
42
                return CLOCK_REALTIME;
8✔
43

44
        default:
29,024,748✔
45
                return c;
29,024,748✔
46
        }
47
}
48

49
usec_t now(clockid_t clock_id) {
29,022,209✔
50
        struct timespec ts;
29,022,209✔
51

52
        assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
29,022,209✔
53

54
        return timespec_load(&ts);
29,022,209✔
55
}
56

57
nsec_t now_nsec(clockid_t clock_id) {
620✔
58
        struct timespec ts;
620✔
59

60
        assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
620✔
61

62
        return timespec_load_nsec(&ts);
620✔
63
}
64

65
dual_timestamp* dual_timestamp_now(dual_timestamp *ts) {
1,917,080✔
66
        assert(ts);
1,917,080✔
67

68
        ts->realtime = now(CLOCK_REALTIME);
1,917,080✔
69
        ts->monotonic = now(CLOCK_MONOTONIC);
1,917,080✔
70

71
        return ts;
1,917,080✔
72
}
73

74
triple_timestamp* triple_timestamp_now(triple_timestamp *ts) {
3,277,735✔
75
        assert(ts);
3,277,735✔
76

77
        ts->realtime = now(CLOCK_REALTIME);
3,277,735✔
78
        ts->monotonic = now(CLOCK_MONOTONIC);
3,277,735✔
79
        ts->boottime = now(CLOCK_BOOTTIME);
3,277,735✔
80

81
        return ts;
3,277,735✔
82
}
83

84
usec_t map_clock_usec_raw(usec_t from, usec_t from_base, usec_t to_base) {
8,360✔
85

86
        /* Maps the time 'from' between two clocks, based on a common reference point where the first clock
87
         * is at 'from_base' and the second clock at 'to_base'. Basically calculates:
88
         *
89
         *         from - from_base + to_base
90
         *
91
         * But takes care of overflows/underflows and avoids signed operations. */
92

93
        if (from >= from_base) { /* In the future */
8,360✔
94
                usec_t delta = from - from_base;
28✔
95

96
                if (to_base >= USEC_INFINITY - delta) /* overflow? */
28✔
97
                        return USEC_INFINITY;
98

99
                return to_base + delta;
28✔
100

101
        } else { /* In the past */
102
                usec_t delta = from_base - from;
8,332✔
103

104
                if (to_base <= delta) /* underflow? */
8,332✔
105
                        return 0;
106

107
                return to_base - delta;
8,323✔
108
        }
109
}
110

111
usec_t map_clock_usec(usec_t from, clockid_t from_clock, clockid_t to_clock) {
452✔
112

113
        /* Try to avoid any inaccuracy needlessly added in case we convert from effectively the same clock
114
         * onto itself */
115
        if (map_clock_id(from_clock) == map_clock_id(to_clock))
452✔
116
                return from;
117

118
        /* Keep infinity as is */
119
        if (from == USEC_INFINITY)
452✔
120
                return from;
121

122
        return map_clock_usec_raw(from, now(from_clock), now(to_clock));
418✔
123
}
124

125
dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) {
12✔
126
        assert(ts);
12✔
127

128
        if (!timestamp_is_set(u)) {
12✔
129
                ts->realtime = ts->monotonic = u;
×
130
                return ts;
×
131
        }
132

133
        ts->realtime = u;
12✔
134
        ts->monotonic = map_clock_usec(u, CLOCK_REALTIME, CLOCK_MONOTONIC);
12✔
135
        return ts;
12✔
136
}
137

138
triple_timestamp* triple_timestamp_from_realtime(triple_timestamp *ts, usec_t u) {
338✔
139
        usec_t nowr;
338✔
140

141
        assert(ts);
338✔
142

143
        if (!timestamp_is_set(u)) {
338✔
144
                ts->realtime = ts->monotonic = ts->boottime = u;
×
145
                return ts;
×
146
        }
147

148
        nowr = now(CLOCK_REALTIME);
338✔
149

150
        ts->realtime = u;
338✔
151
        ts->monotonic = map_clock_usec_raw(u, nowr, now(CLOCK_MONOTONIC));
338✔
152
        ts->boottime = map_clock_usec_raw(u, nowr, now(CLOCK_BOOTTIME));
338✔
153

154
        return ts;
338✔
155
}
156

157
triple_timestamp* triple_timestamp_from_boottime(triple_timestamp *ts, usec_t u) {
×
158
        usec_t nowb;
×
159

160
        assert(ts);
×
161

162
        if (u == USEC_INFINITY) {
×
163
                ts->realtime = ts->monotonic = ts->boottime = u;
×
164
                return ts;
×
165
        }
166

167
        nowb = now(CLOCK_BOOTTIME);
×
168

169
        ts->boottime = u;
×
170
        ts->monotonic = map_clock_usec_raw(u, nowb, now(CLOCK_MONOTONIC));
×
171
        ts->realtime = map_clock_usec_raw(u, nowb, now(CLOCK_REALTIME));
×
172

173
        return ts;
×
174
}
175

176
dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) {
240✔
177
        assert(ts);
240✔
178

179
        if (u == USEC_INFINITY) {
240✔
180
                ts->realtime = ts->monotonic = USEC_INFINITY;
×
181
                return ts;
×
182
        }
183

184
        ts->monotonic = u;
240✔
185
        ts->realtime = map_clock_usec(u, CLOCK_MONOTONIC, CLOCK_REALTIME);
240✔
186
        return ts;
240✔
187
}
188

189
dual_timestamp* dual_timestamp_from_boottime(dual_timestamp *ts, usec_t u) {
506✔
190
        usec_t nowm;
506✔
191

192
        assert(ts);
506✔
193

194
        if (u == USEC_INFINITY) {
506✔
195
                ts->realtime = ts->monotonic = USEC_INFINITY;
×
196
                return ts;
×
197
        }
198

199
        nowm = now(CLOCK_BOOTTIME);
506✔
200
        ts->monotonic = map_clock_usec_raw(u, nowm, now(CLOCK_MONOTONIC));
506✔
201
        ts->realtime = map_clock_usec_raw(u, nowm, now(CLOCK_REALTIME));
506✔
202
        return ts;
506✔
203
}
204

205
usec_t triple_timestamp_by_clock(triple_timestamp *ts, clockid_t clock) {
3,425,804✔
206
        assert(ts);
3,425,804✔
207

208
        switch (clock) {
3,425,804✔
209

210
        case CLOCK_REALTIME:
1,878,389✔
211
        case CLOCK_REALTIME_ALARM:
212
                return ts->realtime;
1,878,389✔
213

214
        case CLOCK_MONOTONIC:
1,510,703✔
215
                return ts->monotonic;
1,510,703✔
216

217
        case CLOCK_BOOTTIME:
36,712✔
218
        case CLOCK_BOOTTIME_ALARM:
219
                return ts->boottime;
36,712✔
220

221
        default:
222
                return USEC_INFINITY;
223
        }
224
}
225

226
usec_t timespec_load(const struct timespec *ts) {
29,379,130✔
227
        assert(ts);
29,379,130✔
228

229
        if (ts->tv_sec < 0 || ts->tv_nsec < 0)
29,379,130✔
230
                return USEC_INFINITY;
231

232
        if ((usec_t) ts->tv_sec > (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC)
29,379,130✔
233
                return USEC_INFINITY;
234

235
        return
29,379,130✔
236
                (usec_t) ts->tv_sec * USEC_PER_SEC +
29,379,130✔
237
                (usec_t) ts->tv_nsec / NSEC_PER_USEC;
238
}
239

240
nsec_t timespec_load_nsec(const struct timespec *ts) {
1,444✔
241
        assert(ts);
1,444✔
242

243
        if (ts->tv_sec < 0 || ts->tv_nsec < 0)
1,444✔
244
                return NSEC_INFINITY;
245

246
        if ((nsec_t) ts->tv_sec >= (UINT64_MAX - ts->tv_nsec) / NSEC_PER_SEC)
1,444✔
247
                return NSEC_INFINITY;
248

249
        return (nsec_t) ts->tv_sec * NSEC_PER_SEC + (nsec_t) ts->tv_nsec;
1,444✔
250
}
251

252
struct timespec *timespec_store(struct timespec *ts, usec_t u) {
2,225,810✔
253
        assert(ts);
2,225,810✔
254

255
        if (u == USEC_INFINITY ||
2,225,810✔
256
            u / USEC_PER_SEC >= TIME_T_MAX) {
257
                ts->tv_sec = (time_t) -1;
×
258
                ts->tv_nsec = -1L;
×
259
                return ts;
×
260
        }
261

262
        ts->tv_sec = (time_t) (u / USEC_PER_SEC);
2,225,810✔
263
        ts->tv_nsec = (long) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
2,225,810✔
264

265
        return ts;
2,225,810✔
266
}
267

268
struct timespec *timespec_store_nsec(struct timespec *ts, nsec_t n) {
56✔
269
        assert(ts);
56✔
270

271
        if (n == NSEC_INFINITY ||
56✔
272
            n / NSEC_PER_SEC >= TIME_T_MAX) {
273
                ts->tv_sec = (time_t) -1;
×
274
                ts->tv_nsec = -1L;
×
275
                return ts;
×
276
        }
277

278
        ts->tv_sec = (time_t) (n / NSEC_PER_SEC);
56✔
279
        ts->tv_nsec = (long) (n % NSEC_PER_SEC);
56✔
280

281
        return ts;
56✔
282
}
283

284
usec_t timeval_load(const struct timeval *tv) {
771,042✔
285
        assert(tv);
771,042✔
286

287
        if (tv->tv_sec < 0 || tv->tv_usec < 0)
771,042✔
288
                return USEC_INFINITY;
289

290
        if ((usec_t) tv->tv_sec > (UINT64_MAX - tv->tv_usec) / USEC_PER_SEC)
771,042✔
291
                return USEC_INFINITY;
292

293
        return
771,042✔
294
                (usec_t) tv->tv_sec * USEC_PER_SEC +
771,042✔
295
                (usec_t) tv->tv_usec;
296
}
297

298
struct timeval *timeval_store(struct timeval *tv, usec_t u) {
142,504✔
299
        assert(tv);
142,504✔
300

301
        if (u == USEC_INFINITY ||
142,504✔
302
            u / USEC_PER_SEC > TIME_T_MAX) {
303
                tv->tv_sec = (time_t) -1;
×
304
                tv->tv_usec = (suseconds_t) -1;
×
305
        } else {
306
                tv->tv_sec = (time_t) (u / USEC_PER_SEC);
142,504✔
307
                tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
142,504✔
308
        }
309

310
        return tv;
142,504✔
311
}
312

313
char* format_timestamp_style(
5,884✔
314
                char *buf,
315
                size_t l,
316
                usec_t t,
317
                TimestampStyle style) {
318

319
        /* The weekdays in non-localized (English) form. We use this instead of the localized form, so that
320
         * our generated timestamps may be parsed with parse_timestamp(), and always read the same. */
321
        static const char * const weekdays[] = {
5,884✔
322
                [0] = "Sun",
323
                [1] = "Mon",
324
                [2] = "Tue",
325
                [3] = "Wed",
326
                [4] = "Thu",
327
                [5] = "Fri",
328
                [6] = "Sat",
329
        };
330

331
        struct tm tm;
5,884✔
332
        bool utc, us;
5,884✔
333
        size_t n;
5,884✔
334

335
        assert(buf);
5,884✔
336
        assert(style >= 0);
5,884✔
337
        assert(style < _TIMESTAMP_STYLE_MAX);
5,884✔
338

339
        if (!timestamp_is_set(t))
5,884✔
340
                return NULL; /* Timestamp is unset */
5,884✔
341

342
        if (style == TIMESTAMP_UNIX) {
4,284✔
343
                if (l < (size_t) (1 + 1 + 1))
102✔
344
                        return NULL; /* not enough space for even the shortest of forms */
345

346
                return snprintf_ok(buf, l, "@" USEC_FMT, t / USEC_PER_SEC);  /* round down μs → s */
102✔
347
        }
348

349
        utc = IN_SET(style, TIMESTAMP_UTC, TIMESTAMP_US_UTC, TIMESTAMP_DATE);
4,182✔
350
        us = IN_SET(style, TIMESTAMP_US, TIMESTAMP_US_UTC);
4,182✔
351

352
        if (l < (size_t) (3 +                   /* week day */
3,714✔
353
                          1 + 10 +              /* space and date */
354
                          style == TIMESTAMP_DATE ? 0 :
355
                          (1 + 8 +              /* space and time */
356
                           (us ? 1 + 6 : 0) +   /* "." and microsecond part */
4,182✔
357
                           1 + (utc ? 3 : 1)) + /* space and shortest possible zone */
8,027✔
358
                          1))
359
                return NULL; /* Not enough space even for the shortest form. */
360

361
        /* Let's not format times with years > 9999 */
362
        if (t > USEC_TIMESTAMP_FORMATTABLE_MAX) {
4,182✔
363
                static const char* const xxx[_TIMESTAMP_STYLE_MAX] = {
4✔
364
                        [TIMESTAMP_PRETTY] = "--- XXXX-XX-XX XX:XX:XX",
365
                        [TIMESTAMP_US]     = "--- XXXX-XX-XX XX:XX:XX.XXXXXX",
366
                        [TIMESTAMP_UTC]    = "--- XXXX-XX-XX XX:XX:XX UTC",
367
                        [TIMESTAMP_US_UTC] = "--- XXXX-XX-XX XX:XX:XX.XXXXXX UTC",
368
                        [TIMESTAMP_DATE]   = "--- XXXX-XX-XX",
369
                };
370

371
                assert(l >= strlen(xxx[style]) + 1);
4✔
372
                return strcpy(buf, xxx[style]);
4✔
373
        }
374

375
        if (localtime_or_gmtime_usec(t, utc, &tm) < 0)
4,178✔
376
                return NULL;
377

378
        /* Start with the week day */
379
        assert((size_t) tm.tm_wday < ELEMENTSOF(weekdays));
4,178✔
380
        memcpy(buf, weekdays[tm.tm_wday], 4);
4,178✔
381

382
        if (style == TIMESTAMP_DATE) {
4,178✔
383
                /* Special format string if only date should be shown. */
384
                if (strftime(buf + 3, l - 3, " %Y-%m-%d", &tm) <= 0)
104✔
385
                        return NULL; /* Doesn't fit */
386

387
                return buf;
104✔
388
        }
389

390
        /* Add the main components */
391
        if (strftime(buf + 3, l - 3, " %Y-%m-%d %H:%M:%S", &tm) <= 0)
4,074✔
392
                return NULL; /* Doesn't fit */
393

394
        /* Append the microseconds part, if that's requested */
395
        if (us) {
4,074✔
396
                n = strlen(buf);
467✔
397
                if (n + 8 > l)
467✔
398
                        return NULL; /* Microseconds part doesn't fit. */
399

400
                sprintf(buf + n, ".%06"PRI_USEC, t % USEC_PER_SEC);
467✔
401
        }
402

403
        /* Append the timezone */
404
        n = strlen(buf);
4,074✔
405
        if (utc) {
4,074✔
406
                /* If this is UTC then let's explicitly use the "UTC" string here, because gmtime_r()
407
                 * normally uses the obsolete "GMT" instead. */
408
                if (n + 5 > l)
230✔
409
                        return NULL; /* "UTC" doesn't fit. */
410

411
                strcpy(buf + n, " UTC");
230✔
412

413
        } else if (!isempty(tm.tm_zone)) {
3,844✔
414
                size_t tn;
3,844✔
415

416
                /* An explicit timezone is specified, let's use it, if it fits */
417
                tn = strlen(tm.tm_zone);
3,844✔
418
                if (n + 1 + tn + 1 > l) {
3,844✔
419
                        /* The full time zone does not fit in. Yuck. */
420

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

425
                        /* So the time zone doesn't fit in fully, but the caller passed enough space for the
426
                         * POSIX minimum time zone length. In this case suppress the timezone entirely, in
427
                         * order not to dump an overly long, hard to read string on the user. This should be
428
                         * safe, because the user will assume the local timezone anyway if none is shown. And
429
                         * so does parse_timestamp(). */
430
                } else {
431
                        buf[n++] = ' ';
3,844✔
432
                        strcpy(buf + n, tm.tm_zone);
3,844✔
433
                }
434
        }
435

436
        return buf;
437
}
438

439
char* format_timestamp_relative_full(char *buf, size_t l, usec_t t, clockid_t clock, bool implicit_left) {
1,413✔
440
        const char *s;
1,413✔
441
        usec_t n, d;
1,413✔
442

443
        assert(buf);
1,413✔
444

445
        if (!timestamp_is_set(t))
1,413✔
446
                return NULL;
447

448
        n = now(clock);
1,407✔
449
        if (n > t) {
1,407✔
450
                d = n - t;
1,212✔
451
                s = " ago";
1,212✔
452
        } else {
453
                d = t - n;
195✔
454
                s = implicit_left ? "" : " left";
195✔
455
        }
456

457
        if (d >= USEC_PER_YEAR) {
1,407✔
458
                usec_t years = d / USEC_PER_YEAR;
742✔
459
                usec_t months = (d % USEC_PER_YEAR) / USEC_PER_MONTH;
742✔
460

461
                (void) snprintf(buf, l, USEC_FMT " %s " USEC_FMT " %s%s",
2,194✔
462
                                years,
463
                                years == 1 ? "year" : "years",
464
                                months,
465
                                months == 1 ? "month" : "months",
466
                                s);
467
        } else if (d >= USEC_PER_MONTH) {
665✔
468
                usec_t months = d / USEC_PER_MONTH;
13✔
469
                usec_t days = (d % USEC_PER_MONTH) / USEC_PER_DAY;
13✔
470

471
                (void) snprintf(buf, l, USEC_FMT " %s " USEC_FMT " %s%s",
31✔
472
                                months,
473
                                months == 1 ? "month" : "months",
474
                                days,
475
                                days == 1 ? "day" : "days",
476
                                s);
477
        } else if (d >= USEC_PER_WEEK) {
652✔
478
                usec_t weeks = d / USEC_PER_WEEK;
8✔
479
                usec_t days = (d % USEC_PER_WEEK) / USEC_PER_DAY;
8✔
480

481
                (void) snprintf(buf, l, USEC_FMT " %s " USEC_FMT " %s%s",
16✔
482
                                weeks,
483
                                weeks == 1 ? "week" : "weeks",
484
                                days,
485
                                days == 1 ? "day" : "days",
486
                                s);
487
        } else if (d >= 2*USEC_PER_DAY)
644✔
488
                (void) snprintf(buf, l, USEC_FMT " days%s", d / USEC_PER_DAY,s);
×
489
        else if (d >= 25*USEC_PER_HOUR)
644✔
490
                (void) snprintf(buf, l, "1 day " USEC_FMT "h%s",
8✔
491
                                (d - USEC_PER_DAY) / USEC_PER_HOUR, s);
8✔
492
        else if (d >= 6*USEC_PER_HOUR)
636✔
493
                (void) snprintf(buf, l, USEC_FMT "h%s",
25✔
494
                                d / USEC_PER_HOUR, s);
495
        else if (d >= USEC_PER_HOUR)
611✔
496
                (void) snprintf(buf, l, USEC_FMT "h " USEC_FMT "min%s",
4✔
497
                                d / USEC_PER_HOUR,
498
                                (d % USEC_PER_HOUR) / USEC_PER_MINUTE, s);
4✔
499
        else if (d >= 5*USEC_PER_MINUTE)
607✔
500
                (void) snprintf(buf, l, USEC_FMT "min%s",
8✔
501
                                d / USEC_PER_MINUTE, s);
502
        else if (d >= USEC_PER_MINUTE)
599✔
503
                (void) snprintf(buf, l, USEC_FMT "min " USEC_FMT "s%s",
2✔
504
                                d / USEC_PER_MINUTE,
505
                                (d % USEC_PER_MINUTE) / USEC_PER_SEC, s);
2✔
506
        else if (d >= USEC_PER_SEC)
597✔
507
                (void) snprintf(buf, l, USEC_FMT "s%s",
503✔
508
                                d / USEC_PER_SEC, s);
509
        else if (d >= USEC_PER_MSEC)
94✔
510
                (void) snprintf(buf, l, USEC_FMT "ms%s",
91✔
511
                                d / USEC_PER_MSEC, s);
512
        else if (d > 0)
3✔
513
                (void) snprintf(buf, l, USEC_FMT"us%s",
3✔
514
                                d, s);
515
        else
516
                (void) snprintf(buf, l, "now");
×
517

518
        buf[l-1] = 0;
1,407✔
519
        return buf;
1,407✔
520
}
521

522
char* format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
14,336✔
523
        static const struct {
14,336✔
524
                const char *suffix;
525
                usec_t usec;
526
        } table[] = {
527
                { "y",     USEC_PER_YEAR   },
528
                { "month", USEC_PER_MONTH  },
529
                { "w",     USEC_PER_WEEK   },
530
                { "d",     USEC_PER_DAY    },
531
                { "h",     USEC_PER_HOUR   },
532
                { "min",   USEC_PER_MINUTE },
533
                { "s",     USEC_PER_SEC    },
534
                { "ms",    USEC_PER_MSEC   },
535
                { "us",    1               },
536
        };
537

538
        char *p = ASSERT_PTR(buf);
14,336✔
539
        bool something = false;
14,336✔
540

541
        assert(l > 0);
14,336✔
542

543
        if (t == USEC_INFINITY) {
14,336✔
544
                strncpy(p, "infinity", l-1);
932✔
545
                p[l-1] = 0;
932✔
546
                return p;
932✔
547
        }
548

549
        if (t <= 0) {
13,404✔
550
                strncpy(p, "0", l-1);
570✔
551
                p[l-1] = 0;
570✔
552
                return p;
570✔
553
        }
554

555
        /* The result of this function can be parsed with parse_sec */
556

557
        FOREACH_ELEMENT(i, table) {
106,826✔
558
                int k = 0;
106,735✔
559
                size_t n;
106,735✔
560
                bool done = false;
106,735✔
561
                usec_t a, b;
106,735✔
562

563
                if (t <= 0)
106,735✔
564
                        break;
565

566
                if (t < accuracy && something)
94,028✔
567
                        break;
568

569
                if (t < i->usec)
93,992✔
570
                        continue;
76,314✔
571

572
                if (l <= 1)
17,678✔
573
                        break;
574

575
                a = t / i->usec;
17,678✔
576
                b = t % i->usec;
17,678✔
577

578
                /* Let's see if we should shows this in dot notation */
579
                if (t < USEC_PER_MINUTE && b > 0) {
17,678✔
580
                        signed char j = 0;
581

582
                        for (usec_t cc = i->usec; cc > 1; cc /= 10)
57,894✔
583
                                j++;
47,694✔
584

585
                        for (usec_t cc = accuracy; cc > 1; cc /= 10) {
46,581✔
586
                                b /= 10;
36,381✔
587
                                j--;
36,381✔
588
                        }
589

590
                        if (j > 0) {
10,200✔
591
                                k = snprintf(p, l,
6,246✔
592
                                             "%s"USEC_FMT".%0*"PRI_USEC"%s",
593
                                             p > buf ? " " : "",
594
                                             a,
595
                                             j,
596
                                             b,
597
                                             i->suffix);
3,123✔
598

599
                                t = 0;
3,123✔
600
                                done = true;
3,123✔
601
                        }
602
                }
603

604
                /* No? Then let's show it normally */
605
                if (!done) {
3,123✔
606
                        k = snprintf(p, l,
29,110✔
607
                                     "%s"USEC_FMT"%s",
608
                                     p > buf ? " " : "",
609
                                     a,
610
                                     i->suffix);
14,555✔
611

612
                        t = b;
14,555✔
613
                }
614

615
                n = MIN((size_t) k, l-1);
17,678✔
616

617
                l -= n;
17,678✔
618
                p += n;
17,678✔
619

620
                something = true;
17,678✔
621
        }
622

623
        *p = 0;
12,834✔
624

625
        return buf;
12,834✔
626
}
627

628
const char* get_tzname(bool dst) {
2,956✔
629
        /* musl leaves the DST timezone name unset if there is no DST, map this back to no DST */
630
        if (dst && isempty(tzname[1]))
2,957✔
631
                dst = false;
632

633
        return empty_to_null(tzname[dst]);
2,956✔
634
}
635

636
int parse_gmtoff(const char *t, long *ret) {
1,234✔
637
        int r;
1,234✔
638

639
        assert(t);
1,234✔
640

641
        struct tm tm;
1,234✔
642
        const char *k = strptime(t, "%z", &tm);
1,234✔
643
        if (k && *k == '\0') {
1,234✔
644
                /* Success! */
645
                if (ret)
172✔
646
                        *ret = tm.tm_gmtoff;
172✔
647
                return 0;
172✔
648
        }
649

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

656
        bool positive;
1,062✔
657
        switch (*t) {
1,062✔
658
        case '+':
659
                positive = true;
660
                break;
661
        case '-':
103✔
662
                positive = false;
103✔
663
                break;
103✔
664
        default:
665
                return -EINVAL;
666
        }
667

668
        t++;
108✔
669
        r = undecchar(*t);
108✔
670
        if (r < 0)
108✔
671
                return r;
672

673
        usec_t u = r * 10 * USEC_PER_HOUR;
108✔
674

675
        t++;
108✔
676
        r = undecchar(*t);
108✔
677
        if (r < 0)
108✔
678
                return r;
679
        u += r * USEC_PER_HOUR;
107✔
680

681
        t++;
107✔
682
        if (*t == '\0') /* 2 digits case */
107✔
683
                goto finalize;
×
684

685
        if (*t == ':') /* skip colon */
107✔
686
                t++;
×
687

688
        r = undecchar(*t);
107✔
689
        if (r < 0)
107✔
690
                return r;
691
        if (r >= 6) /* refuse minutes equal to or larger than 60 */
5✔
692
                return -EINVAL;
693

694
        u += r * 10 * USEC_PER_MINUTE;
5✔
695

696
        t++;
5✔
697
        r = undecchar(*t);
5✔
698
        if (r < 0)
5✔
699
                return r;
700

701
        u += r * USEC_PER_MINUTE;
5✔
702

703
        t++;
5✔
704
        if (*t != '\0')
5✔
705
                return -EINVAL;
706

707
finalize:
×
708
        if (u > USEC_PER_DAY) /* refuse larger than one day */
×
709
                return -EINVAL;
710

711
        if (ret) {
×
712
                long gmtoff = u / USEC_PER_SEC;
×
713
                *ret = positive ? gmtoff : -gmtoff;
×
714
        }
715

716
        return 0;
717
}
718

719
static int parse_timestamp_impl(
2,130✔
720
                const char *t,
721
                size_t max_len,
722
                bool utc,
723
                int isdst,
724
                long gmtoff,
725
                usec_t *ret) {
726

727
        static const struct {
2,130✔
728
                const char *name;
729
                const int nr;
730
        } day_nr[] = {
731
                { "Sunday",    0 },
732
                { "Sun",       0 },
733
                { "Monday",    1 },
734
                { "Mon",       1 },
735
                { "Tuesday",   2 },
736
                { "Tue",       2 },
737
                { "Wednesday", 3 },
738
                { "Wed",       3 },
739
                { "Thursday",  4 },
740
                { "Thu",       4 },
741
                { "Friday",    5 },
742
                { "Fri",       5 },
743
                { "Saturday",  6 },
744
                { "Sat",       6 },
745
        };
746

747
        _cleanup_free_ char *t_alloc = NULL;
2,130✔
748
        usec_t usec, plus = 0, minus = 0;
2,130✔
749
        bool with_tz = false;
2,130✔
750
        int r, weekday = -1;
2,130✔
751
        unsigned fractional = 0;
2,130✔
752
        const char *k;
2,130✔
753
        struct tm tm, copy;
2,130✔
754

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

787
        assert(t);
2,130✔
788

789
        if (max_len != SIZE_MAX) {
2,130✔
790
                /* If the input string contains timezone, then cut it here. */
791

792
                if (max_len == 0) /* Can't be the only field */
1,690✔
793
                        return -EINVAL;
794

795
                t_alloc = strndup(t, max_len);
1,690✔
796
                if (!t_alloc)
1,690✔
797
                        return -ENOMEM;
798

799
                t = t_alloc;
800
                with_tz = true;
801
        }
802

803
        if (utc) {
2,130✔
804
                /* glibc accepts gmtoff more than 24 hours, but we refuse it. */
805
                if ((usec_t) labs(gmtoff) * USEC_PER_SEC > USEC_PER_DAY)
991✔
806
                        return -EINVAL;
807
        } else {
808
                if (gmtoff != 0)
1,139✔
809
                        return -EINVAL;
810
        }
811

812
        if (t[0] == '@' && !with_tz)
2,130✔
813
                return parse_sec(t + 1, ret);
107✔
814

815
        usec = now(CLOCK_REALTIME);
2,023✔
816

817
        if (!with_tz) {
2,023✔
818
                if (streq(t, "now"))
333✔
819
                        goto finish;
5✔
820

821
                if (t[0] == '+') {
328✔
822
                        r = parse_sec(t+1, &plus);
6✔
823
                        if (r < 0)
6✔
824
                                return r;
825

826
                        goto finish;
6✔
827
                }
828

829
                if (t[0] == '-') {
322✔
830
                        r = parse_sec(t+1, &minus);
6✔
831
                        if (r < 0)
6✔
832
                                return r;
833

834
                        goto finish;
6✔
835
                }
836

837
                if ((k = endswith(t, " ago"))) {
316✔
838
                        _cleanup_free_ char *buf = NULL;
3✔
839

840
                        buf = strndup(t, k - t);
3✔
841
                        if (!buf)
3✔
842
                                return -ENOMEM;
843

844
                        r = parse_sec(buf, &minus);
3✔
845
                        if (r < 0)
3✔
846
                                return r;
847

848
                        goto finish;
3✔
849
                }
850

851
                if ((k = endswith(t, " left"))) {
313✔
852
                        _cleanup_free_ char *buf = NULL;
102✔
853

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

858
                        r = parse_sec(buf, &plus);
102✔
859
                        if (r < 0)
102✔
860
                                return r;
861

862
                        goto finish;
102✔
863
                }
864
        }
865

866
        r = localtime_or_gmtime_usec(usec, utc, &tm);
1,901✔
867
        if (r < 0)
1,901✔
868
                return r;
869

870
        tm.tm_isdst = isdst;
1,901✔
871

872
        if (streq(t, "today")) {
1,901✔
873
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
13✔
874
                goto from_tm;
13✔
875

876
        } else if (streq(t, "yesterday")) {
1,888✔
877
                tm.tm_mday--;
10✔
878
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
10✔
879
                goto from_tm;
10✔
880

881
        } else if (streq(t, "tomorrow")) {
1,878✔
882
                tm.tm_mday++;
8✔
883
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
8✔
884
                goto from_tm;
8✔
885
        }
886

887
        FOREACH_ELEMENT(day, day_nr) {
19,532✔
888
                k = startswith_no_case(t, day->name);
19,214✔
889
                if (!k || *k != ' ')
19,214✔
890
                        continue;
17,662✔
891

892
#ifdef __GLIBC__
893
                /* musl does not set tm_wday field and set 0 unless it is explicitly requested by %w or so.
894
                 * In the below, let's only check tm_wday field only when built with glibc. */
895
                weekday = day->nr;
1,552✔
896
#endif
897
                t = k + 1;
1,552✔
898
                break;
1,552✔
899
        }
900

901
        copy = tm;
1,870✔
902
        k = strptime(t, "%y-%m-%d %H:%M:%S", &tm);
1,870✔
903
        if (k) {
1,870✔
904
                if (*k == '.')
100✔
905
                        goto parse_usec;
64✔
906
                else if (*k == 0)
36✔
907
                        goto from_tm;
32✔
908
        }
909

910
        /* Our "canonical" RFC3339 syntax variant */
911
        tm = copy;
1,774✔
912
        k = strptime(t, "%Y-%m-%d %H:%M:%S", &tm);
1,774✔
913
        if (k) {
1,774✔
914
                if (*k == '.')
1,474✔
915
                        goto parse_usec;
345✔
916
                else if (*k == 0)
1,129✔
917
                        goto from_tm;
1,120✔
918
        }
919

920
        /* RFC3339 syntax */
921
        tm = copy;
309✔
922
        k = strptime(t, "%Y-%m-%dT%H:%M:%S", &tm);
309✔
923
        if (k) {
309✔
924
                if (*k == '.')
42✔
925
                        goto parse_usec;
20✔
926
                else if (*k == 0)
22✔
927
                        goto from_tm;
22✔
928
        }
929

930
        /* Support OUTPUT_SHORT and OUTPUT_SHORT_PRECISE formats */
931
        tm = copy;
267✔
932
        k = strptime(t, "%b %d %H:%M:%S", &tm);
267✔
933
        if (k) {
267✔
934
                if (*k == '.')
4✔
935
                        goto parse_usec;
2✔
936
                else if (*k == 0)
2✔
937
                        goto from_tm;
2✔
938
        }
939

940
        tm = copy;
263✔
941
        k = strptime(t, "%y-%m-%d %H:%M", &tm);
263✔
942
        if (k && *k == 0) {
263✔
943
                tm.tm_sec = 0;
30✔
944
                goto from_tm;
30✔
945
        }
946

947
        /* Our "canonical" RFC3339 syntax variant without seconds */
948
        tm = copy;
233✔
949
        k = strptime(t, "%Y-%m-%d %H:%M", &tm);
233✔
950
        if (k && *k == 0) {
233✔
951
                tm.tm_sec = 0;
31✔
952
                goto from_tm;
31✔
953
        }
954

955
        /* RFC3339 syntax without seconds */
956
        tm = copy;
202✔
957
        k = strptime(t, "%Y-%m-%dT%H:%M", &tm);
202✔
958
        if (k && *k == 0) {
202✔
959
                tm.tm_sec = 0;
8✔
960
                goto from_tm;
8✔
961
        }
962

963
        tm = copy;
194✔
964
        k = strptime(t, "%y-%m-%d", &tm);
194✔
965
        if (k && *k == 0) {
194✔
966
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
×
967
                goto from_tm;
×
968
        }
969

970
        tm = copy;
194✔
971
        k = strptime(t, "%Y-%m-%d", &tm);
194✔
972
        if (k && *k == 0) {
194✔
973
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
106✔
974
                goto from_tm;
106✔
975
        }
976

977
        tm = copy;
88✔
978
        k = strptime(t, "%H:%M:%S", &tm);
88✔
979
        if (k) {
88✔
980
                if (*k == '.')
30✔
981
                        goto parse_usec;
8✔
982
                else if (*k == 0)
22✔
983
                        goto from_tm;
22✔
984
        }
985

986
        tm = copy;
58✔
987
        k = strptime(t, "%H:%M", &tm);
58✔
988
        if (k && *k == 0) {
58✔
989
                tm.tm_sec = 0;
6✔
990
                goto from_tm;
6✔
991
        }
992

993
        return -EINVAL;
994

995
parse_usec:
439✔
996
        k++;
439✔
997
        r = parse_fractional_part_u(&k, 6, &fractional);
439✔
998
        if (r < 0)
439✔
999
                return -EINVAL;
1000
        if (*k != '\0')
439✔
1001
                return -EINVAL;
1002

1003
from_tm:
423✔
1004
        assert(plus == 0);
1,833✔
1005
        assert(minus == 0);
1,833✔
1006

1007
        if (weekday >= 0 && tm.tm_wday != weekday)
1,833✔
1008
                return -EINVAL;
1009

1010
        if (gmtoff < 0) {
1,833✔
1011
                plus = -gmtoff * USEC_PER_SEC;
110✔
1012

1013
                /* If gmtoff is negative, the string may be too old to be parsed as UTC.
1014
                 * E.g. 1969-12-31 23:00:00 -06 == 1970-01-01 05:00:00 UTC
1015
                 * We assumed that gmtoff is in the range of -24:00…+24:00, hence the only date we need to
1016
                 * handle here is 1969-12-31. So, let's shift the date with one day, then subtract the shift
1017
                 * later. */
1018
                if (tm.tm_year == 69 && tm.tm_mon == 11 && tm.tm_mday == 31) {
110✔
1019
                        /* Thu 1970-01-01-00:00:00 */
1020
                        tm.tm_year = 70;
96✔
1021
                        tm.tm_mon = 0;
96✔
1022
                        tm.tm_mday = 1;
96✔
1023
                        tm.tm_wday = 4;
96✔
1024
                        tm.tm_yday = 0;
96✔
1025
                        minus = USEC_PER_DAY;
96✔
1026
                }
1027
        } else
1028
                minus = gmtoff * USEC_PER_SEC;
1,723✔
1029

1030
        r = mktime_or_timegm_usec(&tm, utc, &usec);
1,833✔
1031
        if (r < 0)
1,833✔
1032
                return r;
1033

1034
        usec = usec_add(usec, fractional);
3,660✔
1035

1036
finish:
1,952✔
1037
        usec = usec_add(usec, plus);
1,952✔
1038

1039
        if (usec < minus)
1,952✔
1040
                return -EINVAL;
1041

1042
        usec = usec_sub_unsigned(usec, minus);
1,951✔
1043

1044
        if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX)
1,951✔
1045
                return -EINVAL;
1046

1047
        if (ret)
1,950✔
1048
                *ret = usec;
1,948✔
1049
        return 0;
1050
}
1051

1052
int parse_timestamp(const char *t, usec_t *ret) {
2,097✔
1053
        long gmtoff;
2,097✔
1054
        int r;
2,097✔
1055

1056
        assert(t);
2,097✔
1057

1058
        size_t t_len = strlen(t);
2,097✔
1059
        if (t_len > 2 && t[t_len - 1] == 'Z') {
2,097✔
1060
                /* Try to parse as RFC3339-style welded UTC: "1985-04-12T23:20:50.52Z" */
1061
                r = parse_timestamp_impl(t, t_len - 1, /* utc = */ true, /* isdst = */ -1, /* gmtoff = */ 0, ret);
67✔
1062
                if (r >= 0)
67✔
1063
                        return r;
2,097✔
1064
        }
1065

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

1070
        const char *tz = strrchr(t, ' ');
2,023✔
1071
        if (!tz)
2,023✔
1072
                return parse_timestamp_impl(t, /* max_len = */ SIZE_MAX, /* utc = */ false, /* isdst = */ -1, /* gmtoff = */ 0, ret);
191✔
1073

1074
        size_t max_len = tz - t;
1,832✔
1075
        tz++;
1,832✔
1076

1077
        /* Shortcut, parse the string as UTC. */
1078
        if (streq(tz, "UTC"))
1,832✔
1079
                return parse_timestamp_impl(t, max_len, /* utc = */ true, /* isdst = */ -1, /* gmtoff = */ 0, ret);
758✔
1080

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

1087
        /* Check if the last word matches tzname[] of the local timezone. Note, this must be done earlier
1088
         * than the check by timezone_is_valid() below, as some short timezone specifications have their own
1089
         * timezone files (e.g. WET has its own timezone file, but JST does not), but using such files does
1090
         * not follow the timezone change in the current area. */
1091
        tzset();
948✔
1092
        for (int j = 0; j <= 1; j++) {
2,844✔
1093
                if (!streq_ptr(tz, get_tzname(j)))
1,896✔
1094
                        continue;
1,896✔
1095

1096
                /* The specified timezone matches tzname[] of the local timezone. */
1097
                return parse_timestamp_impl(t, max_len, /* utc = */ false, /* isdst = */ j, /* gmtoff = */ 0, ret);
×
1098
        }
1099

1100
        /* If the last word is a valid timezone file (e.g. Asia/Tokyo), then save the current timezone, apply
1101
         * the specified timezone, and parse the remaining string as a local time. */
1102
        if (timezone_is_valid(tz, LOG_DEBUG)) {
948✔
1103
                SAVE_TIMEZONE;
1,398✔
1104

1105
                if (setenv("TZ", tz, /* overwrite = */ true) < 0)
699✔
1106
                        return negative_errno();
×
1107

1108
                return parse_timestamp_impl(t, max_len, /* utc = */ false, /* isdst = */ -1, /* gmtoff = */ 0, ret);
699✔
1109
        }
1110

1111
        /* Otherwise, assume that the last word is a part of the time and try to parse the whole string as a
1112
         * local time. */
1113
        return parse_timestamp_impl(t, SIZE_MAX, /* utc = */ false, /* isdst = */ -1, /* gmtoff = */ 0, ret);
249✔
1114
}
1115

1116
static const char* extract_multiplier(const char *p, usec_t *ret) {
10,262✔
1117
        static const struct {
10,262✔
1118
                const char *suffix;
1119
                usec_t usec;
1120
        } table[] = {
1121
                { "seconds", USEC_PER_SEC    },
1122
                { "second",  USEC_PER_SEC    },
1123
                { "sec",     USEC_PER_SEC    },
1124
                { "s",       USEC_PER_SEC    },
1125
                { "minutes", USEC_PER_MINUTE },
1126
                { "minute",  USEC_PER_MINUTE },
1127
                { "min",     USEC_PER_MINUTE },
1128
                { "months",  USEC_PER_MONTH  },
1129
                { "month",   USEC_PER_MONTH  },
1130
                { "M",       USEC_PER_MONTH  },
1131
                { "msec",    USEC_PER_MSEC   },
1132
                { "ms",      USEC_PER_MSEC   },
1133
                { "m",       USEC_PER_MINUTE },
1134
                { "hours",   USEC_PER_HOUR   },
1135
                { "hour",    USEC_PER_HOUR   },
1136
                { "hr",      USEC_PER_HOUR   },
1137
                { "h",       USEC_PER_HOUR   },
1138
                { "days",    USEC_PER_DAY    },
1139
                { "day",     USEC_PER_DAY    },
1140
                { "d",       USEC_PER_DAY    },
1141
                { "weeks",   USEC_PER_WEEK   },
1142
                { "week",    USEC_PER_WEEK   },
1143
                { "w",       USEC_PER_WEEK   },
1144
                { "years",   USEC_PER_YEAR   },
1145
                { "year",    USEC_PER_YEAR   },
1146
                { "y",       USEC_PER_YEAR   },
1147
                { "usec",    1ULL            },
1148
                { "us",      1ULL            },
1149
                { "μs",      1ULL            }, /* U+03bc (aka GREEK SMALL LETTER MU) */
1150
                { "µs",      1ULL            }, /* U+b5 (aka MICRO SIGN) */
1151
        };
1152

1153
        assert(p);
10,262✔
1154
        assert(ret);
10,262✔
1155

1156
        FOREACH_ELEMENT(i, table) {
175,900✔
1157
                char *e;
172,334✔
1158

1159
                e = startswith(p, i->suffix);
172,334✔
1160
                if (e) {
172,334✔
1161
                        *ret = i->usec;
6,696✔
1162
                        return e;
6,696✔
1163
                }
1164
        }
1165

1166
        return p;
1167
}
1168

1169
int parse_time(const char *t, usec_t *ret, usec_t default_unit) {
10,349✔
1170
        const char *p, *s;
10,349✔
1171

1172
        assert(t);
10,349✔
1173
        assert(default_unit > 0);
10,349✔
1174

1175
        p = skip_leading_chars(t, /* bad = */ NULL);
10,349✔
1176
        s = startswith(p, "infinity");
10,349✔
1177
        if (s) {
10,349✔
1178
                if (!in_charset(s, WHITESPACE))
305✔
1179
                        return -EINVAL;
1180

1181
                if (ret)
304✔
1182
                        *ret = USEC_INFINITY;
304✔
1183
                return 0;
304✔
1184
        }
1185

1186
        usec_t usec = 0;
1187

1188
        for (bool something = false;;) {
10,246✔
1189
                usec_t multiplier = default_unit, k;
20,290✔
1190
                long long l;
20,290✔
1191
                char *e;
20,290✔
1192

1193
                p = skip_leading_chars(p, /* bad = */ NULL);
20,290✔
1194
                if (*p == 0) {
20,290✔
1195
                        if (!something)
10,006✔
1196
                                return -EINVAL;
43✔
1197

1198
                        break;
10,001✔
1199
                }
1200

1201
                if (*p == '-') /* Don't allow "-0" */
10,284✔
1202
                        return -ERANGE;
1203

1204
                errno = 0;
10,276✔
1205
                l = strtoll(p, &e, 10);
10,276✔
1206
                if (errno > 0)
10,276✔
1207
                        return -errno;
×
1208
                if (l < 0)
10,276✔
1209
                        return -ERANGE;
1210

1211
                if (*e == '.') {
10,276✔
1212
                        p = e + 1;
55✔
1213
                        p += strspn(p, DIGITS);
55✔
1214
                } else if (e == p)
10,221✔
1215
                        return -EINVAL;
1216
                else
1217
                        p = e;
1218

1219
                s = extract_multiplier(p + strspn(p, WHITESPACE), &multiplier);
10,262✔
1220
                if (s == p && *s != '\0')
10,262✔
1221
                        /* Don't allow '12.34.56', but accept '12.34 .56' or '12.34s.56' */
1222
                        return -EINVAL;
1223

1224
                p = s;
10,253✔
1225

1226
                if ((usec_t) l >= USEC_INFINITY / multiplier)
10,253✔
1227
                        return -ERANGE;
1228

1229
                k = (usec_t) l * multiplier;
10,251✔
1230
                if (k >= USEC_INFINITY - usec)
10,251✔
1231
                        return -ERANGE;
1232

1233
                usec += k;
10,251✔
1234

1235
                something = true;
10,251✔
1236

1237
                if (*e == '.') {
10,251✔
1238
                        usec_t m = multiplier / 10;
48✔
1239
                        const char *b;
48✔
1240

1241
                        for (b = e + 1; *b >= '0' && *b <= '9'; b++, m /= 10) {
188✔
1242
                                k = (usec_t) (*b - '0') * m;
140✔
1243
                                if (k >= USEC_INFINITY - usec)
140✔
1244
                                        return -ERANGE;
1245

1246
                                usec += k;
140✔
1247
                        }
1248

1249
                        /* Don't allow "0.-0", "3.+1", "3. 1", "3.sec" or "3.hoge" */
1250
                        if (b == e + 1)
48✔
1251
                                return -EINVAL;
1252
                }
1253
        }
1254

1255
        if (ret)
10,001✔
1256
                *ret = usec;
9,999✔
1257
        return 0;
1258
}
1259

1260
int parse_sec(const char *t, usec_t *ret) {
10,266✔
1261
        return parse_time(t, ret, USEC_PER_SEC);
10,266✔
1262
}
1263

1264
int parse_sec_fix_0(const char *t, usec_t *ret) {
1,777✔
1265
        usec_t k;
1,777✔
1266
        int r;
1,777✔
1267

1268
        assert(t);
1,777✔
1269
        assert(ret);
1,777✔
1270

1271
        r = parse_sec(t, &k);
1,777✔
1272
        if (r < 0)
1,777✔
1273
                return r;
1,777✔
1274

1275
        *ret = k == 0 ? USEC_INFINITY : k;
1,777✔
1276
        return r;
1,777✔
1277
}
1278

1279
int parse_sec_def_infinity(const char *t, usec_t *ret) {
7✔
1280
        assert(t);
7✔
1281
        assert(ret);
7✔
1282

1283
        t += strspn(t, WHITESPACE);
7✔
1284
        if (isempty(t)) {
7✔
1285
                *ret = USEC_INFINITY;
2✔
1286
                return 0;
2✔
1287
        }
1288
        return parse_sec(t, ret);
5✔
1289
}
1290

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

1331
        assert(p);
44✔
1332
        assert(ret);
44✔
1333

1334
        FOREACH_ELEMENT(i, table) {
803✔
1335
                char *e;
803✔
1336

1337
                e = startswith(p, i->suffix);
803✔
1338
                if (e) {
803✔
1339
                        *ret = i->nsec;
44✔
1340
                        return e;
44✔
1341
                }
1342
        }
1343

1344
        return p;
1345
}
1346

1347
int parse_nsec(const char *t, nsec_t *ret) {
48✔
1348
        const char *p, *s;
48✔
1349
        nsec_t nsec = 0;
48✔
1350
        bool something = false;
48✔
1351

1352
        assert(t);
48✔
1353
        assert(ret);
48✔
1354

1355
        p = t;
48✔
1356

1357
        p += strspn(p, WHITESPACE);
48✔
1358
        s = startswith(p, "infinity");
48✔
1359
        if (s) {
48✔
1360
                s += strspn(s, WHITESPACE);
4✔
1361
                if (*s != 0)
4✔
1362
                        return -EINVAL;
1363

1364
                *ret = NSEC_INFINITY;
2✔
1365
                return 0;
2✔
1366
        }
1367

1368
        for (;;) {
30✔
1369
                nsec_t multiplier = 1, k;
74✔
1370
                long long l;
74✔
1371
                char *e;
74✔
1372

1373
                p += strspn(p, WHITESPACE);
74✔
1374

1375
                if (*p == 0) {
74✔
1376
                        if (!something)
21✔
1377
                                return -EINVAL;
24✔
1378

1379
                        break;
20✔
1380
                }
1381

1382
                if (*p == '-') /* Don't allow "-0" */
53✔
1383
                        return -ERANGE;
1384

1385
                errno = 0;
48✔
1386
                l = strtoll(p, &e, 10);
48✔
1387
                if (errno > 0)
48✔
1388
                        return -errno;
×
1389
                if (l < 0)
48✔
1390
                        return -ERANGE;
1391

1392
                if (*e == '.') {
48✔
1393
                        p = e + 1;
31✔
1394
                        p += strspn(p, DIGITS);
31✔
1395
                } else if (e == p)
17✔
1396
                        return -EINVAL;
1397
                else
1398
                        p = e;
1399

1400
                s = extract_nsec_multiplier(p + strspn(p, WHITESPACE), &multiplier);
44✔
1401
                if (s == p && *s != '\0')
44✔
1402
                        /* Don't allow '12.34.56', but accept '12.34 .56' or '12.34s.56' */
1403
                        return -EINVAL;
1404

1405
                p = s;
36✔
1406

1407
                if ((nsec_t) l >= NSEC_INFINITY / multiplier)
36✔
1408
                        return -ERANGE;
1409

1410
                k = (nsec_t) l * multiplier;
35✔
1411
                if (k >= NSEC_INFINITY - nsec)
35✔
1412
                        return -ERANGE;
1413

1414
                nsec += k;
35✔
1415

1416
                something = true;
35✔
1417

1418
                if (*e == '.') {
35✔
1419
                        nsec_t m = multiplier / 10;
24✔
1420
                        const char *b;
24✔
1421

1422
                        for (b = e + 1; *b >= '0' && *b <= '9'; b++, m /= 10) {
56✔
1423
                                k = (nsec_t) (*b - '0') * m;
32✔
1424
                                if (k >= NSEC_INFINITY - nsec)
32✔
1425
                                        return -ERANGE;
1426

1427
                                nsec += k;
32✔
1428
                        }
1429

1430
                        /* Don't allow "0.-0", "3.+1", "3. 1", "3.sec" or "3.hoge" */
1431
                        if (b == e + 1)
24✔
1432
                                return -EINVAL;
1433
                }
1434
        }
1435

1436
        *ret = nsec;
20✔
1437

1438
        return 0;
20✔
1439
}
1440

1441
static int get_timezones_from_zone1970_tab(char ***ret) {
×
1442
        _cleanup_fclose_ FILE *f = NULL;
×
1443
        _cleanup_strv_free_ char **zones = NULL;
×
1444
        int r;
×
1445

1446
        assert(ret);
×
1447

1448
        f = fopen("/usr/share/zoneinfo/zone1970.tab", "re");
×
1449
        if (!f)
×
1450
                return -errno;
×
1451

1452
        for (;;) {
×
1453
                _cleanup_free_ char *line = NULL, *cc = NULL, *co = NULL, *tz = NULL;
×
1454

1455
                r = read_line(f, LONG_LINE_MAX, &line);
×
1456
                if (r < 0)
×
1457
                        return r;
1458
                if (r == 0)
×
1459
                        break;
1460

1461
                const char *p = line;
×
1462

1463
                /* Line format is:
1464
                 * 'country codes' 'coordinates' 'timezone' 'comments' */
1465
                r = extract_many_words(&p, NULL, 0, &cc, &co, &tz);
×
1466
                if (r < 0)
×
1467
                        continue;
×
1468

1469
                /* Lines that start with # are comments. */
1470
                if (*cc == '#')
×
1471
                        continue;
×
1472

1473
                if (!timezone_is_valid(tz, LOG_DEBUG))
×
1474
                        /* Don't list unusable timezones. */
1475
                        continue;
×
1476

1477
                r = strv_extend(&zones, tz);
×
1478
                if (r < 0)
×
1479
                        return r;
1480
        }
1481

1482
        *ret = TAKE_PTR(zones);
×
1483
        return 0;
×
1484
}
1485

1486
static int get_timezones_from_tzdata_zi(char ***ret) {
4✔
1487
        _cleanup_fclose_ FILE *f = NULL;
4✔
1488
        _cleanup_strv_free_ char **zones = NULL;
4✔
1489
        int r;
4✔
1490

1491
        assert(ret);
4✔
1492

1493
        f = fopen("/usr/share/zoneinfo/tzdata.zi", "re");
4✔
1494
        if (!f)
4✔
1495
                return -errno;
×
1496

1497
        for (;;) {
17,204✔
1498
                _cleanup_free_ char *line = NULL, *type = NULL, *f1 = NULL, *f2 = NULL;
17,200✔
1499

1500
                r = read_line(f, LONG_LINE_MAX, &line);
17,204✔
1501
                if (r < 0)
17,204✔
1502
                        return r;
1503
                if (r == 0)
17,204✔
1504
                        break;
1505

1506
                const char *p = line;
17,200✔
1507

1508
                /* The only lines we care about are Zone and Link lines.
1509
                 * Zone line format is:
1510
                 * 'Zone' 'timezone' ...
1511
                 * Link line format is:
1512
                 * 'Link' 'target' 'alias'
1513
                 * See 'man zic' for more detail. */
1514
                r = extract_many_words(&p, NULL, 0, &type, &f1, &f2);
17,200✔
1515
                if (r < 0)
17,200✔
1516
                        continue;
×
1517

1518
                char *tz;
17,200✔
1519
                if (IN_SET(*type, 'Z', 'z'))
17,200✔
1520
                        /* Zone lines have timezone in field 1. */
1521
                        tz = f1;
1,364✔
1522
                else if (IN_SET(*type, 'L', 'l'))
15,836✔
1523
                        /* Link lines have timezone in field 2. */
1524
                        tz = f2;
1,028✔
1525
                else
1526
                        /* Not a line we care about. */
1527
                        continue;
14,808✔
1528

1529
                if (!timezone_is_valid(tz, LOG_DEBUG))
2,392✔
1530
                        /* Don't list unusable timezones. */
1531
                        continue;
×
1532

1533
                r = strv_extend(&zones, tz);
2,392✔
1534
                if (r < 0)
2,392✔
1535
                        return r;
1536
        }
1537

1538
        *ret = TAKE_PTR(zones);
4✔
1539
        return 0;
4✔
1540
}
1541

1542
int get_timezones(char ***ret) {
4✔
1543
        _cleanup_strv_free_ char **zones = NULL;
4✔
1544
        int r;
4✔
1545

1546
        assert(ret);
4✔
1547

1548
        r = get_timezones_from_tzdata_zi(&zones);
4✔
1549
        if (r == -ENOENT) {
4✔
1550
                log_debug_errno(r, "Could not get timezone data from tzdata.zi, using zone1970.tab: %m");
×
1551
                r = get_timezones_from_zone1970_tab(&zones);
×
1552
                if (r == -ENOENT)
×
1553
                        log_debug_errno(r, "Could not get timezone data from zone1970.tab, using UTC: %m");
×
1554
        }
1555
        if (r < 0 && r != -ENOENT)
4✔
1556
                return r;
1557

1558
        /* Always include UTC */
1559
        r = strv_extend(&zones, "UTC");
4✔
1560
        if (r < 0)
4✔
1561
                return r;
1562

1563
        strv_sort_uniq(zones);
4✔
1564

1565
        *ret = TAKE_PTR(zones);
4✔
1566
        return 0;
4✔
1567
}
1568

1569
int verify_timezone(const char *name, int log_level) {
4,212✔
1570
        bool slash = false;
4,212✔
1571
        const char *p, *t;
4,212✔
1572
        _cleanup_close_ int fd = -EBADF;
4,212✔
1573
        char buf[4];
4,212✔
1574
        int r;
4,212✔
1575

1576
        if (isempty(name))
8,424✔
1577
                return -EINVAL;
1578

1579
        /* Always accept "UTC" as valid timezone, since it's the fallback, even if user has no timezones installed. */
1580
        if (streq(name, "UTC"))
4,210✔
1581
                return 0;
1582

1583
        if (name[0] == '/')
4,203✔
1584
                return -EINVAL;
1585

1586
        for (p = name; *p; p++) {
59,263✔
1587
                if (!ascii_isdigit(*p) &&
55,208✔
1588
                    !ascii_isalpha(*p) &&
53,908✔
1589
                    !IN_SET(*p, '-', '_', '+', '/'))
4,786✔
1590
                        return -EINVAL;
1591

1592
                if (*p == '/') {
55,061✔
1593

1594
                        if (slash)
3,707✔
1595
                                return -EINVAL;
1596

1597
                        slash = true;
1598
                } else
1599
                        slash = false;
1600
        }
1601

1602
        if (slash)
4,055✔
1603
                return -EINVAL;
1604

1605
        if (p - name >= PATH_MAX)
4,054✔
1606
                return -ENAMETOOLONG;
1607

1608
        t = strjoina("/usr/share/zoneinfo/", name);
20,270✔
1609

1610
        fd = open(t, O_RDONLY|O_CLOEXEC);
4,054✔
1611
        if (fd < 0)
4,054✔
1612
                return log_full_errno(log_level, errno, "Failed to open timezone file '%s': %m", t);
210✔
1613

1614
        r = fd_verify_regular(fd);
3,844✔
1615
        if (r < 0)
3,844✔
1616
                return log_full_errno(log_level, r, "Timezone file '%s' is not a regular file: %m", t);
×
1617

1618
        r = loop_read_exact(fd, buf, 4, false);
3,844✔
1619
        if (r < 0)
3,844✔
1620
                return log_full_errno(log_level, r, "Failed to read from timezone file '%s': %m", t);
×
1621

1622
        /* Magic from tzfile(5) */
1623
        if (memcmp(buf, "TZif", 4) != 0)
3,844✔
1624
                return log_full_errno(log_level, SYNTHETIC_ERRNO(EBADMSG),
×
1625
                                      "Timezone file '%s' has wrong magic bytes", t);
1626

1627
        return 0;
1628
}
1629

1630
void reset_timezonep(char **p) {
757✔
1631
        assert(p);
757✔
1632

1633
        (void) set_unset_env("TZ", *p, /* overwrite = */ true);
757✔
1634
        tzset();
757✔
1635
        *p = mfree(*p);
757✔
1636
}
757✔
1637

1638
char* save_timezone(void) {
757✔
1639
        const char *e = getenv("TZ");
757✔
1640
        if (!e)
757✔
1641
                return NULL;
1642

1643
        char *s = strdup(e);
57✔
1644
        if (!s)
57✔
1645
                log_debug("Failed to save $TZ=%s, unsetting the environment variable.", e);
×
1646

1647
        return s;
1648
}
1649

1650
bool clock_supported(clockid_t clock) {
305,824✔
1651
        struct timespec ts;
305,824✔
1652

1653
        switch (clock) {
305,824✔
1654

1655
        case CLOCK_MONOTONIC:
1656
        case CLOCK_REALTIME:
1657
        case CLOCK_BOOTTIME:
1658
                /* These three are always available in our baseline, and work in timerfd, as of kernel 3.15 */
1659
                return true;
1660

1661
        default:
1✔
1662
                /* For everything else, check properly */
1663
                return clock_gettime(clock, &ts) >= 0;
1✔
1664
        }
1665
}
1666

1667
int get_timezone(char **ret) {
58✔
1668
        _cleanup_free_ char *t = NULL;
58✔
1669
        int r;
58✔
1670

1671
        assert(ret);
58✔
1672

1673
        r = readlink_malloc(etc_localtime(), &t);
58✔
1674
        if (r == -ENOENT)
58✔
1675
                /* If the symlink does not exist, assume "UTC", like glibc does */
1676
                return strdup_to(ret, "UTC");
1✔
1677
        if (r < 0)
57✔
1678
                return r; /* Return EINVAL if not a symlink */
1679

1680
        const char *e = PATH_STARTSWITH_SET(t, "/usr/share/zoneinfo/", "../usr/share/zoneinfo/");
57✔
1681
        if (!e)
57✔
1682
                return -EINVAL;
1683
        if (!timezone_is_valid(e, LOG_DEBUG))
57✔
1684
                return -EINVAL;
1685

1686
        return strdup_to(ret, e);
57✔
1687
}
1688

1689
const char* etc_localtime(void) {
983✔
1690
        static const char *cached = NULL;
983✔
1691

1692
        if (!cached)
983✔
1693
                cached = secure_getenv("SYSTEMD_ETC_LOCALTIME") ?: "/etc/localtime";
874✔
1694

1695
        return cached;
983✔
1696
}
1697

1698
int mktime_or_timegm_usec(
7,315✔
1699
                struct tm *tm, /* input + normalized output */
1700
                bool utc,
1701
                usec_t *ret) {
1702

1703
        time_t t;
7,315✔
1704

1705
        assert(tm);
7,315✔
1706

1707
        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!) */
7,315✔
1708
                return -ERANGE;
1709
        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 */
7,313✔
1710
                return -ERANGE;
1711

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

1717
        t = utc ? timegm(tm) : mktime(tm);
7,313✔
1718
        if (t < 0) /* Refuse negative times and errors */
7,313✔
1719
                return -ERANGE;
1720
        if ((usec_t) t >= USEC_INFINITY / USEC_PER_SEC) /* Never return USEC_INFINITY by accident (or overflow) */
7,311✔
1721
                return -ERANGE;
1722

1723
        if (ret)
7,311✔
1724
                *ret = (usec_t) t * USEC_PER_SEC;
2,146✔
1725
        return 0;
1726
}
1727

1728
int localtime_or_gmtime_usec(
127,145✔
1729
                usec_t t,
1730
                bool utc,
1731
                struct tm *ret) {
1732

1733
        t /= USEC_PER_SEC; /* Round down */
127,145✔
1734
        if (t > (usec_t) TIME_T_MAX)
127,145✔
1735
                return -ERANGE;
1736
        time_t sec = (time_t) t;
127,145✔
1737

1738
        struct tm buf = {};
127,145✔
1739
        if (!(utc ? gmtime_r(&sec, &buf) : localtime_r(&sec, &buf)))
127,145✔
1740
                return -EINVAL;
1741

1742
        if (ret)
127,145✔
1743
                *ret = buf;
127,145✔
1744

1745
        return 0;
1746
}
1747

1748
static uint32_t sysconf_clock_ticks_cached(void) {
653✔
1749
        static thread_local uint32_t hz = 0;
653✔
1750
        long r;
653✔
1751

1752
        if (hz == 0) {
653✔
1753
                r = sysconf(_SC_CLK_TCK);
69✔
1754

1755
                assert(r > 0);
69✔
1756
                hz = r;
69✔
1757
        }
1758

1759
        return hz;
653✔
1760
}
1761

1762
uint32_t usec_to_jiffies(usec_t u) {
9✔
1763
        uint32_t hz = sysconf_clock_ticks_cached();
9✔
1764
        return DIV_ROUND_UP(u, USEC_PER_SEC / hz);
9✔
1765
}
1766

1767
usec_t jiffies_to_usec(uint32_t j) {
644✔
1768
        uint32_t hz = sysconf_clock_ticks_cached();
644✔
1769
        return DIV_ROUND_UP(j * USEC_PER_SEC, hz);
644✔
1770
}
1771

1772
usec_t usec_shift_clock(usec_t x, clockid_t from, clockid_t to) {
654✔
1773
        usec_t a, b;
654✔
1774

1775
        if (x == USEC_INFINITY)
654✔
1776
                return USEC_INFINITY;
1777
        if (map_clock_id(from) == map_clock_id(to))
512✔
1778
                return x;
1779

1780
        a = now(from);
8✔
1781
        b = now(to);
8✔
1782

1783
        if (x > a)
8✔
1784
                /* x lies in the future */
1785
                return usec_add(b, usec_sub_unsigned(x, a));
12✔
1786
        else
1787
                /* x lies in the past */
1788
                return usec_sub_unsigned(b, usec_sub_unsigned(a, x));
6✔
1789
}
1790

1791
bool in_utc_timezone(void) {
676✔
1792
        tzset();
676✔
1793

1794
        return timezone == 0 && daylight == 0;
676✔
1795
}
1796

1797
int time_change_fd(void) {
451✔
1798

1799
        /* We only care for the cancellation event, hence we set the timeout to the latest possible value. */
1800
        static const struct itimerspec its = {
451✔
1801
                .it_value.tv_sec = TIME_T_MAX,
1802
        };
1803

1804
        _cleanup_close_ int fd = -EBADF;
902✔
1805

1806
        assert_cc(sizeof(time_t) == sizeof(TIME_T_MAX));
451✔
1807

1808
        /* Uses TFD_TIMER_CANCEL_ON_SET to get notifications whenever CLOCK_REALTIME makes a jump relative to
1809
         * CLOCK_MONOTONIC. */
1810

1811
        fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
451✔
1812
        if (fd < 0)
451✔
1813
                return -errno;
×
1814

1815
        if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) >= 0)
451✔
1816
                return TAKE_FD(fd);
451✔
1817

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

1828
#if SIZEOF_TIME_T == 8 && ULONG_MAX < UINT64_MAX
1829
        if (ERRNO_IS_NOT_SUPPORTED(errno) || errno == EOVERFLOW) {
1830
                static const struct itimerspec its32 = {
1831
                        .it_value.tv_sec = INT32_MAX,
1832
                };
1833

1834
                if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its32, NULL) >= 0)
1835
                        return TAKE_FD(fd);
1836
        }
1837
#endif
1838

1839
        return -errno;
×
1840
}
1841

1842
static const char* const timestamp_style_table[_TIMESTAMP_STYLE_MAX] = {
1843
        [TIMESTAMP_PRETTY] = "pretty",
1844
        [TIMESTAMP_US]     = "us",
1845
        [TIMESTAMP_UTC]    = "utc",
1846
        [TIMESTAMP_US_UTC] = "us+utc",
1847
        [TIMESTAMP_UNIX]   = "unix",
1848
};
1849

1850
/* Use the macro for enum → string to allow for aliases */
1851
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(timestamp_style, TimestampStyle);
×
1852

1853
/* For the string → enum mapping we use the generic implementation, but also support two aliases */
1854
TimestampStyle timestamp_style_from_string(const char *s) {
8✔
1855
        TimestampStyle t;
8✔
1856

1857
        t = (TimestampStyle) string_table_lookup_from_string(timestamp_style_table, ELEMENTSOF(timestamp_style_table), s);
8✔
1858
        if (t >= 0)
8✔
1859
                return t;
1860
        if (STRPTR_IN_SET(s, "µs", "μs")) /* accept both µ symbols in unicode, i.e. micro symbol + Greek small letter mu. */
2✔
1861
                return TIMESTAMP_US;
1✔
1862
        if (STRPTR_IN_SET(s, "µs+utc", "μs+utc"))
1✔
1863
                return TIMESTAMP_US_UTC;
1✔
1864
        return t;
1865
}
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