ตามมาตรฐาน C11 (7.27.2.5) มีฟังก์ชัน timespec_get
ระบุไว้ใน time.h
ฉันได้ลองใช้คอมไพเลอร์หลายตัวแล้ว รวมถึง clang และ gcc หลายเวอร์ชันซึ่งควรจะรองรับ C11 แต่ฟังก์ชันนี้หายไปอยู่เสมอ แมโคร TIME_UTC
ก็หายไปเช่นกัน
นี่คือไฟล์ทดสอบ 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);
}
และเอาต์พุตโดยใช้ 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.
ฉันใส่ความคิดเห็นในบรรทัด timespec_get
เพียงเพื่อให้แน่ใจว่าฉันใช้ C11 และฉันเป็นเช่นนั้น
โดยพื้นฐานแล้วฉันได้ผลลัพธ์เดียวกันสำหรับ gcc เวอร์ชัน 4.8, 5 และ 6
ฉันใช้ Mac, OS 10.11.6
gettimeofday
เพื่อรับเวลารอง (เครื่อง Mac ก็ไม่รองรับ Posixclock_gettime
เช่นกัน) นอกจากนี้ยังมี ฟังก์ชันนาฬิกาเฉพาะของดาร์วิน - person Steve Summit   schedule 26.08.2018