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

systemd / systemd / 20355307520

18 Dec 2025 09:15PM UTC coverage: 72.578% (-0.1%) from 72.709%
20355307520

push

github

DaanDeMeyer
mkosi: update debian commit reference to d9f2aa170

* d9f2aa1704 Install systemd-tpm2-generator.8 only for UEFI builds
* ac1c7d8048 Drop dependencies on libcap-dev, no longer used since v259
* c36e5871ca Do not install systemd-sysv-generator.8 in upstream build
* bac0cca0e8 Install new files for upstream build
* 2855fb1302 Update changelog for 259-1 release

309322 of 426195 relevant lines covered (72.58%)

1149469.57 hits per line

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

90.41
/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) {
30,454,079✔
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) {
30,454,079✔
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:
30,454,070✔
43
                return c;
30,454,070✔
44
        }
45
}
46

47
usec_t now(clockid_t clock_id) {
30,451,329✔
48
        struct timespec ts;
30,451,329✔
49

50
        assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
30,451,329✔
51

52
        return timespec_load(&ts);
30,451,329✔
53
}
54

55
nsec_t now_nsec(clockid_t clock_id) {
620✔
56
        struct timespec ts;
620✔
57

58
        assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
620✔
59

60
        return timespec_load_nsec(&ts);
620✔
61
}
62

63
dual_timestamp* dual_timestamp_now(dual_timestamp *ts) {
1,902,123✔
64
        assert(ts);
1,902,123✔
65

66
        ts->realtime = now(CLOCK_REALTIME);
1,902,123✔
67
        ts->monotonic = now(CLOCK_MONOTONIC);
1,902,123✔
68

69
        return ts;
1,902,123✔
70
}
71

72
triple_timestamp* triple_timestamp_now(triple_timestamp *ts) {
3,499,963✔
73
        assert(ts);
3,499,963✔
74

75
        ts->realtime = now(CLOCK_REALTIME);
3,499,963✔
76
        ts->monotonic = now(CLOCK_MONOTONIC);
3,499,963✔
77
        ts->boottime = now(CLOCK_BOOTTIME);
3,499,963✔
78

79
        return ts;
3,499,963✔
80
}
81

82
usec_t map_clock_usec_raw(usec_t from, usec_t from_base, usec_t to_base) {
9,218✔
83

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

91
        if (from >= from_base) { /* In the future */
9,218✔
92
                usec_t delta = from - from_base;
32✔
93

94
                if (to_base >= USEC_INFINITY - delta) /* overflow? */
32✔
95
                        return USEC_INFINITY;
96

97
                return to_base + delta;
32✔
98

99
        } else { /* In the past */
100
                usec_t delta = from_base - from;
9,186✔
101

102
                if (to_base <= delta) /* underflow? */
9,186✔
103
                        return 0;
104

105
                return to_base - delta;
9,177✔
106
        }
107
}
108

109
usec_t map_clock_usec(usec_t from, clockid_t from_clock, clockid_t to_clock) {
495✔
110

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

116
        /* Keep infinity as is */
117
        if (from == USEC_INFINITY)
495✔
118
                return from;
119

120
        return map_clock_usec_raw(from, now(from_clock), now(to_clock));
453✔
121
}
122

