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

systemd / systemd / 18081732282

28 Sep 2025 11:03AM UTC coverage: 72.034% (-0.05%) from 72.087%
18081732282

push

github

yuwata
po: Translated using Weblate (Khmer (Central))

Currently translated at 100.0% (264 of 264 strings)

Co-authored-by: kanitha chim <kchim@redhat.com>
Translate-URL: https://translate.fedoraproject.org/projects/systemd/main/km/
Translation: systemd/main

302506 of 419947 relevant lines covered (72.03%)

1101873.5 hits per line

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

90.51
/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 "io-util.h"
17
#include "log.h"
18
#include "parse-util.h"
19
#include "path-util.h"
20
#include "process-util.h"
21
#include "stat-util.h"
22
#include "stdio-util.h"
23
#include "string-table.h"
24
#include "string-util.h"
25
#include "strv.h"
26
#include "time-util.h"
27

28
static clockid_t map_clock_id(clockid_t c) {
29,085,639✔
29

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

35
        switch (c) {
29,085,639✔
36

37
        case CLOCK_BOOTTIME_ALARM:
38
                return CLOCK_BOOTTIME;
39

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

43
        default:
29,085,630✔
44
                return c;
29,085,630✔
45
        }
46
}
47

48
usec_t now(clockid_t clock_id) {
29,082,896✔
49
        struct timespec ts;
29,082,896✔
50

51
        assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
29,082,896✔
52

53
        return timespec_load(&ts);
29,082,896✔
54
}
55

56
nsec_t now_nsec(clockid_t clock_id) {
601✔
57
        struct timespec ts;
601✔
58

59
        assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
601✔
60

61
        return timespec_load_nsec(&ts);
601✔
62
}
63

64
dual_timestamp* dual_timestamp_now(dual_timestamp *ts) {
1,969,921✔
65
        assert(ts);
1,969,921✔
66

67
        ts->realtime = now(CLOCK_REALTIME);
1,969,921✔
68
        ts->monotonic = now(CLOCK_MONOTONIC);
1,969,921✔
69

70
        return ts;
1,969,921✔
71
}
72

73
triple_timestamp* triple_timestamp_now(triple_timestamp *ts) {
3,214,792✔
74
        assert(ts);
3,214,792✔
75

76
        ts->realtime = now(CLOCK_REALTIME);
3,214,792✔
77
        ts->monotonic = now(CLOCK_MONOTONIC);
3,214,792✔
78
        ts->boottime = now(CLOCK_BOOTTIME);
3,214,792✔
79

80
        return ts;
3,214,792✔
81
}
82

83
usec_t map_clock_usec_raw(usec_t from, usec_t from_base, usec_t to_base) {
8,693✔
84

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

92
        if (from >= from_base) { /* In the future */
8,693✔
93
                usec_t delta = from - from_base;
33✔
94

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

98
                return to_base + delta;
33✔
99

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

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

106
                return to_base - delta;
8,652✔
107
        }
108
}
109

110
usec_t map_clock_usec(usec_t from, clockid_t from_clock, clockid_t to_clock) {
557✔
111

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

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

121
        return map_clock_usec_raw(from, now(from_clock), now(to_clock));
523✔
122
}
123

124
dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) {
16✔
125
        assert(ts);
16✔
126

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

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

137
triple_timestamp* triple_timestamp_from_realtime(triple_timestamp *ts, usec_t u) {
337✔
138
        usec_t nowr;
337✔
139

140
        assert(ts);
337✔
141

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

147
        nowr = now(CLOCK_REALTIME);
337✔
148

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

153
        return ts;
337✔
154
}
155

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

159
        assert(ts);
×
160

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

166
        nowb = now(CLOCK_BOOTTIME);
×
167

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

172
        return ts;
×
173
}
174

175
dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) {
244✔
176
        assert(ts);
244✔
177

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

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

188
dual_timestamp* dual_timestamp_from_boottime(dual_timestamp *ts, usec_t u) {
507✔
189
        usec_t nowm;
507✔
190

191
        assert(ts);
507✔
192

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

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

204
usec_t triple_timestamp_by_clock(triple_timestamp *ts, clockid_t clock) {
3,446,415✔
205
        assert(ts);
3,446,415✔
206

207
        switch (clock) {
3,446,415✔
208

209
        case CLOCK_REALTIME:
1,891,792✔
210
        case CLOCK_REALTIME_ALARM:
211
                return ts->realtime;
1,891,792✔
212

213
        case CLOCK_MONOTONIC:
1,517,469✔
214
                return ts->monotonic;
1,517,469✔
215

216
        case CLOCK_BOOTTIME:
37,154✔
217
        case CLOCK_BOOTTIME_ALARM:
218
                return ts->boottime;
37,154✔
219

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

225
usec_t timespec_load(const struct timespec *ts) {
29,436,541✔
226
        assert(ts);
29,436,541✔
227

228
        if (ts->tv_sec < 0 || ts->tv_nsec < 0)
29,436,541✔
229
                return USEC_INFINITY;
230

231
        if ((usec_t) ts->tv_sec > (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC)
29,436,541✔
232
                return USEC_INFINITY;
233

234
        return
29,436,541✔
235
                (usec_t) ts->tv_sec * USEC_PER_SEC +
29,436,541✔
236
                (usec_t) ts->tv_nsec / NSEC_PER_USEC;
237
}
238

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

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

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

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

251
struct timespec *timespec_store(struct timespec *ts, usec_t u) {
2,187,152✔
252
        assert(ts);
2,187,152✔
253

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

261
        ts->tv_sec = (time_t) (u / USEC_PER_SEC);
2,187,152✔
262
        ts->tv_nsec = (long) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
2,187,152✔
263

264
        return ts;
2,187,152✔
265
}
266

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

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

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

280
        return ts;
56✔
281
}
282

283
usec_t timeval_load(const struct timeval *tv) {
785,440✔
284
        assert(tv);
785,440✔
285

286
        if (tv->tv_sec < 0 || tv->tv_usec < 0)
785,440✔
287
                return USEC_INFINITY;
288

289
        if ((usec_t) tv->tv_sec > (UINT64_MAX - tv->tv_usec) / USEC_PER_SEC)
785,440✔
290
                return USEC_INFINITY;
291

292
        return
785,440✔
293
                (usec_t) tv->tv_sec * USEC_PER_SEC +
785,440✔
294
                (usec_t) tv->tv_usec;
295
}
296

297
struct timeval *timeval_store(struct timeval *tv, usec_t u) {
164,452✔
298
        assert(tv);
164,452✔
299

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

309
        return tv;
164,452✔
310
}
311

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

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

330
        struct tm tm;
5,820✔
331
        bool utc, us;
5,820✔
332
        size_t n;
5,820✔
333

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

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

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

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

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

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

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

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

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

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

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

386
                return buf;
104✔
387
        }
388

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

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

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

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

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

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

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

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

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

435
        return buf;
436
}
437

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

442
        assert(buf);
1,406✔
443

444
        if (!timestamp_is_set(t))
1,406✔
445
                return NULL;
446

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

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

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

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

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

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

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

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

540
        assert(l > 0);
14,269✔
541

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

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

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

556
        FOREACH_ELEMENT(i, table) {
106,088✔
557
                int k = 0;
105,995✔
558
                size_t n;
105,995✔
559
                bool done = false;
105,995✔
560
                usec_t a, b;
105,995✔
561

562
                if (t <= 0)
105,995✔
563
                        break;
564

565
                if (t < accuracy && something)
93,370✔
566
                        break;
567

568
                if (t < i->usec)
93,331✔
569
                        continue;
75,741✔
570

571
                if (l <= 1)
17,590✔
572
                        break;
573

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

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

581
                        for (usec_t cc = i->usec; cc > 1; cc /= 10)
57,983✔
582
                                j++;
47,823✔
583

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

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

598
                                t = 0;
3,167✔
599
                                done = true;
3,167✔
600
                        }
601
                }
602

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

611
                        t = b;
14,423✔
612
                }
