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[TIME_STR_BUFSIZE]) | |
6 | { | ||
7 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→4) 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 (5→6) taken 2 times.
✗ Branch 1 (5→13) not taken.
|
2 | if (unlikely(!localtime_r(&ts->tv_sec, &tm))) { |
14 | return NULL; | ||
15 | } | ||
16 | |||
17 | // Append date and time | ||
18 | 2 | size_t i = strftime(buf, TIME_STR_BUFSIZE, "%F %T.", &tm); | |
19 | 2 | bool overflow = TIME_STR_BUFSIZE < i + sizeof("123456789 +0600"); | |
20 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→10) taken 2 times.
|
2 | if (unlikely(i == 0 || overflow)) { |
21 | ✗ | errno = overflow ? ERANGE : errno; | |
22 | ✗ | return NULL; | |
23 | } | ||
24 | |||
25 | // Append nanoseconds | ||
26 | 2 | i += buf_umax_to_str(ts->tv_nsec, buf + i); | |
27 | |||
28 | // Append timezone | ||
29 | 2 | size_t zlen = strftime(buf + i, TIME_STR_BUFSIZE - i, " %z", &tm); | |
30 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 2 times.
|
2 | return likely(zlen) ? buf : NULL; |
31 | } | ||
32 |