123
dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) {
12✔
124
        assert(ts);
12✔
125

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

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

136
triple_timestamp* triple_timestamp_from_realtime(triple_timestamp *ts, usec_t u) {
344✔
137
        usec_t nowr;
344✔
138

139
        assert(ts);
344✔
140

141
        if (!timestamp_is_set(u)) {
344✔
142
                ts->realtime = ts->monotonic = ts->boottime = u;
×
143
                return ts;
×
144
        }
145

146
        nowr = now(CLOCK_REALTIME);
344✔
147

148
        ts->realtime = u;
344✔
149
        ts->monotonic = map_clock_usec_raw(u, nowr, now(CLOCK_MONOTONIC));
344✔
150
        ts->boottime = map_clock_usec_raw(u, nowr, now(CLOCK_BOOTTIME));
344✔
151

152
        return ts;
344✔
153
}
154

155
triple_timestamp* triple_timestamp_from_boottime(triple_timestamp *ts, usec_t u) {
×
156
        usec_t nowb;
×
157

158
        assert(ts);
×
159

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

165
        nowb = now(CLOCK_BOOTTIME);
×
166

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

171
        return ts;
×
172
}
173

174
dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) {
246✔
175
        assert(ts);
246✔
176

177
        if (u == USEC_INFINITY) {
246✔
178
                ts->realtime = ts->monotonic = USEC_INFINITY;
×
179
                return ts;
×
180
        }
181

182
        ts->monotonic = u;
246✔
183
        ts->realtime = map_clock_usec(u, CLOCK_MONOTONIC, CLOCK_REALTIME);
246✔
184
        return ts;
246✔
185
}
186

187
dual_timestamp* dual_timestamp_from_boottime(dual_timestamp *ts, usec_t u) {
585✔
188
        usec_t nowm;
585✔
189

190
        assert(ts);
585✔
191

192
        if (u == USEC_INFINITY) {
585✔
193
                ts->realtime = ts->monotonic = USEC_INFINITY;
×
194
                return ts;
×
195
        }
196

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

203
usec_t triple_timestamp_by_clock(triple_timestamp *ts, clockid_t clock) {
3,779,112✔
204
        assert(ts);
3,779,112✔
205

206
        switch (clock) {
3,779,112✔
207

208
        case CLOCK_REALTIME:
2,073,446✔
209
        case CLOCK_REALTIME_ALARM:
210
                return ts->realtime;
2,073,446✔
211

212
        case CLOCK_MONOTONIC:
1,662,403✔
213
                return ts->monotonic;
1,662,403✔
214

215
        case CLOCK_BOOTTIME:
43,263✔
216
        case CLOCK_BOOTTIME_ALARM:
217
                return ts->boottime;
43,263✔
218

219
        default:
220
                return USEC_INFINITY;
221
        }
222
}
223

224
usec_t timespec_load(const struct timespec *ts) {
30,841,484✔
225
        assert(ts);
30,841,484✔
226

227
        if (ts->tv_sec < 0 || ts->tv_nsec < 0)
30,841,484✔
228
                return USEC_INFINITY;
229

230
        if ((usec_t) ts->tv_sec > (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC)
30,841,484✔
231
                return USEC_INFINITY;
232

233
        return
30,841,484✔
234
                (usec_t) ts->tv_sec * USEC_PER_SEC +
30,841,484✔
235
                (usec_t) ts->tv_nsec / NSEC_PER_USEC;
236
}
237

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

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

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

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

250
struct timespec *timespec_store(struct timespec *ts, usec_t u) {
2,338,034✔
251
        assert(ts);
2,338,034✔
252

253
        if (u == USEC_INFINITY ||
2,338,034✔
254
            u / USEC_PER_SEC >= TIME_T_MAX) {
255
                ts->tv_sec = (time_t) -1;
×
256
                ts->tv_nsec = -1L;
×
257
                return ts;
×
258
        }
259

260
        ts->tv_sec = (time_t) (u / USEC_PER_SEC);
2,338,034✔
261
        ts->tv_nsec = (long) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
2,338,034✔
262

263
        return ts;
2,338,034✔
264
}
265

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

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

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

279
        return ts;
56✔
280
}
281

282
usec_t timeval_load(const struct timeval *tv) {
954,568✔
283
        assert(tv);
954,568✔
284

285
        if (tv->tv_sec < 0 || tv->tv_usec < 0)
954,568✔
286
                return USEC_INFINITY;
287

288
        if ((usec_t) tv->tv_sec > (UINT64_MAX - tv->tv_usec) / USEC_PER_SEC)
954,568✔
289
                return USEC_INFINITY;
290

291
        return
954,568✔
292
                (usec_t) tv->tv_sec * USEC_PER_SEC +
954,568✔
293
                (usec_t) tv->tv_usec;
294
}
295

296
struct timeval *timeval_store(struct timeval *tv, usec_t u) {
169,093✔
297
        assert(tv);
169,093✔
298

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

308
        return tv;
169,093✔
309
}
310

311
char* format_timestamp_style(
7,390✔
312
                char *buf,
313
                size_t l,
314
                usec_t t,
315
                TimestampStyle style) {
316

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

329
        struct tm tm;
7,390✔
330
        bool utc, us;
7,390✔
331
        size_t n;
7,390✔
332

333
        assert(buf);
7,390✔
334
        assert(style >= 0);
7,390✔
335
        assert(style < _TIMESTAMP_STYLE_MAX);
7,390✔
336

337
        if (!timestamp_is_set(t))
7,390✔
338
                return NULL; /* Timestamp is unset */
7,390✔
339

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

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

347
        utc = IN_SET(style, TIMESTAMP_UTC, TIMESTAMP_US_UTC, TIMESTAMP_DATE);
4,853✔
348
        us = IN_SET(style, TIMESTAMP_US, TIMESTAMP_US_UTC);
4,853✔
349

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

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

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

373
        if (localtime_or_gmtime_usec(t, utc, &tm) < 0)
4,849✔
374
                return NULL;
375

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

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

385
                return buf;
104✔
386
        }
387

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

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

398
                sprintf(buf + n, ".%06"PRI_USEC, t % USEC_PER_SEC);
499✔
399
        }
