As shown in @zwol's answer, things really get a bit complicated and interesing ——
See the following simple program (`foo.c`):
#include <stdio.h>
#include <unistd.h>
#include <time.h>
int
main()
{
struct timespec spec;
printf("_POSIX_MONOTONIC_CLOCK = %d\n",
(int)_POSIX_MONOTONIC_CLOCK);
printf("sysconf(_SC_MONOTONIC_CLOCK) = %ld\n",
sysconf(_SC_MONOTONIC_CLOCK) );
printf("clock_gettime(CLOCK_MONOTONIC) = %d\n",
clock_gettime(CLOCK_MONOTONIC, & spec) );
return 0;
}
On Linux (Debian 9, x86_64):
<!-- language: none -->
[STEP 101] # uname -a
Linux debian9 4.9.0-6-amd64 #1 SMP Debian 4.9.88-1 (2018-04-29) x86_64 GNU/Linux
[STEP 102] # gcc foo.c && ./a.out
_POSIX_MONOTONIC_CLOCK = 0
sysconf(_SC_MONOTONIC_CLOCK) = 200809
clock_gettime(CLOCK_MONOTONIC) = 0
[STEP 103] #
On macOS (10.13, High Sierra):
<!-- language: none -->
[STEP 201] $ uname -a
Darwin macbook.home 17.5.0 Darwin Kernel Version 17.5.0: Fri Apr 13 19:32:32 PDT 2018; root:xnu-4570.51.2~1/RELEASE_X86_64 x86_64
[STEP 202] $ cc foo.c && ./a.out
_POSIX_MONOTONIC_CLOCK = -1
sysconf(_SC_MONOTONIC_CLOCK) = -1
clock_gettime(CLOCK_MONOTONIC) = 0
[STEP 203] $
On FreeBSD (11.1, x86_64):
<!-- language: none -->
[STEP 301] # uname -a
FreeBSD freebsd 11.1-RELEASE FreeBSD 11.1-RELEASE #0 r321309: Fri Jul 21 02:08:28 UTC 2017
[email protected]:/usr/obj/usr/src/sys/GENERIC amd64
[STEP 302] # cc foo.c && ./a.out
_POSIX_MONOTONIC_CLOCK = 200112
sysconf(_SC_MONOTONIC_CLOCK) = 200112
clock_gettime(CLOCK_MONOTONIC) = 0
[STEP 303] #
The result on macOS really surprised me. `sysconf()` returns `-1` but `clock_gettime(CLOCK_MONOTONIC)` succeeds! Not sure if this indicate macOS is not POSIX compliant. Anyway it proves **runtime checking with `sysconf()` is not reliable!**
Finally I'm going with this:
int
Clock_gettime(struct timespec * spec)
{
static bool firstime = true;
static clockid_t clock = CLOCK_REALTIME;
if (firstime) {
firstime = false;
#ifdef CLOCK_MONOTONIC
if (clock_gettime(CLOCK_MONOTONIC, spec) == 0) {
clock = CLOCK_MONOTONIC;
return 0;
}
#endif
}
return clock_gettime(clock, spec);
}