diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-12-22 17:24:03 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-12-22 17:24:03 -0500 |
commit | bf906b2d5e1ddc5d77c06197d1105246e77b101a (patch) | |
tree | b4e5ec39bdc077206b2e0cb0e35dd39c560a113e /time.c | |
parent | c65b9f4b9faa734cb9c8804ccda0fa97f60e6381 (diff) |
undo actually not quite done yet
Diffstat (limited to 'time.c')
-rw-r--r-- | time.c | 65 |
1 files changed, 65 insertions, 0 deletions
@@ -0,0 +1,65 @@ +#include <time.h> +#include <errno.h> +#include <sys/stat.h> + +static struct timespec time_last_modified(char const *filename) { +#if __unix__ + struct stat statbuf = {}; + stat(filename, &statbuf); + return statbuf.st_mtim; +#else + // windows' _stat does not have st_mtim + struct _stat statbuf = {}; + struct timespec ts = {}; + _stat(filename, &statbuf); + ts.tv_sec = statbuf.st_mtime; + return ts; +#endif +} + +static int timespec_cmp(struct timespec a, struct timespec b) { + if (a.tv_sec > b.tv_sec) return 1; + if (a.tv_sec < b.tv_sec) return -1; + if (a.tv_nsec > b.tv_nsec) return 1; + if (a.tv_nsec < b.tv_nsec) return -1; + return 0; +} + +static bool timespec_eq(struct timespec a, struct timespec b) { + return timespec_cmp(a, b) == 0; +} + +static struct timespec timespec_max(struct timespec a, struct timespec b) { + return timespec_cmp(a, b) < 0 ? b : a; +} + +// sleep for a certain number of nanoseconds +static void sleep_ns(u64 ns) { +#if __unix__ + struct timespec rem = {}, req = { + (time_t)(ns / 1000000000), + (long)(ns % 1000000000) + }; + + while (nanosleep(&req, &rem) == EINTR) // sleep interrupted by signal + req = rem; +#else + // windows.... + Sleep((DWORD)(ns / 1000000)); +#endif +} + +// sleep for microseconds +static void time_sleep_us(u64 us) { + sleep_ns(us * 1000); +} + +// sleep for milliseconds +static void time_sleep_ms(u64 ms) { + sleep_ns(ms * 1000000); +} + +// sleep for seconds +static void time_sleep_s(u64 s) { + sleep_ns(s * 1000000000); +} |