400

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

409
                strcpy(buf + n, " UTC");
231✔
410

411
        } else if (!isempty(tm.tm_zone)) {
4,514✔
412
                size_t tn;
4,514✔
413

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

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

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

434
        return buf;
435
}
436

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

441
        assert(buf);
1,421✔
442

443
        if (!timestamp_is_set(t))
1,421✔
444
                return NULL;
445

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

455
        if (d >= USEC_PER_YEAR) {
1,415✔
456
                usec_t years = d / USEC_PER_YEAR;
742✔
457
                usec_t months = (d % USEC_PER_YEAR) / USEC_PER_MONTH;
742✔
458

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

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

479
                (void) snprintf(buf, l, USEC_FMT " %s " USEC_FMT " %s%s",
16✔
480
                                weeks,
481
                                weeks == 1 ? "week" : "weeks",
482
                                days,
483
                                days == 1 ? "day" : "days",
484
                                s);
485
        } else if (d >= 2*USEC_PER_DAY)
652✔
486
                (void) snprintf(buf, l, USEC_FMT " days%s", d / USEC_PER_DAY,s);
×
487
        else if (d >= 25*USEC_PER_HOUR)
652✔
488
                (void) snprintf(buf, l, "1 day " USEC_FMT "h%s",
11✔
489
                                (d - USEC_PER_DAY) / USEC_PER_HOUR, s);
11✔
490
        else if (d >= 6*USEC_PER_HOUR)
641✔
491
                (void) snprintf(buf, l, USEC_FMT "h%s",
23✔
492
                                d / USEC_PER_HOUR, s);
493
        else if (d >= USEC_PER_HOUR)
618✔
494
                (void) snprintf(buf, l, USEC_FMT "h " USEC_FMT "min%s",
4✔
495
                                d / USEC_PER_HOUR,
496
                                (d % USEC_PER_HOUR) / USEC_PER_MINUTE, s);
4✔
497
        else if (d >= 5*USEC_PER_MINUTE)
614✔
498
                (void) snprintf(buf, l, USEC_FMT "min%s",
8✔
499
                                d / USEC_PER_MINUTE, s);
500
        else if (d >= USEC_PER_MINUTE)
606✔
501
                (void) snprintf(buf, l, USEC_FMT "min " USEC_FMT "s%s",
1✔
502
                                d / USEC_PER_MINUTE,
503
                                (d % USEC_PER_MINUTE) / USEC_PER_SEC, s);