613

614
                n = MIN((size_t) k, l-1);
17,590✔
615

616
                l -= n;
17,590✔
617
                p += n;
17,590✔
618

619
                something = true;
17,590✔
620
        }
621

622
        *p = 0;
12,757✔
623

624
        return buf;
12,757✔
625
}
626

627
int parse_gmtoff(const char *t, long *ret) {
1,231✔
628
        assert(t);
1,231✔
629

630
        struct tm tm;
1,231✔
631
        const char *k = strptime(t, "%z", &tm);
1,231✔
632
        if (!k || *k != '\0')
1,231✔
633
                return -EINVAL;
1,231✔
634

635
        if (ret)
172✔
636
                *ret = tm.tm_gmtoff;
172✔
637
        return 0;
638
}
639

640
static int parse_timestamp_impl(
2,127✔
641
                const char *t,
642
                size_t max_len,
643
                bool utc,
644
                int isdst,
645
                long gmtoff,
646
                usec_t *ret) {
647

648
        static const struct {
2,127✔
649
                const char *name;
650
                const int nr;
651
        } day_nr[] = {
652
                { "Sunday",    0 },
653
                { "Sun",       0 },
654
                { "Monday",    1 },
655
                { "Mon",       1 },
656
                { "Tuesday",   2 },
657
                { "Tue",       2 },
658
                { "Wednesday", 3 },
659
                { "Wed",       3 },
660
                { "Thursday",  4 },
661
                { "Thu",       4 },
662
                { "Friday",    5 },
663
                { "Fri",       5 },
664
                { "Saturday",  6 },
665
                { "Sat",       6 },
666
        };
667

668
        _cleanup_free_ char *t_alloc = NULL;
2,127✔
669
        usec_t usec, plus = 0, minus = 0;
2,127✔
670
        bool with_tz = false;
2,127✔
671
        int r, weekday = -1;
2,127✔
672
        unsigned fractional = 0;
2,127✔
673
        const char *k;
2,127✔
674
        struct tm tm, copy;
2,127✔
675

676
        /* Allowed syntaxes:
677
         *
678
         *   2012-09-22 16:34:22.1[2[3[4[5[6]]]]]
679
         *   2012-09-22 16:34:22  (µsec will be set to 0)
680
         *   2012-09-22 16:34     (seconds will be set to 0)
681
         *   2012-09-22T16:34:22.1[2[3[4[5[6]]]]]
682
         *   2012-09-22T16:34:22  (µsec will be set to 0)
683
         *   2012-09-22T16:34     (seconds will be set to 0)
684
         *   2012-09-22           (time will be set to 00:00:00)
685
         *   16:34:22             (date will be set to today)
686
         *   16:34                (date will be set to today, seconds to 0)
687
         *   now
688
         *   yesterday            (time is set to 00:00:00)
689
         *   today                (time is set to 00:00:00)
690
         *   tomorrow             (time is set to 00:00:00)
691
         *   +5min
692
         *   -5days
693
         *   @2147483647          (seconds since epoch)
694
         *
695
         * Note, on DST change, 00:00:00 may not exist and in that case the time part may be shifted.
696
         * E.g. "Sun 2023-03-13 America/Havana" is parsed as "Sun 2023-03-13 01:00:00 CDT".
697
         *
698
         * A simplified strptime-spelled RFC3339 ABNF looks like
699
         *   "%Y-%m-%d" "T" "%H" ":" "%M" ":" "%S" [".%N"] ("Z" / (("+" / "-") "%H:%M"))
700
         * We additionally allow no seconds and inherited timezone
701
         * for symmetry with our other syntaxes and improved interactive usability:
702
         *   "%Y-%m-%d" "T" "%H" ":" "%M" ":" ["%S" [".%N"]] ["Z" / (("+" / "-") "%H:%M")]
703
         * RFC3339 defines time-secfrac to as "." 1*DIGIT, but we limit to 6 digits,
704
         * since we're limited to 1µs resolution.
705
         * We also accept "Sat 2012-09-22T16:34:22", RFC3339 warns against it.
706
         */
707

708
        assert(t);
2,127✔
709

710
        if (max_len != SIZE_MAX) {
2,127✔
711
                /* If the input string contains timezone, then cut it here. */
712

713
                if (max_len == 0) /* Can't be the only field */
1,690✔
714
                        return -EINVAL;
715

716
                t_alloc = strndup(t, max_len);
1,690✔
717
                if (!t_alloc)
1,690✔
718
                        return -ENOMEM;
719

720
                t = t_alloc;
721
                with_tz = true;
722
        }
723

724
        if (utc) {
2,127✔
725
                /* glibc accepts gmtoff more than 24 hours, but we refuse it. */
726
                if ((usec_t) labs(gmtoff) * USEC_PER_SEC > USEC_PER_DAY)
991✔
727
                        return -EINVAL;
728
        } else {
729
                if (gmtoff != 0)
1,136✔
730
                        return -EINVAL;
731
        }
732

733
        if (t[0] == '@' && !with_tz)
2,127✔
734
                return parse_sec(t + 1, ret);
107✔
735

736
        usec = now(CLOCK_REALTIME);
2,020✔
737

738
        if (!with_tz) {
2,020✔
739
                if (streq(t, "now"))
330✔
740
                        goto finish;
5✔
741

742
                if (t[0] == '+') {
325✔
743
                        r = parse_sec(t+1, &plus);
6✔
744
                        if (r < 0)
6✔
745
                                return r;
746

747
                        goto finish;
6✔
748
                }
749

750
                if (t[0] == '-') {
319✔
751
                        r = parse_sec(t+1, &minus);
6✔
752
                        if (r < 0)
6✔
753
                                return r;
754

755
                        goto finish;
6✔
756
                }
757

758
                if ((k = endswith(t, " ago"))) {
313✔
759
                        _cleanup_free_ char *buf = NULL;
4✔
760

761
                        buf = strndup(t, k - t);
4✔
762
                        if (!buf)
4✔
763
                                return -ENOMEM;
764

765
                        r = parse_sec(buf, &minus);
4✔
766
                        if (r < 0)
4✔
767
                                return r;
768

769
                        goto finish;
4✔
770
                }
771

772
                if ((k = endswith(t, " left"))) {
309✔
773
                        _cleanup_free_ char *buf = NULL;
101✔
774

775
                        buf = strndup(t, k - t);
101✔
776
                        if (!buf)
101✔
777
                                return -ENOMEM;
778

779
                        r = parse_sec(buf, &plus);
101✔
780
                        if (r < 0)
101✔
781
                                return r;
782

783
                        goto finish;
101✔
784
                }
785
        }
786

787
        r = localtime_or_gmtime_usec(usec, utc, &tm);
1,898✔
788
        if (r < 0)
1,898✔
789
                return r;
790

791
        tm.tm_isdst = isdst;
1,898✔
792

793
        if (streq(t, "today")) {
1,898✔
794
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
13✔
795
                goto from_tm;
13✔
796

797
        } else if (streq(t, "yesterday")) {
1,885✔
798
                tm.tm_mday--;
10✔
799
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
10✔
800
                goto from_tm;
10✔
801

802
        } else if (streq(t, "tomorrow")) {
1,875✔
803
                tm.tm_mday++;
8✔
804
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
8✔
805
                goto from_tm;
8✔
806
        }
807

808
        FOREACH_ELEMENT(day, day_nr) {
19,333✔
809
                k = startswith_no_case(t, day->name);
19,018✔
810
                if (!k || *k != ' ')
19,018✔
811
                        continue;
17,466✔
812

813
                weekday = day->nr;
1,552✔
814
                t = k + 1;
1,552✔
815
                break;
1,552✔
816
        }
817

818
        copy = tm;
1,867✔
819
        k = strptime(t, "%y-%m-%d %H:%M:%S", &tm);
1,867✔
820
        if (k) {
1,867✔
821
                if (*k == '.')
100✔
822
                        goto parse_usec;
64✔
823
                else if (*k == 0)
36✔
824
                        goto from_tm;
32✔
825
        }
826

827
        /* Our "canonical" RFC3339 syntax variant */
828
        tm = copy;
1,771✔
829
        k = strptime(t, "%Y-%m-%d %H:%M:%S", &tm);
1,771✔
830
        if (k) {
1,771✔
831
                if (*k == '.')
1,471✔
832
                        goto parse_usec;
342✔
833
                else if (*k == 0)
1,129✔
834
                        goto from_tm;
1,120✔
835
        }
836

837
        /* RFC3339 syntax */
838
        tm = copy;
309✔
839
        k = strptime(t, "%Y-%m-%dT%H:%M:%S", &tm);
309✔
840
        if (k) {
309✔
841
                if (*k == '.')
42✔
842
                        goto parse_usec;
20✔
843
                else if (*k == 0)
22✔
844
                        goto from_tm;
22✔
845
        }
846

847
        /* Support OUTPUT_SHORT and OUTPUT_SHORT_PRECISE formats */
848
        tm = copy;
267✔
849
        k = strptime(t, "%b %d %H:%M:%S", &tm);
267✔
850
        if (k) {
267✔
851
                if (*k == '.')
4✔
852
                        goto parse_usec;
2✔
853
                else if (*k == 0)
2✔
854
                        goto from_tm;
2✔
855
        }
856

857
        tm = copy;
263✔
858
        k = strptime(t, "%y-%m-%d %H:%M", &tm);
263✔
859
        if (k && *k == 0) {
263✔
860
                tm.tm_sec = 0;
30✔
861
                goto from_tm;
30✔
862
        }
863

864
        /* Our "canonical" RFC3339 syntax variant without seconds */
865
        tm = copy;
233✔
866
        k = strptime(t, "%Y-%m-%d %H:%M", &tm);
233✔
867
        if (k && *k == 0) {
233✔
868
                tm.tm_sec = 0;
31✔
869
                goto from_tm;
31✔
870
        }
871

872
        /* RFC3339 syntax without seconds */
873
        tm = copy;
202✔
874
        k = strptime(t, "%Y-%m-%dT%H:%M", &tm);
202✔
875
        if (k && *k == 0) {
202✔
876
                tm.tm_sec = 0;
8✔
877
                goto from_tm;
8✔
878
        }
879

880
        tm = copy;
194✔
881
        k = strptime(t, "%y-%m-%d", &tm);
194✔
882
        if (k && *k == 0) {
194✔
883
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
×
884
                goto from_tm;
×
885
        }
886

887
        tm = copy;
194✔
888
        k = strptime(t, "%Y-%m-%d", &tm);
194✔
889
        if (k && *k == 0) {
194✔
890
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
106✔
891
                goto from_tm;
106✔
892
        }
893

894
        tm = copy;
88✔
895
        k = strptime(t, "%H:%M:%S", &tm);
88✔
896
        if (k) {
88✔
897
                if (*k == '.')
30✔
898
                        goto parse_usec;
8✔
899
                else if (*k == 0)
22✔
900
                        goto from_tm;
22✔
901
        }
902

903
        tm = copy;
58✔
904
        k = strptime(t, "%H:%M", &tm);
58✔
905
        if (k && *k == 0) {
58✔
906
                tm.tm_sec = 0;
6✔
907
                goto from_tm;
6✔
908
        }
909

910
        return -EINVAL;
911

912
parse_usec:
436✔
913
        k++;
436✔
914
        r = parse_fractional_part_u(&k, 6, &fractional);
436✔
915
        if (r < 0)
436✔
916
                return -EINVAL;
917
        if (*k != '\0')
436✔
918
                return -EINVAL;
919

920
from_tm:
420✔
921
        assert(plus == 0);
1,830✔
922
        assert(minus == 0);
1,830✔
923

924
        if (weekday >= 0 && tm.tm_wday != weekday)
1,830✔
925
                return -EINVAL;
926

927
        if (gmtoff < 0) {
1,830✔
928
                plus = -gmtoff * USEC_PER_SEC;
110✔
929

930
                /* If gmtoff is negative, the string may be too old to be parsed as UTC.
931
                 * E.g. 1969-12-31 23:00:00 -06 == 1970-01-01 05:00:00 UTC
932
                 * We assumed that gmtoff is in the range of -24:00…+24:00, hence the only date we need to
933
                 * handle here is 1969-12-31. So, let's shift the date with one day, then subtract the shift
934
                 * later. */
935
                if (tm.tm_year == 69 && tm.tm_mon == 11 && tm.tm_mday == 31) {
110✔
936
                        /* Thu 1970-01-01-00:00:00 */
937
                        tm.tm_year = 70;
96✔
938
                        tm.tm_mon = 0;
96✔
939
                        tm.tm_mday = 1;
96✔
940
                        tm.tm_wday = 4;
96✔
941
                        tm.tm_yday = 0;
96✔
942
                        minus = USEC_PER_DAY;
96✔
943
                }
944
        } else
945
                minus = gmtoff * USEC_PER_SEC;
1,720✔
946

947
        r = mktime_or_timegm_usec(&tm, utc, &usec);
1,830✔
948
        if (r < 0)
1,830✔
949
                return r;
950

951
        usec = usec_add(usec, fractional);
3,654✔
952

953
finish:
1,949✔
954
        usec = usec_add(usec, plus);
1,949✔
955

956
        if (usec < minus)
1,949✔
957
                return -EINVAL;
958

959
        usec = usec_sub_unsigned(usec, minus);
1,948✔
960

961
        if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX)
