linux下常用时间函数

在linux下编程,有一些常用的时间函数,下面做一个总结梳理。

1、time

SYNOPSIS
       #include <time.h>

       time_t time(time_t *t);

DESCRIPTION
       time() returns the time since the Epoch (00:00:00 UTC, January 1, 1970), mea-
       sured in seconds.

       If t is non-NULL, the return value is also stored in the memory pointed to by
       t.

先说一下,time_t类型是一个有符号整数,是32位系统下,就是32位有符号数,在64位系统下,就是64位有符号数。那么time函数返回的是自从UTC时间1970年1月1日 00:00:00到现在经过的秒数,如果32位有符号数存储这个值,那么在2038年这个值将溢出,无法存储这么大的数了。

这个函数传进去一个time_t指针,会将时间写入这个传入的内存,同时time也返回这个值,总感觉有点多余,要么用返回值,要么用指针。

int now = time(NULL); // 常用的形式

2、localtime

 struct tm *localtime(const time_t *timep);
 struct tm *localtime_r(const time_t *timep, struct tm *result);

 struct tm {
               int tm_sec;         /* seconds */
               int tm_min;         /* minutes */
               int tm_hour;        /* hours */
               int tm_mday;        /* day of the month */
               int tm_mon;         /* month */
               int tm_year;        /* year */
               int tm_wday;        /* day of the week */
               int tm_yday;        /* day in the year */
               int tm_isdst;       /* daylight saving time */
           };
/*
The members of the tm structure are:

       tm_sec    The  number of seconds after the minute, normally in the range 0 to
                 59, but can be up to 60 to allow for leap seconds.

       tm_min    The number of minutes after the hour, in the range 0 to 59.

       tm_hour   The number of hours past midnight, in the range 0 to 23.

       tm_mday   The day of the month, in the range 1 to 31.

       tm_mon    The number of months since January, in the range 0 to 11.

       tm_year   The number of years since 1900.
       tm_wday   The number of days since Sunday, in the range 0 to 6.

       tm_yday   The number of days since January 1, in the range 0 to 365.

       tm_isdst  A flag that indicates whether daylight saving time is in effect  at
                 the  time described.  The value is positive if daylight saving time
                 is in effect, zero if it is not, and negative if the information is
                 not available.

*/

这两个函数是将time_t的时间戳转化为本地且容易阅读的时间,关键是tm结构体,注释已经很清楚了。这两个函数的区别是前者是线程不安全的函数,后者是线程函数函数。你看第一个函数,平白无故的返回一个指针,很少看到这种情况,其实这个指针指向的是一个函数内部静态分配的结构体,有可能被其他线程重写,所以是不安全的,尽量用第二个。

3、gettimeofday

#include <sys/time.h>

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

struct timeval {
               time_t      tv_sec;     /* seconds */
               suseconds_t tv_usec;    /* microseconds */
           };
 struct timezone {
               int tz_minuteswest;     /* minutes west of Greenwich */
               int tz_dsttime;         /* type of DST correction */
           };

这个函数也可以得到当前的时间,而且可以达到微秒级别,由第一个参数tv指针传出,第二个参数tz已经废弃,传入NULL即可。

timeval结构体,保存秒数和微秒数,也是从UTC时间1970年1月1日 00:00:00算起的。

测试程序:

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

int main ()
{
  struct  timeval tv;
  gettimeofday(&tv,NULL);
  printf("second=%d\n", tv.tv_sec);
  printf("microseconds=%d\n", tv.tv_usec);
  printf("time=%d\n", time(0));
  return 0;
}
输出:
[KentZhang@LOCAL-192-168-97-2 ]$ ./a.out 
second=1543334965
microseconds=963832
time=1543334965

4、clock_gettime

#include <time.h>

int clock_gettime(clockid_t clk_id, struct timespec *tp);
/*
clk_id 有以下五种取值,我只用过前2个,后面3个待研究。
CLOCK_REALTIME // 获取实时时间,类似time()
              System-wide real-time clock.  Setting this clock requires  appropriate
              privileges.
CLOCK_MONOTONIC // 获取系统从启动到现在经历的时间
              Clock  that  cannot  be  set  and represents monotonic time since some
              unspecified starting point.
CLOCK_MONOTONIC_RAW (since Linux 2.6.28; Linux-specific)
              Similar to CLOCK_MONOTONIC, but provides access  to  a  raw  hardware-
              based time that is not subject to NTP adjustments.
CLOCK_PROCESS_CPUTIME_ID
              High-resolution per-process timer from the CPU.
CLOCK_THREAD_CPUTIME_ID
              Thread-specific CPU-time clock.
*/    

// 传出参数的结构体,保存秒和纳秒    
struct timespec {
    time_t   tv_sec;        /* seconds */
    long     tv_nsec;       /* nanoseconds */
};

据说这个函数是获取系统的时钟时间,和CPU的时钟有关系,所以精度才比较高。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值