1✔
504
        else if (d >= USEC_PER_SEC)
605✔
505
                (void) snprintf(buf, l, USEC_FMT "s%s",
506✔
506
                                d / USEC_PER_SEC, s);
507
        else if (d >= USEC_PER_MSEC)
99✔
508
                (void) snprintf(buf, l, USEC_FMT "ms%s",
96✔
509
                                d / USEC_PER_MSEC, s);
510
        else if (d > 0)
3✔
511
                (void) snprintf(buf, l, USEC_FMT"us%s",
3✔
512
                                d, s);
513
        else
514
                (void) snprintf(buf, l, "now");
×
515

516
        buf[l-1] = 0;
1,415✔
517
        return buf;
1,415✔
518
}
519

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

536
        char *p = ASSERT_PTR(buf);
16,859✔
537
        bool something = false;
16,859✔
538

539
        assert(l > 0);
16,859✔
540

541
        if (t == USEC_INFINITY) {
16,859✔
542
                strncpy(p, "infinity", l-1);
1,744✔
543
                p[l-1] = 0;
1,744✔
544
                return p;
1,744✔
545
        }
546

547
        if (t <= 0) {
15,115✔
548
                strncpy(p, "0", l-1);
781✔
549
                p[l-1] = 0;
781✔
550
                return p;
781✔
551
        }
552

553
        /* The result of this function can be parsed with parse_sec */
554

555
        FOREACH_ELEMENT(i, table) {
119,721✔
556
                int k = 0;
119,626✔
557
                size_t n;
119,626✔
558
                bool done = false;
119,626✔
559
                usec_t a, b;
119,626✔
560

561
                if (t <= 0)
119,626✔
562
                        break;
563

564
                if (t < accuracy && something)
105,431✔
565
                        break;
566

567
                if (t < i->usec)
105,387✔
568
                        continue;
85,986✔
569

570
                if (l <= 1)
19,401✔
571
                        break;
572

573
                a = t / i->usec;
19,401✔
574
                b = t % i->usec;
19,401✔
575

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

580
                        for (usec_t cc = i->usec; cc > 1; cc /= 10)
62,120✔
581
                                j++;
51,030✔
582

583
                        for (usec_t cc = accuracy; cc > 1; cc /= 10) {
50,206✔
584
                                b /= 10;
39,116✔
585
                                j--;
39,116✔
586
                        }
587

588
                        if (j > 0) {
11,090✔
589
                                k = snprintf(p, l,
6,702✔
590
                                             "%s"USEC_FMT".%0*"PRI_USEC"%s",
591
                                             p > buf ? " " : "",
592
                                             a,
593
                                             j,
594
                                             b,
595
                                             i->suffix);
3,351✔
596

597
                                t = 0;
3,351✔
598
                                done = true;
3,351✔
599
                        }
600
                }
601

602
                /* No? Then let's show it normally */
603
                if (!done) {
3,351✔
604
                        k = snprintf(p, l,
32,100✔
605
                                     "%s"USEC_FMT"%s",
606
                                     p > buf ? " " : "",
607
                                     a,
608
                                     i->suffix);
16,050✔
609

610
                        t = b;
16,050✔
611
                }
612

613
                n = MIN((size_t) k, l-1);
19,401✔
614

615
                l -= n;
19,401✔
616
                p += n;
19,401✔
617

618
                something = true;
19,401✔
619
        }
620

621
        *p = 0;
14,334✔
622

623
        return buf;
14,334✔
624
}
625

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

631
        return empty_to_null(tzname[dst]);
2,994✔
632
}
633

