clock_gettime获取系统时间

clock_gettime获取系统时间

clock_gettime()是基于Linux C语言的时间函数,他可以用于计算精度和纳秒。

需要包含头文件
#include
函数原型为
int clock_gettime(clockid_t clk_id,struct timespec *tp);

参数
clk_id 获取和设置指定时钟时间的 clk_id;
//CLOCK_REALTIME:系统相对时间,从UTC 1970-1-1 0:0:0开始计时,更改系统时间会更改获取的值;
//CLOCK_MONOTONIC:系统绝对时间/单调时间,为系统重启到现在的时间,更改系统时间对它没有影响;
//CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间;
//CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间;

struct timespec
{
    time_t tv_sec; /* 秒*/
    long tv_nsec; /* 纳秒*/
}

基于此封装的两个函数如下,分别是获取系统绝对时间和相对时间;

long long getSteadyMillis()
{
    //CLOCK_REALTIME:系统相对时间,从UTC 1970-1-1 0:0:0开始计时,更改系统时间会更改获取的值;
    //CLOCK_MONOTONIC:系统绝对时间/单调时间,为系统重启到现在的时间,更改系统时间对它没有影响;
    //CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间;
    //CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间;
    struct timespec ts {};
    (void)clock_gettime(CLOCK_MONOTONIC, &ts);
    long long milliseconds = (ts.tv_sec*1000) + (ts.tv_nsec/1000000);
    return milliseconds;
}

long long getSystemMillis()
{
    struct timespec ts {};
    (void)clock_gettime(CLOCK_REALTIME, &ts);
    long long milliseconds = (ts.tv_sec*1000) + (ts.tv_nsec/1000000);
    return milliseconds;
}

测试程序如下

/*****************************************
* Copyright (C) 2020 * Ltd. All rights reserved.
* File name   : time.cpp
* Created date: 2020-05-04 14:59:40
*******************************************/

#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

long long getSteadyMillis()
{
    //CLOCK_REALTIME:系统相对时间,从UTC 1970-1-1 0:0:0开始计时,更改系统时间会更改获取的值;
    //CLOCK_MONOTONIC:系统绝对时间/单调时间,为系统重启到现在的时间,更改系统时间对它没有影响;
    //CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间;
    //CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间;
    struct timespec ts {};
    (void)clock_gettime(CLOCK_MONOTONIC, &ts);
    long long milliseconds = (ts.tv_sec*1000) + (ts.tv_nsec/1000000);
    return milliseconds;
}

long long getSystemMillis()
{
    struct timespec ts {};
    (void)clock_gettime(CLOCK_REALTIME, &ts);
    long long milliseconds = (ts.tv_sec*1000) + (ts.tv_nsec/1000000);
    return milliseconds;
}

int main() {
    int i = 0;
    long long nowSys;
    long long lastSys = getSystemMillis();
    long long sysinterval = nowSys - lastSys;
    long long nowclk;
    long long lastclk = getSteadyMillis();
    long long clkinterval = nowclk - lastclk;
    std::cout << "CLOCKS_PER_SEC: " << CLOCKS_PER_SEC << std::endl;
    for (i=0; i<= 100; i++) {
        nowSys = getSystemMillis();
        nowclk = getSteadyMillis();
        clkinterval = nowclk - lastclk;
        sysinterval = nowSys - lastSys;
        std::cout<<"nowSys:"<

结果

nowSys:1588688076572,nowclk:203208551,sysinterval:1000,clkinterval:1000
nowSys:1588688077573,nowclk:203209552,sysinterval:1001,clkinterval:1001
nowSys:1588688078574,nowclk:203210553,sysinterval:1001,clkinterval:1001
nowSys:1588688079575,nowclk:203211554,sysinterval:1001,clkinterval:1001
nowSys:1588688080575,nowclk:203212554,sysinterval:1000,clkinterval:1000
nowSys:1588688081575,nowclk:203213555,sysinterval:1000,clkinterval:1001
nowSys:1588688130302,nowclk:203214556,sysinterval:48727,clkinterval:1001 #sudo date -s "22:15:30"修改系统时间
nowSys:1588688131303,nowclk:203215557,sysinterval:1001,clkinterval:1001
nowSys:1588688132303,nowclk:203216557,sysinterval:1000,clkinterval:1000
nowSys:1588688133304,nowclk:203217557,sysinterval:1001,clkinterval:1000
nowSys:1588688134304,nowclk:203218558,sysinterval:1000,clkinterval:1001
nowSys:1588688135304,nowclk:203219558,sysinterval:1000,clkinterval:1000
nowSys:1588688136305,nowclk:203220559,sysinterval:1001,clkinterval:1001
nowSys:1588688130856,nowclk:203221559,sysinterval:-5449,clkinterval:1000 #sudo date -s "22:15:30"修改系统时间
nowSys:1588688131857,nowclk:203222560,sysinterval:1001,clkinterval:1001
nowSys:1588688132858,nowclk:203223562,sysinterval:1001,clkinterval:1002
nowSys:1588688133859,nowclk:203224562,sysinterval:1001,clkinterval:1000
nowSys:1588688134860,nowclk:203225563,sysinterval:1001,clkinterval:1001

你可能感兴趣的:(#,04std_C++,#,Linux_env,#,03unix_c)