Line | Branch | Exec | Source |
---|---|---|---|
1 | #include <errno.h> | ||
2 | #include "time-util.h" | ||
3 | #include "numtostr.h" | ||
4 | |||
5 | 4 | char *timespec_to_str(const struct timespec *ts, char *buf, size_t bufsize) | |
6 | { | ||
7 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (unlikely(ts->tv_nsec < 0 || ts->tv_nsec >= NS_PER_SECOND)) { |
8 | 2 | errno = EINVAL; | |
9 | 2 | return NULL; | |
10 | } | ||
11 | |||
12 | 2 | struct tm tm; | |
13 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (unlikely(!localtime_r(&ts->tv_sec, &tm))) { |
14 | return NULL; | ||
15 | } | ||
16 | |||
17 | // Append date and time | ||
18 | 2 | char *ptr = buf; | |
19 | 2 | size_t max = bufsize; | |
20 | 2 | size_t n = strftime(ptr, max, "%F %T.", &tm); | |
21 | 2 | ptr += n; | |
22 | 2 | max -= n; | |
23 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (unlikely(n == 0 || max < 10)) { |
24 | ✗ | errno = ENOBUFS; | |
25 | ✗ | return NULL; | |
26 | } | ||
27 | |||
28 | // Append nanoseconds | ||
29 | 2 | n = buf_umax_to_str(ts->tv_nsec, ptr); | |
30 | 2 | BUG_ON(n == 0 || n > 9); | |
31 | 2 | ptr += n; | |
32 | 2 | max -= n; | |
33 | |||
34 | // Append timezone | ||
35 | 2 | n = strftime(ptr, max, " %z", &tm); | |
36 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (unlikely(n == 0)) { |
37 | ✗ | errno = ENOBUFS; | |
38 | ✗ | return NULL; | |
39 | } | ||
40 | |||
41 | return buf; | ||
42 | } | ||
43 |