634
int parse_gmtoff(const char *t, long *ret) {
1,230✔
635
        assert(t);
1,230✔
636

637
        struct tm tm;
1,230✔
638
        const char *k = strptime(t, "%z", &tm);
1,230✔
639
        if (k && *k == '\0') {
1,230✔
640
                /* Success! */
641
                if (ret)
172✔
642
                        *ret = tm.tm_gmtoff;
172✔
643
                return 0;
172✔
644
        }
645

646
#ifdef __GLIBC__
647
        return -EINVAL;
648
#else
649
        int r;
650

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

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

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

674
        usec_t u = r * 10 * USEC_PER_HOUR;
675

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

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

686
        if (*t == ':') /* skip colon */
687
                t++;
688

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

695
        u += r * 10 * USEC_PER_MINUTE;
696

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

702
        u += r * USEC_PER_MINUTE;
703

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

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

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

717
        return 0;
718
#endif
719
}
720

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

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

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

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

789
        assert(t);
2,126✔
790

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

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

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

801
                t = t_alloc;
802
                with_tz = true;
803
        }
804

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

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

817
        usec = now(CLOCK_REALTIME);
2,019✔
818

819
        if (!with_tz) {
2,019✔
820
                if (streq(t, "now"))
329✔
821
                        goto finish;
5✔
822

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

828
                        goto finish;
6✔
829
                }
830

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

836
                        goto finish;
6✔
837
                }
838

839
                if ((k = endswith(t, " ago"))) {
312✔
840
                        _cleanup_free_ char *buf = NULL;
4✔
841

842
                        buf = strndup(t, k - t);
4✔
843
                        if (!buf)
4✔
844
                                return -ENOMEM;
845

846
                        r = parse_sec(buf, &minus);
4✔
847
                        if (r < 0)
4✔
848
                                return r;
849

850
                        goto finish;
4✔
851
                }
852

853
                if ((k = endswith(t, " left"))) {
308✔
854
                        _cleanup_free_ char *buf = NULL;
101✔
855

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

860
                        r = parse_sec(buf, &plus);
101✔
861
                        if (r < 0)
101✔
862
                                return r;
863

864
                        goto finish;
101✔
865
                }
866
        }
867

868
        r = localtime_or_gmtime_usec(usec, utc, &tm);
1,897✔
869
        if (r < 0)
1,897✔
870
                return r;
871

872
        tm.tm_isdst = isdst;
1,897✔
873

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

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

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

889
        FOREACH_ELEMENT(day, day_nr) {
19,616✔
890
                k = startswith_no_case(t, day->name);
19,302✔
891
                if (!k || *k != ' ')
19,302✔
892
                        continue;
17,750✔
893

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

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

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

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

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

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

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

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

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

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

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

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

995
        return -EINVAL;
996

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

1005
from_tm:
419✔
1006
        assert(plus == 0);
1,829✔
1007
        assert(minus == 0);
1,829✔
1008

1009
        if (weekday >= 0 && tm.tm_wday != weekday)
1,829✔
1010
                return -EINVAL;
1011

1012
        if (gmtoff < 0) {
1,829✔
1013
                plus = -gmtoff * USEC_PER_SEC;
110✔
1014

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

1032
        r = mktime_or_timegm_usec(&tm, utc, &usec);
1,829✔
1033
        if (r < 0)
1,829✔
1034
                return r;
1035

1036
        usec = usec_add(usec, fractional);
3,652✔
1037

1038
finish:
1,948✔
1039
        usec = usec_add(usec, plus);
1,948✔
1040

1041
        if (usec < minus)
1,948✔
1042
                return -EINVAL;
1043

1044
        usec = usec_sub_unsigned(usec, minus);
1,947✔
1045

1046
        if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX)
1,947✔
1047
                return -EINVAL;
1048

1049
        if (ret)
1,946✔
1050
                *ret = usec;
1,944✔
1051
        return 0;
1052
}
1053

1054
int parse_timestamp(const char *t, usec_t *ret) {
2,093✔
1055
        long gmtoff;
2,093✔
1056
        int r;
2,093✔
1057

1058
        assert(t);
2,093✔
1059

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

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

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

1076
        size_t max_len = tz - t;
1,828✔
1077
        tz++;
1,828✔
1078

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

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

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

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

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

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

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

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

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

1155
        assert(p);
11,070✔
1156
        assert(ret);
11,070✔
1157

1158
        FOREACH_ELEMENT(i, table) {
191,897✔
1159
                const char *e = startswith(p, i->suffix);
187,930✔
1160
                if (e) {
187,930✔
1161
                        *ret = i->usec;
7,103✔
1162
                        return e;
7,103✔
1163
                }
1164
        }
1165

1166
        return p;
1167
}
1168

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