1,948✔
962
                return -EINVAL;
963

964
        if (ret)
1,947✔
965
                *ret = usec;
1,945✔
966
        return 0;
967
}
968

969
int parse_timestamp(const char *t, usec_t *ret) {
2,094✔
970
        long gmtoff;
2,094✔
971
        int r;
2,094✔
972

973
        assert(t);
2,094✔
974

975
        size_t t_len = strlen(t);
2,094✔
976
        if (t_len > 2 && t[t_len - 1] == 'Z') {
2,094✔
977
                /* Try to parse as RFC3339-style welded UTC: "1985-04-12T23:20:50.52Z" */
978
                r = parse_timestamp_impl(t, t_len - 1, /* utc = */ true, /* isdst = */ -1, /* gmtoff = */ 0, ret);
67✔
979
                if (r >= 0)
67✔
980
                        return r;
2,094✔
981
        }
982

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

987
        const char *tz = strrchr(t, ' ');
2,020✔
988
        if (!tz)
2,020✔
989
                return parse_timestamp_impl(t, /* max_len = */ SIZE_MAX, /* utc = */ false, /* isdst = */ -1, /* gmtoff = */ 0, ret);
191✔
990

991
        size_t max_len = tz - t;
1,829✔
992
        tz++;
1,829✔
993

994
        /* Shortcut, parse the string as UTC. */
995
        if (streq(tz, "UTC"))
1,829✔
996
                return parse_timestamp_impl(t, max_len, /* utc = */ true, /* isdst = */ -1, /* gmtoff = */ 0, ret);
758✔
997

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

1004
        /* Check if the last word matches tzname[] of the local timezone. Note, this must be done earlier
1005
         * than the check by timezone_is_valid() below, as some short timezone specifications have their own
1006
         * timezone files (e.g. WET has its own timezone file, but JST does not), but using such files does
1007
         * not follow the timezone change in the current area. */
1008
        tzset();
945✔
1009
        for (int j = 0; j <= 1; j++) {
2,835✔
1010
                if (isempty(tzname[j]))
1,890✔
1011
                        continue;
×
1012

1013
                if (!streq(tz, tzname[j]))
1,890✔
1014
                        continue;
1,890✔
1015

1016
                /* The specified timezone matches tzname[] of the local timezone. */
1017
                return parse_timestamp_impl(t, max_len, /* utc = */ false, /* isdst = */ j, /* gmtoff = */ 0, ret);
×
1018
        }
1019

1020
        /* If the last word is a valid timezone file (e.g. Asia/Tokyo), then save the current timezone, apply
1021
         * the specified timezone, and parse the remaining string as a local time. */
1022
        if (timezone_is_valid(tz, LOG_DEBUG)) {
945✔
1023
                SAVE_TIMEZONE;
1,398✔
1024

1025
                if (setenv("TZ", strjoina(":", tz), /* overwrite = */ true) < 0)
3,495✔
1026
                        return negative_errno();
×
1027

1028
                return parse_timestamp_impl(t, max_len, /* utc = */ false, /* isdst = */ -1, /* gmtoff = */ 0, ret);
699✔
1029
        }
1030

1031
        /* Otherwise, assume that the last word is a part of the time and try to parse the whole string as a
1032
         * local time. */
1033
        return parse_timestamp_impl(t, SIZE_MAX, /* utc = */ false, /* isdst = */ -1, /* gmtoff = */ 0, ret);
246✔
1034
}
1035

