获取系统当前时间的两种方式

一、时间用途

根据实践需要,时间有不同的使用方式。
场景1:等待xx秒执行某个动作,这个等待在计算机系统的程序中是如何实现的?
场景2:获取当前年月日等基本时间信息
场景3:计算代码执行时间

二、获取时间方式1:函数"clock_gettime"

此函数是基于Linux C语言的时间函数,可以用于计算精度和纳秒。
要点:与Linux系统强相关。
头文件和函数声明如下:

#include<time.h>
int clock_gettime(clockid_t clk_id, struct timespec *tp);

说明:

  • clk_id : 检索和设置的clk_id指定的时钟时间。
    • CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-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; /* 纳秒*/
};

三、获取时间方式2:函数“gettimeofday”

gettimeofday是计算机函数,使用C语言编写程序需要获得当前精确时间(1970年1月1日到现在的时间),或者为执行计时,可以使用gettimeofday()函数。
特点:与系统无关,便于移植。
头文件和函数声明如下:

#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);

其参数tv是保存获取时间结果的结构体,参数tz用于保存时区结果:

struct timezone{
    int tz_minuteswest; /*格林威治时间往西方的时差*/
    int tz_dsttime;     /*DST 时间的修正方式*/
}

timezone 参数若不使用则传入NULL即可。
而结构体timeval的定义为:

struct timeval{
    long int tv_sec; // 秒数
    long int tv_usec; // 微秒数
}

它获得的时间精确到微秒(1e6s)量级。
在一段代码前后分别使用gettimeofday可以计算代码执行时间:

struct timeval tv_begin, tv_end;
gettimeofday(&tv_begin, NULL);
foo();
//process ...
gettimeofday(&tv_end, NULL);

函数执行成功后返回0,失败后返回-1,错误代码存于errno中。

四、示例代码

#include<stdio.h>

#include <time.h>
#include <sys/time.h>
#include <unistd.h>

typedef unsigned int uint32_t;
#define csp_sleep_ms(time_ms) usleep(time_ms * 1000);


uint32_t csp_get_ms() {
    struct timespec ts;

    if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
        return (ts.tv_sec*1000+ts.tv_nsec/1000000);
    } else {
        return 0;
    }
}

uint32_t csp_get_ms_another(){
    struct timeval tv;
    struct timespec ts;
    if (gettimeofday(&tv, NULL))
        return 0;
    ts.tv_sec  = tv.tv_sec;
    ts.tv_nsec = tv.tv_usec * 1000;
    return ts.tv_sec*1000+ts.tv_nsec/1000000;
}

int main()
{
    uint32_t start = csp_get_ms();
    csp_sleep_ms(1000);
    uint32_t time = csp_get_ms()-start;
    printf(" clock_gettime with %u ms\r\n", time);

    uint32_t start1 = csp_get_ms_another();
    csp_sleep_ms(1000);
    uint32_t time1 = csp_get_ms_another()-start1;
    printf(" gettimeofday with %u ms\r\n", time1);

    return 0;
}

五、执行结果

gcc learntime.c
./a.out
clock_gettime with 1002 ms
gettimeofday with 1003 ms
posted @ 2020-02-04 11:31  khldragon  阅读(2211)  评论(0编辑  收藏  举报