1172
        assert(t);
11,184✔
1173
        assert(default_unit > 0);
11,184✔
1174

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

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

1186
        usec_t usec = 0;
1187

1188
        for (bool something = false;;) {
11,054✔
1189
                usec_t multiplier = default_unit, k;
21,906✔
1190
                long long l;
21,906✔
1191
                char *e;
21,906✔
1192

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

1198
                        break;
10,809✔
1199
                }
1200

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

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

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

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

1224
                p = s;
11,061✔
1225

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

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

1233
                usec += k;
11,059✔
1234

1235
                something = true;
11,059✔
1236

1237
                if (*e == '.') {
11,059✔
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,809✔
1256
                *ret = usec;
10,807✔
1257
        return 0;
1258
}
1259

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

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

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

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

1275
        *ret = k == 0 ? USEC_INFINITY : k;
1,958✔
1276
        return r;
1,958✔
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
                const char *e = startswith(p, i->suffix);
803✔
1336
                if (e) {
803✔
1337
                        *ret = i->nsec;
44✔
1338
                        return e;
44✔
1339
                }
1340
        }
1341

1342
        return p;
1343
}
1344

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

1350
        assert(t);
48✔
1351
        assert(ret);
48✔
1352

1353
        p = t;
48✔
1354

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

1362
                *ret = NSEC_INFINITY;
2✔
1363
                return 0;
2✔
1364
        }
1365

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

1371
                p += strspn(p, WHITESPACE);
74✔
1372

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

1377
                        break;
20✔
1378
                }
1379

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

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

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

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

1403
                p = s;
36✔
1404

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

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

1412
                nsec += k;
35✔
1413

1414
                something = true;
35✔
1415

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

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

1425
                                nsec += k;
32✔
1426
                        }
1427

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

1434
        *ret = nsec;
20✔
1435

1436
        return 0;
20✔
1437
}
1438

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

1444
        assert(ret);
×
1445

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

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

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

1459
                const char *p = line;
×
1460

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

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

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

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

1480
        *ret = TAKE_PTR(zones);
×
1481
        return 0;
×
1482
}
1483

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

1489
        assert(ret);
4✔
1490

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

1495
        for (;;) {
17,196✔
1496
                _cleanup_free_ char *line = NULL, *type = NULL, *f1 = NULL, *f2 = NULL;
17,192✔
1497

1498
                r = read_line(f, LONG_LINE_MAX, &line);
17,196✔
1499
                if (r < 0)
17,196✔
1500
                        return r;
1501
                if (r == 0)
17,196✔
1502
                        break;
1503

1504
                const char *p = line;
17,192✔
1505

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

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

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

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

1536
        *ret = TAKE_PTR(zones);
4✔
1537
        return 0;
4✔
1538
}
1539

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

1544
        assert(ret);
4✔
1545

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

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

1561
        strv_sort_uniq(zones);
4✔
1562

1563
        *ret = TAKE_PTR(zones);
4✔
1564
        return 0;
4✔
1565
}
1566

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

1574
        if (isempty(name))
8,442✔
1575
                return -EINVAL;
1576

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

1581
        if (name[0] == '/')
4,212✔
1582
                return -EINVAL;
1583

1584
        for (p = name; *p; p++) {
59,340✔
1585
                if (!ascii_isdigit(*p) &&
55,274✔
1586
                    !ascii_isalpha(*p) &&
53,982✔
1587
                    !IN_SET(*p, '-', '_', '+', '/'))
4,795✔
1588
                        return -EINVAL;
1589

1590
                if (*p == '/') {
55,129✔
1591

1592
                        if (slash)
3,718✔
1593
                                return -EINVAL;
1594

1595
                        slash = true;
1596
                } else
1597
                        slash = false;
1598
        }
