Line | Branch | Exec | Source |
---|---|---|---|
1 | #ifndef UTIL_TIME_UTIL_H | ||
2 | #define UTIL_TIME_UTIL_H | ||
3 | |||
4 | #include <stdbool.h> | ||
5 | #include <stddef.h> | ||
6 | #include <sys/stat.h> | ||
7 | #include <time.h> | ||
8 | #include "debug.h" | ||
9 | #include "macros.h" | ||
10 | |||
11 | #define TIME_STR_BUFSIZE (64) // sizeof("292271025015-12-01 23:59:00.999999999 +0400") | ||
12 | #define NS_PER_SECOND (1000000000L) | ||
13 | #define MS_PER_SECOND (1000L) | ||
14 | #define NS_PER_MS (1000000L) | ||
15 | #define US_PER_MS (1000L) | ||
16 | |||
17 | 48 | static inline const struct timespec *get_stat_mtime(const struct stat *st) | |
18 | { | ||
19 | #if defined(__APPLE__) | ||
20 | return &st->st_mtimespec; | ||
21 | #else | ||
22 | // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_stat.h.html#tag_13_61_03 | ||
23 | 48 | return &st->st_mtim; | |
24 | #endif | ||
25 | } | ||
26 | |||
27 | 205 | static inline struct timespec timespec_subtract ( | |
28 | const struct timespec *lhs, | ||
29 | const struct timespec *rhs | ||
30 | ) { | ||
31 | 205 | BUG_ON(lhs->tv_nsec >= NS_PER_SECOND); | |
32 | 205 | BUG_ON(rhs->tv_nsec >= NS_PER_SECOND); | |
33 | 205 | time_t sec = lhs->tv_sec - rhs->tv_sec; | |
34 | 205 | long nsec = lhs->tv_nsec - rhs->tv_nsec; | |
35 | 410 | return (struct timespec) { | |
36 | 205 | .tv_sec = sec - (nsec < 0), | |
37 |
2/2✓ Branch 0 (6→7) taken 204 times.
✓ Branch 1 (6→8) taken 1 times.
|
205 | .tv_nsec = nsec + (nsec < 0 ? NS_PER_SECOND : 0), |
38 | }; | ||
39 | } | ||
40 | |||
41 | 1 | static inline bool timespecs_equal(const struct timespec *a, const struct timespec *b) | |
42 | { | ||
43 |
2/4✓ Branch 0 (2→3) taken 1 times.
✗ Branch 1 (2→4) not taken.
✗ Branch 2 (3→4) not taken.
✓ Branch 3 (3→5) taken 1 times.
|
1 | return a->tv_sec == b->tv_sec && a->tv_nsec == b->tv_nsec; |
44 | } | ||
45 | |||
46 | char *timespec_to_str(const struct timespec *ts, char buf[TIME_STR_BUFSIZE]) NONNULL_ARGS WARN_UNUSED_RESULT; | ||
47 | |||
48 | #endif | ||
49 |