1036
static const char* extract_multiplier(const char *p, usec_t *ret) {
10,043✔
1037
        static const struct {
10,043✔
1038
                const char *suffix;
1039
                usec_t usec;
1040
        } table[] = {
1041
                { "seconds", USEC_PER_SEC    },
1042
                { "second",  USEC_PER_SEC    },
1043
                { "sec",     USEC_PER_SEC    },
1044
                { "s",       USEC_PER_SEC    },
1045
                { "minutes", USEC_PER_MINUTE },
1046
                { "minute",  USEC_PER_MINUTE },
1047
                { "min",     USEC_PER_MINUTE },
1048
                { "months",  USEC_PER_MONTH  },
1049
                { "month",   USEC_PER_MONTH  },
1050
                { "M",       USEC_PER_MONTH  },
1051
                { "msec",    USEC_PER_MSEC   },
1052
                { "ms",      USEC_PER_MSEC   },
1053
                { "m",       USEC_PER_MINUTE },
1054
                { "hours",   USEC_PER_HOUR   },
1055
                { "hour",    USEC_PER_HOUR   },
1056
                { "hr",      USEC_PER_HOUR   },
1057
                { "h",       USEC_PER_HOUR   },
1058
                { "days",    USEC_PER_DAY    },
1059
                { "day",     USEC_PER_DAY    },
1060
                { "d",       USEC_PER_DAY    },
1061
                { "weeks",   USEC_PER_WEEK   },
1062
                { "week",    USEC_PER_WEEK   },
1063
                { "w",       USEC_PER_WEEK   },
1064
                { "years",   USEC_PER_YEAR   },
1065
                { "year",    USEC_PER_YEAR   },
1066
                { "y",       USEC_PER_YEAR   },
1067
                { "usec",    1ULL            },
1068
                { "us",      1ULL            },
1069
                { "μs",      1ULL            }, /* U+03bc (aka GREEK SMALL LETTER MU) */
1070
                { "µs",      1ULL            }, /* U+b5 (aka MICRO SIGN) */
1071
        };
1072

1073
        assert(p);
10,043✔
1074
        assert(ret);
10,043✔
1075

1076
        FOREACH_ELEMENT(i, table) {
173,571✔
1077
                char *e;
170,045✔
1078

1079
                e = startswith(p, i->suffix);
170,045✔
1080
                if (e) {
170,045✔
1081
                        *ret = i->usec;
6,517✔
1082
                        return e;
6,517✔
1083
                }
1084
        }
1085

1086
        return p;
1087
}
1088