1599

1600
        if (slash)
4,066✔
1601
                return -EINVAL;
1602

1603
        if (p - name >= PATH_MAX)
4,065✔
1604
                return -ENAMETOOLONG;
1605

1606
        t = strjoina("/usr/share/zoneinfo/", name);
20,325✔
1607

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

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

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

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

1625
        return 0;
1626
}
1627

1628
void reset_timezonep(char **p) {
757✔
1629
        assert(p);
757✔
1630

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

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

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

1645
        return s;
1646
}
1647

1648
bool clock_supported(clockid_t clock) {
314,501✔
1649
        struct timespec ts;
314,501✔
1650

1651
        switch (clock) {
314,501✔
1652

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

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

1665
int get_timezone(char **ret) {
62✔
1666
        _cleanup_free_ char *t = NULL;
62✔
1667
        int r;
62✔
1668

1669
        assert(ret);
62✔
1670

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

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

1684
        return strdup_to(ret, e);
61✔
1685
}
1686

1687
const char* etc_localtime(void) {
1,020✔
1688
        static const char *cached = NULL;
1,020✔
1689

1690
        if (!cached)
1,020✔
1691
                cached = secure_getenv("SYSTEMD_ETC_LOCALTIME") ?: "/etc/localtime";
902✔
1692

1693
        return cached;
1,020✔
1694
}
1695

1696
int mktime_or_timegm_usec(
7,668✔
1697
                struct tm *tm, /* input + normalized output */
1698
                bool utc,
1699
                usec_t *ret) {
1700

1701
        time_t t;
7,668✔
1702

1703
        assert(tm);
7,668✔
1704

1705
        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,668✔
1706
                return -ERANGE;
1707
        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,665✔
1708
                return -ERANGE;
1709

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

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

1721
        if (ret)
7,663✔
1722
                *ret = (usec_t) t * USEC_PER_SEC;
2,164✔
1723
        return 0;
1724
}
1725

1726
int localtime_or_gmtime_usec(
130,610✔
1727
                usec_t t,
1728
                bool utc,
1729
                struct tm *ret) {
1730

1731
        t /= USEC_PER_SEC; /* Round down */
130,610✔
1732
        if (t > (usec_t) TIME_T_MAX)
130,610✔
1733
                return -ERANGE;
1734
        time_t sec = (time_t) t;
130,610✔
1735

1736
        struct tm buf = {};
130,610✔
1737
        if (!(utc ? gmtime_r(&sec, &buf) : localtime_r(&sec, &buf)))
130,610✔
1738
                return -EINVAL;
1739

1740
        if (ret)
130,610✔
1741
                *ret = buf;
130,610✔
1742

1743
        return 0;
1744
}
1745

1746
static uint32_t sysconf_clock_ticks_cached(void) {
732✔
1747
        static thread_local uint32_t hz = 0;
732✔
1748
        long r;
732✔
1749

1750
        if (hz == 0) {
732✔
1751
                r = sysconf(_SC_CLK_TCK);
70✔
1752

1753
                assert(r > 0);
70✔
1754
                hz = r;
70✔
1755
        }
1756

1757
        return hz;
732✔
1758
}
1759

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

1765
usec_t jiffies_to_usec(uint32_t j) {
723✔
1766
        uint32_t hz = sysconf_clock_ticks_cached();
723✔
1767
        return DIV_ROUND_UP(j * USEC_PER_SEC, hz);
723✔
1768
}
1769

1770
usec_t usec_shift_clock(usec_t x, clockid_t from, clockid_t to) {
737✔
1771
        usec_t a, b;
737✔
1772

1773
        if (x == USEC_INFINITY)
737✔
1774
                return USEC_INFINITY;
1775
        if (map_clock_id(from) == map_clock_id(to))
570✔
1776
                return x;
1777

1778
        a = now(from);
8✔
1779
        b = now(to);
8✔
1780

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

1789
bool in_utc_timezone(void) {
676✔
1790
        tzset();
676✔
1791

1792
        return timezone == 0 && daylight == 0;
676✔
1793
}
1794

1795
int usleep_safe(usec_t usec) {
365✔
1796
        int r;
365✔
1797

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

1804
        if (usec == 0)
365✔
1805
                return 0;
365✔
1806

1807
        if (usec == USEC_INFINITY)
365✔
1808
                return RET_NERRNO(pause());
×
1809

1810
        struct timespec t;
365✔
1811
        timespec_store(&t, usec);
365✔
1812

1813
        for (;;) {
365✔
1814
                struct timespec remaining;
365✔
1815

1816
                /* `clock_nanosleep()` does not use `errno`, but returns positive error codes. */
1817
                r = -clock_nanosleep(CLOCK_MONOTONIC, /* flags= */ 0, &t, &remaining);
365✔
1818
                if (r == -EINTR) {
365✔
1819
                        /* Interrupted. Continue sleeping for the remaining time. */
1820
                        t = remaining;
×
1821
                        continue;
×
1822
                }
1823

1824
                return r;
365✔
1825
        }
1826
}
1827

1828
int time_change_fd(void) {
481✔
1829

1830
        /* We only care for the cancellation event, hence we set the timeout to the latest possible value. */
1831
        static const struct itimerspec its = {
481✔
1832
                .it_value.tv_sec = TIME_T_MAX,
1833
        };
1834

1835
        _cleanup_close_ int fd = -EBADF;
962✔
1836

1837
        /* Uses TFD_TIMER_CANCEL_ON_SET to get notifications whenever CLOCK_REALTIME makes a jump relative to
1838
         * CLOCK_MONOTONIC. */
1839

1840
        fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
481✔
1841
        if (fd < 0)
481✔
1842
                return -errno;
×
1843

1844
        if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) >= 0)
481✔
1845
                return TAKE_FD(fd);
481✔
1846

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

1857
#if SIZEOF_TIME_T == 8 && ULONG_MAX < UINT64_MAX
1858
        if (ERRNO_IS_NOT_SUPPORTED(errno) || errno == EOVERFLOW) {
1859
                static const struct itimerspec its32 = {
1860
                        .it_value.tv_sec = INT32_MAX,
1861
                };
1862

1863
                if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its32, NULL) >= 0)
1864
                        return TAKE_FD(fd);
1865
        }
