主要函数long clock_gettime(clockid_t ,struct timespec*)
clockid_t 目前定义了6种时钟,分别是
CLOCK_REALTIME 系统当前时间,从1970年1.1日算起
CLOCK_MONOTONIC 系统的启动时间,不能被设置
CLOCK_PROCESS_CPUTIME_ID 进程运行时间
CLOCK_THREAD_CPUTIME_ID 线程运行时间
CLOCK_REALTIME_HR CLOCK_REALTIME的高精度版本
CLOCK_MONOTONIC_HR CLOCK_MONOTONIC的高精度版本
获取特定时钟的时间精度:long clock_getres(clockid_t )
设置特定时钟的时间:long clock_settime(clockid_t ,struct timespec*)
以下为获取linux高精度系统启动时间示例:
#include <stdio.h>
#include <time.h>
void main(){
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC ,&ts);
printf("%d %d",ts.tv_sec, ts.tv_nsec);
}