1089
int parse_time(const char *t, usec_t *ret, usec_t default_unit) {
10,131✔
1090
        const char *p, *s;
10,131✔
1091

1092
        assert(t);
10,131✔
1093
        assert(default_unit > 0);
10,131✔
1094

1095
        p = skip_leading_chars(t, /* bad = */ NULL);
10,131✔
1096
        s = startswith(p, "infinity");
10,131✔
1097
        if (s) {
10,131✔
1098
                if (!in_charset(s, WHITESPACE))
305✔
1099
                        return -EINVAL;
1100

1101
                if (ret)
304✔
1102
                        *ret = USEC_INFINITY;
304✔
1103
                return 0;
304✔
1104
        }
1105

1106
        usec_t usec = 0;
1107

1108
        for (bool something = false;;) {
10,027✔
1109
                usec_t multiplier = default_unit, k;
19,853✔
1110
                long long l;
19,853✔
1111
                char *e;
19,853✔
1112

1113
                p = skip_leading_chars(p, /* bad = */ NULL);
19,853✔
1114
                if (*p == 0) {
19,853✔
1115
                        if (!something)
9,788✔
1116
                                return -EINVAL;
43✔
1117

1118
                        break;
9,783✔
1119
                }
1120

1121
                if (*p == '-') /* Don't allow "-0" */
10,065✔
1122
                        return -ERANGE;
1123

1124
                errno = 0;
10,057✔
1125
                l = strtoll(p, &e, 10);
10,057✔
1126
                if (errno > 0)
10,057✔
1127
                        return -errno;
×
1128
                if (l < 0)
10,057✔
1129
                        return -ERANGE;
1130

1131
                if (*e == '.') {
10,057✔
1132
                        p = e + 1;
55✔
1133
                        p += strspn(p, DIGITS);
55✔
1134
                } else if (e == p)
10,002✔
1135
                        return -EINVAL;
1136
                else
1137
                        p = e;
1138

1139
                s = extract_multiplier(p + strspn(p, WHITESPACE), &multiplier);
10,043✔
1140
                if (s == p && *s != '\0')
10,043✔
1141
                        /* Don't allow '12.34.56', but accept '12.34 .56' or '12.34s.56' */
1142
                        return -EINVAL;
1143

1144
                p = s;
10,034✔
1145

1146
                if ((usec_t) l >= USEC_INFINITY / multiplier)
10,034✔
1147
                        return -ERANGE;
1148

1149
                k = (usec_t) l * multiplier;
10,032✔
1150
                if (k >= USEC_INFINITY - usec)
10,032✔
1151
                        return -ERANGE;
1152

1153
                usec += k;
10,032✔
1154

1155
                something = true;
10,032✔
1156

1157
                if (*e == '.') {
10,032✔
1158
                        usec_t m = multiplier / 10;
48✔
1159
                        const char *b;
48✔
1160

1161
                        for (b = e + 1; *b >= '0' && *b <= '9'; b++, m /= 10) {
188✔
1162
                                k = (usec_t) (*b - '0') * m;
140✔
1163
                                if (k >= USEC_INFINITY - usec)
140✔
1164
                                        return -ERANGE;
1165

1166
                                usec += k;
140✔
1167
                        }
1168

1169
                        /* Don't allow "0.-0", "3.+1", "3. 1", "3.sec" or "3.hoge" */
1170
                        if (b == e + 1)
48✔
1171
                                return -EINVAL;
1172
                }
1173
        }
1174

1175
        if (ret)
9,783✔
1176
                *ret = usec;
9,781✔
1177
        return 0;
1178
}
1179

1180
int parse_sec(const char *t, usec_t *ret) {
10,087✔
1181
        return parse_time(t, ret, USEC_PER_SEC);
10,087✔
1182
}
1183

1184
int parse_sec_fix_0(const char *t, usec_t *ret) {
1,777✔
1185
        usec_t k;
1,777✔
1186
        int r;
1,777✔
1187

1188
        assert(t);
1,777✔
1189
        assert(ret);
1,777✔
1190

1191
        r = parse_sec(t, &k);
1,777✔
1192
        if (r < 0)
1,777✔
1193
                return r;
1,777✔
1194

1195
        *ret = k == 0 ? USEC_INFINITY : k;
1,777✔
1196
        return r;
1,777✔
1197
}
1198

1199
int parse_sec_def_infinity(const char *t, usec_t *ret) {
7✔
1200
        assert(t);
7✔
1201
        assert(ret);
7✔
1202

1203
        t += strspn(t, WHITESPACE);
7✔
1204
        if (isempty(t)) {
7✔
1205
                *ret = USEC_INFINITY;
2✔
1206
                return 0;
2✔
1207
        }
1208
        return parse_sec(t, ret);
5✔
1209
}
1210

1211
static const char* extract_nsec_multiplier(const char *p, nsec_t *ret) {
44✔
1212
        static const struct {
44✔
1213
                const char *suffix;
1214
                nsec_t nsec;
1215
        } table[] = {
1216
                { "seconds", NSEC_PER_SEC    },
1217
                { "second",  NSEC_PER_SEC    },
1218
                { "sec",     NSEC_PER_SEC    },
1219
                { "s",       NSEC_PER_SEC    },
1220
                { "minutes", NSEC_PER_MINUTE },
1221
                { "minute",  NSEC_PER_MINUTE },
1222
                { "min",     NSEC_PER_MINUTE },
1223
                { "months",  NSEC_PER_MONTH  },
1224
                { "month",   NSEC_PER_MONTH  },
1225
                { "M",       NSEC_PER_MONTH  },
1226
                { "msec",    NSEC_PER_MSEC   },
1227
                { "ms",      NSEC_PER_MSEC   },
1228
                { "m",       NSEC_PER_MINUTE },
1229
                { "hours",   NSEC_PER_HOUR   },
1230
                { "hour",    NSEC_PER_HOUR   },
1231
                { "hr",      NSEC_PER_HOUR   },
1232
                { "h",       NSEC_PER_HOUR   },
1233
                { "days",    NSEC_PER_DAY    },
1234
                { "day",     NSEC_PER_DAY    },
1235
                { "d",       NSEC_PER_DAY    },
1236
                { "weeks",   NSEC_PER_WEEK   },
1237
                { "week",    NSEC_PER_WEEK   },
1238
                { "w",       NSEC_PER_WEEK   },
1239
                { "years",   NSEC_PER_YEAR   },
1240
                { "year",    NSEC_PER_YEAR   },
1241
                { "y",       NSEC_PER_YEAR   },
1242
                { "usec",    NSEC_PER_USEC   },
1243
                { "us",      NSEC_PER_USEC   },
1244
                { "μs",      NSEC_PER_USEC   }, /* U+03bc (aka GREEK LETTER MU) */
1245
                { "µs",      NSEC_PER_USEC   }, /* U+b5 (aka MICRO SIGN) */
1246
                { "nsec",    1ULL            },
1247
                { "ns",      1ULL            },
1248
                { "",        1ULL            }, /* default is nsec */
1249
        };
1250

1251
        assert(p);
44✔
1252
        assert(ret);
44✔
1253

1254
        FOREACH_ELEMENT(i, table) {
803✔
1255
                char *e;
803✔
1256

1257
                e = startswith(p, i->suffix);
803✔
1258
                if (e) {
803✔
1259
                        *ret = i->nsec;
44✔
1260
                        return e;
44✔
1261
                }
1262
        }
1263

1264
        return p;
1265
}
1266