1866
#endif
1867

1868
        return -errno;
×
1869
}
1870

1871
static const char* const timestamp_style_table[_TIMESTAMP_STYLE_MAX] = {
1872
        [TIMESTAMP_PRETTY] = "pretty",
1873
        [TIMESTAMP_US]     = "us",
1874
        [TIMESTAMP_UTC]    = "utc",
1875
        [TIMESTAMP_US_UTC] = "us+utc",
1876
        [TIMESTAMP_UNIX]   = "unix",
1877
};
1878

1879
/* Use the macro for enum → string to allow for aliases */
1880
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(timestamp_style, TimestampStyle);
×
1881

1882
/* For the string → enum mapping we use the generic implementation, but also support two aliases */
1883
TimestampStyle timestamp_style_from_string(const char *s) {
8✔
1884
        TimestampStyle t;
8✔
1885

1886
        t = (TimestampStyle) string_table_lookup_from_string(timestamp_style_table, ELEMENTSOF(timestamp_style_table), s);
8✔
1887
        if (t >= 0)
8✔
1888
                return t;
1889
        if (STRPTR_IN_SET(s, "µs", "μs")) /* accept both µ symbols in unicode, i.e. micro symbol + Greek small letter mu. */
2✔
1890
                return TIMESTAMP_US;
1✔
1891
        if (STRPTR_IN_SET(s, "µs+utc", "μs+utc"))
1✔
1892
                return TIMESTAMP_US_UTC;
1✔
1893
        return t;
1894
}
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