linux下常用的几个时间函数:gettimeofday和clock_gettime

转自:https://blog.csdn.net/rosekin/article/details/17246797

 

time()提供了秒级的精确度  

1、头文件 <time.h>  

2、函数原型  

time_t time(time_t * timer)   

函数返回从TC1970-1-1 0:0:0开始到现在的秒数  

用time()函数结合其他函数(如:localtime、gmtime、asctime、ctime)可以获得当前系统时间或是标准时间。  

#include <time.h>  

#include <stdio.h>  

int main(void)  

{  

    time_t t;   

    t = time(NULL);  

    printf("The number of seconds since January 1, 1970 is %ld",t);  

    return 0;  

}   

#include <stdio.h>  

#include <stddef.h>  

#include <time.h>  

int main(void)  

{  

    time_t timer;//time_t就是long int 类型  

    struct tm *tblock;  

    timer = time(NULL);//这一句也可以改成time(&timer);  

    tblock = localtime(&timer);  

    printf("Local time is: %s/n",asctime(tblock));      

    return 0;  

}  

 

gettimeofday()提供了微秒级的精确度  

  

1、头文件 <time.h>  

2、函数原型  

int gettimeofday(struct timeval *tv, struct timezone *tz);   

gettimeofday()会把目前的时间由tv所指的结构返回,当地时区的信息则放到tz所指的结构中(可用NULL)。  

参数说明:  

    timeval结构定义为:  

    struct timeval  

    {  

        long tv_sec; /*秒*/  

        long tv_usec; /*微秒*/  

    };  

    timezone 结构定义为:  

    struct timezone  

    {  

        int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/  

        int tz_dsttime; /*日光节约时间的状态*/  

    };  

    上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime 所代表的状态如下  

        DST_NONE /*不使用*/  

        DST_USA /*美国*/  

        DST_AUST /*澳洲*/  

        DST_WET /*西欧*/  

        DST_MET /*中欧*/  

        DST_EET /*东欧*/  

        DST_CAN /*加拿大*/  

        DST_GB /*大不列颠*/  

        DST_RUM /*罗马尼亚*/  

        DST_TUR /*土耳其*/  

        DST_AUSTALT /*澳洲(1986年以后)*/  

返回值: 成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。  

#include<stdio.h>  

#include<time.h>  

int main(void)  

{  

    struct timeval tv;  

    struct timezone tz;  

    gettimeofday (&tv , &tz);  

      

    printf(“tv_sec; %d/n”, tv,.tv_sec) ;  

    printf(“tv_usec; %d/n”,tv.tv_usec);  

    printf(“tz_minuteswest; %d/n”, tz.tz_minuteswest);  

    printf(“tz_dsttime, %d/n”,tz.tz_dsttime);       

    return 0;  

}  

clock_gettime( ) 提供了纳秒级的精确度  

  

1、头文件 <time.h>  

2、编译&链接。在编译链接时需加上 -lrt ;因为在librt中实现了clock_gettime函数  

3、函数原型  

int clock_gettime(clockid_t clk_id, struct timespect *tp);  

    参数说明:  

    clockid_t clk_id 用于指定计时时钟的类型,有以下4种:  

        CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,中间时刻如果系统时间被用户该成其他,则对应的时间相应改变  

        CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响  

        CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间  

        CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间  

    struct timespect *tp用来存储当前的时间,其结构如下:  

        struct timespec  

        {  

            time_t tv_sec; /* seconds */  

            long tv_nsec; /* nanoseconds */  

        };  

    返回值。0成功,-1失败  

#include<stdio.h>  

#include<time.h>  

int main()  

{  

    struct timespec ts;    

    clock_gettime(CLOCK_REALTIME, &ts);  

    printf("CLOCK_REALTIME: %d, %d", ts.tv_sec, ts.tv_nsec);    

    clock_gettime(CLOCK_MONOTONIC, &ts);//打印出来的时间跟 cat /proc/uptime 第一个参数一样  

    printf("CLOCK_MONOTONIC: %d, %d", ts.tv_sec, ts.tv_nsec);    

    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);  

    printf("CLOCK_PROCESS_CPUTIME_ID: %d, %d", ts.tv_sec, ts.tv_nsec);   

    clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);  

    printf("CLOCK_THREAD_CPUTIME_ID: %d, %d", ts.tv_sec, ts.tv_nsec);     

    printf("/n%d/n", time(NULL));  

    return 0;  

}  

/proc/uptime里面的两个数字分别表示:   

the uptime of the system (seconds), and the amount of time spent in idle process (seconds).   

把第一个数读出来,那就是从系统启动至今的时间,单位是秒  

_ftime()提供毫秒级的精确度  

1、头文件 <sys/types.h> and <sys/timeb.h>   

2、函数原型  

void _ftime(struct _timeb *timeptr);   

参数说明:  

    struct _timeb   

    {  

        time_t time;  

        unsigned short millitm;  

        short timezone;  

        short dstflag;  

    };   

#include <stdio.h>  

#include <sys/timeb.h>  

#include <time.h>  

void main( void )  

{  

    struct _timeb timebuffer;  

    char *timeline;  

    _ftime( &timebuffer );  

    timeline = ctime( & ( timebuffer.time ) );  

    printf( "The time is %.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20] );  

}  

 

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`clock_gettime` 函数函数体实现是由操作系统提供的,具体实现可能因操作系统而异。以下是一种可能的实现方式: ```C #include <time.h> #include <errno.h> #include <sys/time.h> #include <sys/resource.h> int clock_gettime(clockid_t clk_id, struct timespec *tp) { int ret = 0; struct timeval tv; struct timezone tz; if (clk_id != CLOCK_REALTIME && clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_PROCESS_CPUTIME_ID && clk_id != CLOCK_THREAD_CPUTIME_ID) { errno = EINVAL; return -1; } if (tp == NULL) { errno = EFAULT; return -1; } if (clk_id == CLOCK_REALTIME) { ret = gettimeofday(&tv, &tz); tp->tv_sec = tv.tv_sec; tp->tv_nsec = tv.tv_usec * 1000; } else if (clk_id == CLOCK_MONOTONIC) { ret = clock_gettime(CLOCK_MONOTONIC_RAW, tp); } else if (clk_id == CLOCK_PROCESS_CPUTIME_ID) { ret = getrusage(RUSAGE_SELF, &ru); tp->tv_sec = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec; tp->tv_nsec = ru.ru_utime.tv_usec * 1000 + ru.ru_stime.tv_usec * 1000; } else if (clk_id == CLOCK_THREAD_CPUTIME_ID) { ret = getrusage(RUSAGE_THREAD, &ru); tp->tv_sec = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec; tp->tv_nsec = ru.ru_utime.tv_usec * 1000 + ru.ru_stime.tv_usec * 1000; } if (ret == -1) { errno = EINVAL; return -1; } return 0; } ``` 这个实现中,函数首先检查 `clk_id` 是否是支持的时钟类型,以及 `tp` 是否为 `NULL`。然后,根据不同的时钟类型调用不同的系统函数获取时间值,并将结果存储到 `timespec` 结构体中。最后,返回 `0` 表示成功,或者返回 `-1` 表示失败并设置 `errno` 错误码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值