1267
int parse_nsec(const char *t, nsec_t *ret) {
48✔
1268
        const char *p, *s;
48✔
1269
        nsec_t nsec = 0;
48✔
1270
        bool something = false;
48✔
1271

1272
        assert(t);
48✔
1273
        assert(ret);
48✔
1274

1275
        p = t;
48✔
1276

1277
        p += strspn(p, WHITESPACE);
48✔
1278
        s = startswith(p, "infinity");
48✔
1279
        if (s) {
48✔
1280
                s += strspn(s, WHITESPACE);
4✔
1281
                if (*s != 0)
4✔
1282
                        return -EINVAL;
1283

1284
                *ret = NSEC_INFINITY;
2✔
1285
                return 0;
2✔
1286
        }
1287

1288
        for (;;) {
30✔
1289
                nsec_t multiplier = 1, k;
74✔
1290
                long long l;
74✔
1291
                char *e;
74✔
1292

1293
                p += strspn(p, WHITESPACE);
74✔
1294

1295
                if (*p == 0) {
74✔
1296
                        if (!something)
21✔
1297
                                return -EINVAL;
24✔
1298

1299
                        break;
20✔
1300
                }
1301

1302
                if (*p == '-') /* Don't allow "-0" */
53✔
1303
                        return -ERANGE;
1304

1305
                errno = 0;
48✔
1306
                l = strtoll(p, &e, 10);
48✔
1307
                if (errno > 0)
48✔
1308
                        return -errno;
×
1309
                if (l < 0)
48✔
1310
                        return -ERANGE;
1311

1312
                if (*e == '.') {
48✔
1313
                        p = e + 1;
31✔
1314
                        p += strspn(p, DIGITS);
31✔
1315
                } else if (e == p)
17✔
1316
                        return -EINVAL;
1317
                else
1318
                        p = e;
1319

1320
                s = extract_nsec_multiplier(p + strspn(p, WHITESPACE), &multiplier);
44✔
1321
                if (s == p && *s != '\0')
44✔
1322
                        /* Don't allow '12.34.56', but accept '12.34 .56' or '12.34s.56' */
1323
                        return -EINVAL;
1324

1325
                p = s;
36✔
1326

1327
                if ((nsec_t) l >= NSEC_INFINITY / multiplier)
36✔
1328
                        return -ERANGE;
1329

1330
                k = (nsec_t) l * multiplier;
35✔
1331
                if (k >= NSEC_INFINITY - nsec)
35✔
1332
                        return -ERANGE;
1333

1334
                nsec += k;
35✔
1335

1336
                something = true;
35✔
1337

1338
                if (*e == '.') {
35✔
1339
                        nsec_t m = multiplier / 10;
24✔
1340
                        const char *b;
24✔
1341

1342
                        for (b = e + 1; *b >= '0' && *b <= '9'; b++, m /= 10) {
56✔
1343
                                k = (nsec_t) (*b - '0') * m;
32✔
1344
                                if (k >= NSEC_INFINITY - nsec)
32✔
1345
                                        return -ERANGE;
1346

1347
                                nsec += k;
32✔
1348
                        }
1349

1350
                        /* Don't allow "0.-0", "3.+1", "3. 1", "3.sec" or "3.hoge" */
1351
                        if (b == e + 1)
24✔
1352
                                return -EINVAL;
1353
                }
1354
        }
1355

1356
        *ret = nsec;
20✔
1357

1358
        return 0;
20✔
1359
}
1360

1361
static int get_timezones_from_zone1970_tab(char ***ret) {
×
1362
        _cleanup_fclose_ FILE *f = NULL;
×
1363
        _cleanup_strv_free_ char **zones = NULL;
×
1364
        int r;
×
1365

1366
        assert(ret);
×
1367

1368
        f = fopen("/usr/share/zoneinfo/zone1970.tab", "re");
×
1369
        if (!f)
×
1370
                return -errno;
×
1371

1372
        for (;;) {
×
1373
                _cleanup_free_ char *line = NULL, *cc = NULL, *co = NULL, *tz = NULL;
×
1374

1375
                r = read_line(f, LONG_LINE_MAX, &line);
×
1376
                if (r < 0)
×
1377
                        return r;
1378
                if (r == 0)
×
1379
                        break;
1380

1381
                const char *p = line;
×
1382

1383
                /* Line format is:
1384
                 * 'country codes' 'coordinates' 'timezone' 'comments' */
1385
                r = extract_many_words(&p, NULL, 0, &cc, &co, &tz);
×
1386
                if (r < 0)
×
1387
                        continue;
×
1388

1389
                /* Lines that start with # are comments. */
1390
                if (*cc == '#')
×
1391
                        continue;
×
1392

1393
                if (!timezone_is_valid(tz, LOG_DEBUG))
×
1394
                        /* Don't list unusable timezones. */
1395
                        continue;
×
1396

1397
                r = strv_extend(&zones, tz);
×
1398
                if (r < 0)
×
1399
                        return r;
1400
        }
1401

1402
        *ret = TAKE_PTR(zones);
×
1403
        return 0;
×
1404
}
1405

1406
static int get_timezones_from_tzdata_zi(char ***ret) {
4✔
1407
        _cleanup_fclose_ FILE *f = NULL;
4✔
1408
        _cleanup_strv_free_ char **zones = NULL;
4✔
1409
        int r;
4✔
1410

1411
        assert(ret);
4✔
1412

1413
        f = fopen("/usr/share/zoneinfo/tzdata.zi", "re");
4✔
1414
        if (!f)
4✔
1415
                return -errno;
×
1416

1417
        for (;;) {
17,204✔
1418
                _cleanup_free_ char *line = NULL, *type = NULL, *f1 = NULL, *f2 = NULL;
17,200✔
1419

1420
                r = read_line(f, LONG_LINE_MAX, &line);
17,204✔
1421
                if (r < 0)
17,204✔
1422
                        return r;
1423
                if (r == 0)
17,204✔
1424
                        break;
1425

1426
                const char *p = line;
17,200✔
1427

1428
                /* The only lines we care about are Zone and Link lines.
1429
                 * Zone line format is:
1430
                 * 'Zone' 'timezone' ...
1431
                 * Link line format is:
1432
                 * 'Link' 'target' 'alias'
1433
                 * See 'man zic' for more detail. */
1434
                r = extract_many_words(&p, NULL, 0, &type, &f1, &f2);
17,200✔
1435
                if (r < 0)
17,200✔
1436
                        continue;
×
1437

1438
                char *tz;
17,200✔
1439
                if (IN_SET(*type, 'Z', 'z'))
17,200✔
1440
                        /* Zone lines have timezone in field 1. */
1441
                        tz = f1;
1,364✔
1442
                else if (IN_SET(*type, 'L', 'l'))
15,836✔
1443
                        /* Link lines have timezone in field 2. */
1444
                        tz = f2;
1,028✔
1445
                else
1446
                        /* Not a line we care about. */
1447
                        continue;
14,808✔
1448

1449
                if (!timezone_is_valid(tz, LOG_DEBUG))
2,392✔
1450
                        /* Don't list unusable timezones. */
1451
                        continue;
×
1452

1453
                r = strv_extend(&zones, tz);
2,392✔
1454
                if (r < 0)
2,392✔
1455
                        return r;
1456
        }
1457

1458
        *ret = TAKE_PTR(zones);
4✔
1459
        return 0;
4✔
1460
}
1461

