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 | // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_stat.h.html#tag_13_61_03:~:text=struct%20timespec%20st_mtim | ||
21 | 48 | return &st->st_mtim; | |
22 | #else | ||
23 | // macOS doesn't conform to POSIX 2008 and uses its own, platform-specific | ||
24 | // naming for the `struct stat` timespec member | ||
25 | return &st->st_mtimespec; | ||
26 | #endif | ||
27 | } | ||
28 | |||
29 | 210 | static inline struct timespec timespec_subtract ( | |
30 | const struct timespec *lhs, | ||
31 | const struct timespec *rhs | ||
32 | ) { | ||
33 | 210 | BUG_ON(lhs->tv_nsec >= NS_PER_SECOND); | |
34 | 210 | BUG_ON(rhs->tv_nsec >= NS_PER_SECOND); | |
35 | 210 | time_t sec = lhs->tv_sec - rhs->tv_sec; | |
36 | 210 | long nsec = lhs->tv_nsec - rhs->tv_nsec; | |
37 | 420 | return (struct timespec) { | |
38 | 210 | .tv_sec = sec - (nsec < 0), | |
39 |
2/2✓ Branch 0 (6→7) taken 209 times.
✓ Branch 1 (6→8) taken 1 times.
|
210 | .tv_nsec = nsec + (nsec < 0 ? NS_PER_SECOND : 0), |
40 | }; | ||
41 | } | ||
42 | |||
43 | 1 | static inline bool timespecs_equal(const struct timespec *a, const struct timespec *b) | |
44 | { | ||
45 |
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; |
46 | } | ||
47 | |||
48 | char *timespec_to_str(const struct timespec *ts, char buf[TIME_STR_BUFSIZE]) NONNULL_ARGS WARN_UNUSED_RESULT; | ||
49 | |||
50 | #endif | ||
51 |