5

According to the C11 Standard (7.27.2.5), there is a function timespec_get specified in time.h. I have tried several compilers, including clang and several versions of gcc, which are supposed to support C11, but this function is always missing. The macro TIME_UTC is also missing.

Here is a test file mytime.c:

#include <time.h>
#include <stdio.h>
int main() {
  printf("C version: %ld\n", __STDC_VERSION__);
  fflush(stdout);
  struct timespec ts;
  timespec_get(&ts, TIME_UTC);
}

and the output using Clang:

$ cc --version
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

$ cc -std=c11 mytime.c
mytime.c:9:3: warning: implicit declaration of function 'timespec_get' is invalid in C99
      [-Wimplicit-function-declaration]
  timespec_get(&ts, TIME_UTC);
  ^
mytime.c:9:21: error: use of undeclared identifier 'TIME_UTC'
  timespec_get(&ts, TIME_UTC);
                    ^
1 warning and 1 error generated.

I commented out the timespec_get line just to make sure I am using C11, and I am.

I get basically the same results for gcc versions 4.8, 5, and 6.

I am using a Mac, OS 10.11.6.

Steve Siegel
  • 504
  • 5
  • 11
  • 2
    Functions are defined in libraries, not compilers. Maybe the OS X libraries simply don't support that function? – melpomene Aug 26 '18 at 16:24
  • 1
    FWIW, gcc and clang on my Mac don't support it, either. I use `gettimeofday` to get sub-second time. (The Mac doesn't support Posix `clock_gettime`, either.) There are also some [Darwin-specific clock functions](http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/clock_get_time.html). – Steve Summit Aug 26 '18 at 16:38

2 Answers2

9

The Mac OS X standard library does not conform to any modern version of C or POSIX. It is stuck at C99 and POSIX 2001 and has conformance problems even with respect to these.

0xF2
  • 314
  • 3
  • 17
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • 2
    The OP should note that modern C compilers are pretty much a Linux thing. On windows, MSVC is a weird hybrid of C89 and a few bits and pieces borrowed from later specs. If you ever want your C code to be portable, you pretty much want to stick with C89 features, as painful as that is. – lockcmpxchg8b Aug 26 '18 at 18:25
  • 2
    @lockcmpxchg8b: I think that's a misrepresentation and muddles the *library* issue (which this question is about) with a compiler issue (MSVC is junk). Compiler-side there is C99 & C11 support for basically every platform of modern relevance (GCC and clang/llvm support almost everything) and modern MSVC even *kinda* aims to support modern standards (they claim modern C++; modern C is less reliable). – R.. GitHub STOP HELPING ICE Aug 26 '18 at 18:34
  • Hmm. I suppose it was careless to say "compiler" rather than "language implementation" which was the intent. (Particularly given that the content of `time.h` is part of the C specification.) I don't think it's consistent with the notion of "portability" to say "it's portable so long as you have GCC on your platform", but that's not relevant to the OP's question. – lockcmpxchg8b Aug 26 '18 at 20:45
  • So Apple is preventing me from having a conforming C11 implementation? Does anyone know why? All the money in the world, and they can't implement `timespec_get`? – Steve Siegel Aug 29 '18 at 00:52
  • @SteveSiegel: In principle you could use an alternate implementation of the standard library, but that would be potentially incompatible with the whole library ecosystem and AFAIK none for OSX exist. Alternatively you could add a secondary library with these functions and patch the standard headers to add the necessary declarations, etc. (or add a directory of overlay headers to the beginning of the include path and have them use the nonstandard `#include_next` feature) to get the missing C11 library functionality. – R.. GitHub STOP HELPING ICE Aug 29 '18 at 01:34
  • Do you mean all the stuff I install with `brew install` avoids this function? I don't believe it. It must be some configuration option and I don't see why this answer is accepted. My clang is 1001 and have this problem while the online compiler "coliru" has clang 5 and compiles the code without an issue. – Nakilon Mar 09 '21 at 09:33
  • Yes. I've never seen any real world code use this function because it's a less functional clone of the POSIX `clock_gettime`. – R.. GitHub STOP HELPING ICE Mar 09 '21 at 15:15
1

MacOS 10.15 supports timespec_get. This is taken from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/time.h:

#if (__DARWIN_C_LEVEL >= __DARWIN_C_FULL) && \
        ((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
        (defined(__cplusplus) && __cplusplus >= 201703L))
/* ISO/IEC 9899:201x 7.27.2.5 The timespec_get function */
#define TIME_UTC        1       /* time elapsed since epoch */
__API_AVAILABLE(macosx(10.15), ios(13.0), tvos(13.0), watchos(6.0))
int timespec_get(struct timespec *ts, int base);
#endif

You need to be building with C11 or C++17 or newer when time.h is included.

I have not done any timing or other investigation into whether gettimeofday or timespec_get is better for this purpose on Mac 10.15, only that timespec_get becomes available.

Levi Morrison
  • 19,116
  • 7
  • 65
  • 85