1462
int get_timezones(char ***ret) {
4✔
1463
        _cleanup_strv_free_ char **zones = NULL;
4✔
1464
        int r;
4✔
1465

1466
        assert(ret);
4✔
1467

1468
        r = get_timezones_from_tzdata_zi(&zones);
4✔
1469
        if (r == -ENOENT) {
4✔
1470
                log_debug_errno(r, "Could not get timezone data from tzdata.zi, using zone1970.tab: %m");
×
1471
                r = get_timezones_from_zone1970_tab(&zones);
×
1472
                if (r == -ENOENT)
×
1473
                        log_debug_errno(r, "Could not get timezone data from zone1970.tab, using UTC: %m");
×
1474
        }
1475
        if (r < 0 && r != -ENOENT)
4✔
1476
                return r;
1477

1478
        /* Always include UTC */
1479
        r = strv_extend(&zones, "UTC");
4✔
1480
        if (r < 0)
4✔
1481
                return r;
1482

1483
        strv_sort_uniq(zones);
4✔
1484

1485
        *ret = TAKE_PTR(zones);
4✔
1486
        return 0;
4✔
1487
}
1488

1489
int verify_timezone(const char *name, int log_level) {
4,210✔
1490
        bool slash = false;
4,210✔
1491
        const char *p, *t;
4,210✔
1492
        _cleanup_close_ int fd = -EBADF;
4,210✔
1493
        char buf[4];
4,210✔
1494
        int r;
4,210✔
1495

1496
        if (isempty(name))
8,420✔
1497
                return -EINVAL;
1498

1499
        /* Always accept "UTC" as valid timezone, since it's the fallback, even if user has no timezones installed. */
1500
        if (streq(name, "UTC"))
4,208✔
1501
                return 0;
1502

1503
        if (name[0] == '/')
4,201✔
1504
                return -EINVAL;
1505

1506
        for (p = name; *p; p++) {
59,261✔
1507
                if (!ascii_isdigit(*p) &&
55,205✔
1508
                    !ascii_isalpha(*p) &&
53,911✔
1509
                    !IN_SET(*p, '-', '_', '+', '/'))
4,784✔
1510
                        return -EINVAL;
1511

1512
                if (*p == '/') {
55,061✔
1513

1514
                        if (slash)
3,708✔
1515
                                return -EINVAL;
1516

1517
                        slash = true;
1518
                } else
1519
                        slash = false;
1520
        }
1521

1522
        if (slash)
4,056✔
1523
                return -EINVAL;
1524

1525
        if (p - name >= PATH_MAX)
4,055✔
1526
                return -ENAMETOOLONG;
1527

1528
        t = strjoina("/usr/share/zoneinfo/", name);
20,275✔
1529

1530
        fd = open(t, O_RDONLY|O_CLOEXEC);
4,055✔
1531
        if (fd < 0)
4,055✔
1532
                return log_full_errno(log_level, errno, "Failed to open timezone file '%s': %m", t);
210✔
1533

1534
        r = fd_verify_regular(fd);
3,845✔
1535
        if (r < 0)
3,845✔
1536
                return log_full_errno(log_level, r, "Timezone file '%s' is not a regular file: %m", t);
×
1537

1538
        r = loop_read_exact(fd, buf, 4, false);
3,845✔
1539
        if (r < 0)
3,845✔
1540
                return log_full_errno(log_level, r, "Failed to read from timezone file '%s': %m", t);
×
1541

1542
        /* Magic from tzfile(5) */
1543
        if (memcmp(buf, "TZif", 4) != 0)
3,845✔
1544
                return log_full_errno(log_level, SYNTHETIC_ERRNO(EBADMSG),
×
1545
                                      "Timezone file '%s' has wrong magic bytes", t);
1546

1547
        return 0;
1548
}
1549

1550
void reset_timezonep(char **p) {
703✔
1551
        assert(p);
703✔
1552

1553
        (void) set_unset_env("TZ", *p, /* overwrite = */ true);
703✔
1554
        tzset();
703✔
1555
        *p = mfree(*p);
703✔
1556
}
703✔
1557

1558
char* save_timezone(void) {
703✔
1559
        const char *e = getenv("TZ");
703✔
1560
        if (!e)
703✔
1561
                return NULL;
1562

1563
        char *s = strdup(e);
48✔
1564
        if (!s)
48✔
1565
                log_debug("Failed to save $TZ=%s, unsetting the environment variable.", e);
×
1566

1567
        return s;
1568
}
1569

1570
bool clock_supported(clockid_t clock) {
297,189✔
1571
        struct timespec ts;
297,189✔
1572

1573
        switch (clock) {
297,189✔
1574

1575
        case CLOCK_MONOTONIC:
1576
        case CLOCK_REALTIME:
1577
        case CLOCK_BOOTTIME:
1578
                /* These three are always available in our baseline, and work in timerfd, as of kernel 3.15 */
1579
                return true;
1580

1581
        default:
1✔
1582
                /* For everything else, check properly */
1583
                return clock_gettime(clock, &ts) >= 0;
1✔
1584
        }
1585
}
1586

1587
int get_timezone(char **ret) {
59✔
1588
        _cleanup_free_ char *t = NULL;
59✔
1589
        int r;
59✔
1590

1591
        assert(ret);
59✔
1592

1593
        r = readlink_malloc(etc_localtime(), &t);
59✔
1594
        if (r == -ENOENT)
59✔
1595
                /* If the symlink does not exist, assume "UTC", like glibc does */
1596
                return strdup_to(ret, "UTC");
1✔
1597
        if (r < 0)
58✔
1598
                return r; /* Return EINVAL if not a symlink */
1599

1600
        const char *e = PATH_STARTSWITH_SET(t, "/usr/share/zoneinfo/", "../usr/share/zoneinfo/");
58✔
1601
        if (!e)
58✔
1602
                return -EINVAL;
1603
        if (!timezone_is_valid(e, LOG_DEBUG))
58✔
1604
                return -EINVAL;
1605

1606
        return strdup_to(ret, e);
58✔
1607
}
1608

1609
const char* etc_localtime(void) {
985✔
1610
        static const char *cached = NULL;
985✔
1611

1612
        if (!cached)
985✔
1613
                cached = secure_getenv("SYSTEMD_ETC_LOCALTIME") ?: "/etc/localtime";
884✔
1614

1615
        return cached;
985✔
1616
}
1617

1618
int mktime_or_timegm_usec(
7,397✔
1619
                struct tm *tm, /* input + normalized output */
1620
                bool utc,
1621
                usec_t *ret) {
1622

1623
        time_t t;
7,397✔
1624

1625
        assert(tm);
7,397✔
1626

1627
        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,397✔
1628
                return -ERANGE;
1629
        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,395✔
1630
                return -ERANGE;
1631

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

1637
        t = utc ? timegm(tm) : mktime(tm);
7,395✔
1638
        if (t < 0) /* Refuse negative times and errors */
7,395✔
1639
                return -ERANGE;
1640
        if ((usec_t) t >= USEC_INFINITY / USEC_PER_SEC) /* Never return USEC_INFINITY by accident (or overflow) */
7,393✔
1641
                return -ERANGE;
1642

1643
        if (ret)
7,393✔
1644
                *ret = (usec_t) t * USEC_PER_SEC;
2,143✔
1645
        return 0;
1646
}
1647

1648
int localtime_or_gmtime_usec(
126,427✔
1649
                usec_t t,
1650
                bool utc,
1651
                struct tm *ret) {
1652

1653
        t /= USEC_PER_SEC; /* Round down */
126,427✔
1654
        if (t > (usec_t) TIME_T_MAX)
126,427✔
1655
                return -ERANGE;
1656
        time_t sec = (time_t) t;
126,427✔
1657

1658
        struct tm buf = {};
126,427✔
1659
        if (!(utc ? gmtime_r(&sec, &buf) : localtime_r(&sec, &buf)))
126,427✔
1660
                return -EINVAL;
1661

1662
        if (ret)
126,427✔
1663
                *ret = buf;
126,427✔
1664

1665
        return 0;
1666
}
1667

1668
static uint32_t sysconf_clock_ticks_cached(void) {
654✔
1669
        static thread_local uint32_t hz = 0;
654✔
1670
        long r;
654✔
1671

1672
        if (hz == 0) {
654✔
1673
                r = sysconf(_SC_CLK_TCK);
69✔
1674

1675
                assert(r > 0);
69✔
1676
                hz = r;
69✔
1677
        }
1678

1679
        return hz;
654✔
1680
}
1681

1682
uint32_t usec_to_jiffies(usec_t u) {
9✔
1683
        uint32_t hz = sysconf_clock_ticks_cached();
9✔
1684
        return DIV_ROUND_UP(u, USEC_PER_SEC / hz);
9✔
1685
}
1686

1687
usec_t jiffies_to_usec(uint32_t j) {
645✔
1688
        uint32_t hz = sysconf_clock_ticks_cached();
645✔
1689
        return DIV_ROUND_UP(j * USEC_PER_SEC, hz);
645✔
1690
}
1691

1692
usec_t usec_shift_clock(usec_t x, clockid_t from, clockid_t to) {
657✔
1693
        usec_t a, b;
657✔
1694

1695
        if (x == USEC_INFINITY)
657✔
1696
                return USEC_INFINITY;
1697
        if (map_clock_id(from) == map_clock_id(to))
514✔
1698
                return x;
1699

1700
        a = now(from);
8✔
1701
        b = now(to);
8✔
1702

1703
        if (x > a)
8✔
1704
                /* x lies in the future */
1705
                return usec_add(b, usec_sub_unsigned(x, a));
12✔
1706
        else
1707
                /* x lies in the past */
1708
                return usec_sub_unsigned(b, usec_sub_unsigned(a, x));
6✔
1709
}
1710

1711
bool in_utc_timezone(void) {
676✔
1712
        tzset();
676✔
1713

1714
        return timezone == 0 && daylight == 0;
676✔
1715
}
1716

1717
int time_change_fd(void) {
457✔
1718

1719
        /* We only care for the cancellation event, hence we set the timeout to the latest possible value. */
1720
        static const struct itimerspec its = {
457✔
1721
                .it_value.tv_sec = TIME_T_MAX,
1722
        };
1723

1724
        _cleanup_close_ int fd = -EBADF;
914✔
1725

1726
        assert_cc(sizeof(time_t) == sizeof(TIME_T_MAX));
457✔
1727

1728
        /* Uses TFD_TIMER_CANCEL_ON_SET to get notifications whenever CLOCK_REALTIME makes a jump relative to
1729
         * CLOCK_MONOTONIC. */
1730

1731
        fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
457✔
1732
        if (fd < 0)
457✔
1733
                return -errno;
×
1734

1735
        if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) >= 0)
457✔
1736
                return TAKE_FD(fd);
457✔
1737

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

1748
#if SIZEOF_TIME_T == 8 && ULONG_MAX < UINT64_MAX
1749
        if (ERRNO_IS_NOT_SUPPORTED(errno) || errno == EOVERFLOW) {
1750
                static const struct itimerspec its32 = {
1751
                        .it_value.tv_sec = INT32_MAX,
1752
                };
1753

1754
                if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its32, NULL) >= 0)
1755
                        return TAKE_FD(fd);
1756
        }
1757
#endif
1758

1759
        return -errno;
×
1760
}
1761

1762
static const char* const timestamp_style_table[_TIMESTAMP_STYLE_MAX] = {
1763
        [TIMESTAMP_PRETTY] = "pretty",
1764
        [TIMESTAMP_US]     = "us",
1765
        [TIMESTAMP_UTC]    = "utc",
1766
        [TIMESTAMP_US_UTC] = "us+utc",
1767
        [TIMESTAMP_UNIX]   = "unix",
1768
};
1769

1770
/* Use the macro for enum → string to allow for aliases */
1771
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(timestamp_style, TimestampStyle);
×
1772

1773
/* For the string → enum mapping we use the generic implementation, but also support two aliases */
1774
TimestampStyle timestamp_style_from_string(const char *s) {
6✔
1775
        TimestampStyle t;
6✔
1776

1777
        t = (TimestampStyle) string_table_lookup_from_string(timestamp_style_table, ELEMENTSOF(timestamp_style_table), s);
6✔
1778
        if (t >= 0)
6✔
1779
                return t;
1780
        if (STRPTR_IN_SET(s, "µs", "μs")) /* accept both µ symbols in unicode, i.e. micro symbol + Greek small letter mu. */
2✔
1781
                return TIMESTAMP_US;
1✔
1782
        if (STRPTR_IN_SET(s, "µs+utc", "μs+utc"))
1✔
1783
                return TIMESTAMP_US_UTC;
1✔
1784
        return t;
1785
}
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