linux/unix下常用时间函数

1.与时间有关的数据类型

常用的主要有time_t, struct timeb, struct timeval, struct timezone, struct timespec, clock_t,struct tm。
(1).time_t : 这个变量是一个long型,一般用来表示从1970-01-01 00:00:00时以来的秒数,精确度:秒;由函数time()获取。定义在#include <time.h>中。
目前相当一部分操作系统使用32位二进制数字表示时间。此类系统的Unix时间戳最多可以使用到格林威治时间2038年01月19日03时14分07秒(二进制:01111111 11111111 11111111 11111111)。其后一秒,二进制数字会变为10000000 00000000 00000000 00000000,发生溢出错误,造成系统将时间误解为1901年12月13日20时45分52秒。这很可能会引起软件故障,甚至是系统瘫痪。使用64位二进制数字表示时间的系统(最多可以使用到格林威治时间292,277,026,596年12月04日15时30分08秒)则基本不会遇到这类溢出问题。

(2).struct timeb 结构:它有两个主要成员,一个是秒,另一个是毫秒;精确度:毫秒(10E-3秒)。
由函数ftime()获取struct timeb结构的时间;其定义如下:

     struct timeb{
        time_t  time; //秒                    
        unsigned short millitm;//毫秒    
        short   timezone; //Minutes west of GMT
        short   dstflag; // Nonzero if Daylight Savings Time used       
      };
      #include <sys/timeb.h>
      int  ftime(struct  timeb*  tp);
  调用成功返回0;调用失败返回-1。

(3).struct timeval 结构,它有两个成员;一个是秒,另一个表示微秒,精确度:微秒(10E-6)。
由函数gettime0fday()获取;
struct timeval结构定义为:

    struct  timeval{
          long  tv_sec;   //秒
          long  tv_usec;  //微秒
      };
  读取struct timeval结构数据的函数说明:
      #include  <sys/time.h>
      int  gettimeofday(struct  timeval*  tv,struct  timezone*  tz);
  该函数会提取系统当前时间,并把时间分为秒和微秒两部分填充到结构struct  timeval中;
  同时把当地的时区信息填充到结构struct  timezone中;
  返回值:成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。

(4).struct timezone结构的定义为:

    struct  timezone{
         int  tz_minuteswest; 
         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

(5).struct timespec 结构:它是POSIX.4标准定义的一个时间结构,精确度:纳秒(10E-9秒)。
由函数 clock_gettime 获取当前系统struct timespec结构的时间;其定义如下:

     struct  timespec{
          time_t    tv_sec;  //秒         
          long       tv_nsec; //纳秒    
     };
     typedef   struct  timespec   timespec_t;
 该结构定义在头头文件 `/usr/include/time.h` 中;
    int clock_gettime (clock_t which_clock, struct timespec *tp);

(6).clock_t 类型:由函数clock()获取。

    #include  <time.h>
     clock_t   clock(void);
 linux下该函数以微秒的方式返回CPU的时间;
     typedef    long   clock_t;

(7).struct tm 结构:由函数gmtime()解析time_t得到。

     struct tm*gmtime(const time_t*timep);
  函数说明:gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后
  将结果由结构tm返回。
struct tm{
    int tm_sec;     // 代表目前秒数,正常范围为0-59 ,但允许至61 
    int tm_min;     // 代表目前分数,范围为0-59 
    int tm_hour;        // 从午夜算出的时数,范围 0-23 
    int tm_mday;        // 目前月份的日期,范围为 1-31 
    int tm_mon;     // 代表目前月份,从1月算起,范围为 0-11 
    int tm_year;        // 从1900年算起至今的年数 
    int tm_wday;        // 一个星期的日数,从星期一算起,范围为0-6 
    int tm_yday;        // 从今年1月1日算起至今的天数,范围为0-365
    int tm_isdst;       //日光节能时间的旗标
};

注意:#include <time.h>和#include <sys/time.h>后者总包括了前者

2.常用的时间函数

Linux下常用时间函数有:clock()、time( )、ctime( )、gmtime( )、localtime( )、mktime( )、asctime( )、difftime( )、gettimeofday( )、settimeofday( )

(1).clock_t clock (void);
头文件:#include <time.h>
函数说明:返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数
返 回 值:标准POSIX平台下1000000个计时单位为1秒,所以返回的单位相当于us(微秒)
windows下1000个计时单元为1秒,返回的单位相当于ms(毫秒)

(2).time_t time(time_t * t);
头文件:#include <time.h>
函数说明:返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。
如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。time_t = time(0);或者time_t t;time(&t);
返 回 值:成功则返回秒数,失败则返回((time_t)-1)值错误原因存于errno中。

(3).double difftime(time_t time1, time_t time0);
头文件:#include <time.h>
函数说明:difftime( )比较参数timep1和timep2时间是否相同,并返回之间相差秒数。
返 回 值:返回相差秒数,注意返回类型为double,可正可负。
示例:

#include <stdio.h>
#include <time.h>
int main ()
{
  time_t timer;
  struct tm y2k;
  double seconds;
  y2k.tm_hour = 0;   y2k.tm_min = 0; y2k.tm_sec = 0;
  y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;
  time(&timer);  /* get current time; same as: timer = time(NULL)  */
  seconds = difftime(timer,mktime(&y2k));
  printf ("%.f seconds since January 1, 2000 in the current timezone", seconds);
  return 0;
}

(4).time_t mktime(struct tm * timeptr);
头文件:#include <time.h>
函数说明:mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。
返 回 值:返回经过的秒数。
示例: 用time()取得时间(秒数),利用localtime()转换成struct tm,再利用mktine()将struct tm转换成原来的秒数

#include <stdio.h> 
#include <time.h>
int main(){
time_t timep = time(0);
strcut tm *p;
printf("time() : %d \n",timep);
p=localtime(timep);
timep = mktime(p);
printf("time()->localtime()->mktime():%d\n",timep);
return 0;
}

执行结果:

time():974943297
time()->localtime()->mktime():974943297

(5).char * asctime(const struct tm * timeptr);
头文件:#include <time.h>
函数说明:将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为:”Wed Jun 30 21:49:08 1993\n”
返 回 值:若再调用相关的时间日期函数,此字符串可能会被破坏。此函数与ctime不同处在于传入的参数是不同的结构。
附加说明:返回一字符串表示目前当地的时间日期
示例:

#include <stdio.h> 
#include <time.h>
int main(){
    time _t timep=time(0);
    printf("%s",asctime(gmtime(&timep)));
    return 0;
}

执行结果:

Sat Oct 28 02:10:06 2015

(6).char * ctime(const time_t *timep);
头文件:#include <time.h>
函数说明: 将时间和日期以字符串格式表示
ctime ()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地 时间,字符串格式为”Wed Jun 30 21 :49 :08 1993\n”。若再调用相关的时间日期函数,此字符串可能会被破坏。
返 回 值:返回一字符串表示目前当地的时间日期。
示例:

#include <stdio.h> 
#include <time.h>
int main(){
time_t timep;
time(&timep);
printf("%s",ctime(&timep));
return 0;
}

执行结果:

Sat Oct 28 10:12:05 2015

(7).struct tm* gmtime (const time_t* timep);
头文件:#include <time.h>
函数说明:gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,
然后将结果由结构tm返回。
返 回 值:返回结构体tm,返回的日期未经时区转换,而是UTC时间。
示例:

#include <stdio.h> 
#include <time.h>
int main(){
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm *p;
time(timep);
p=gmtime(timep);
printf("%d/%d/%d",(1900+p->m_year), (1+p->tm_mon),p->tm_mday);
printf("%s%d:%d:%d\n", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
return 0;
}

执行结果:

2015/10/28 Sat 8:15:38

(8).struct tm* localtime (const time_t* _v);
头文件:#include <time.h>
函数说明:将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。
和gmtime区别就是此函数返回的时间日期已经转换成当地时区。
返 回 值:返回结构tm代表目前的当地时间,已转换为当地时区。

(9).int gettimeofday(struct timeval*tv, struct timezone *tz);
头文件:#include <sys/time.h>
函数说明:gettimeofday()会把目前的时间有tv所指的结构返回,timezone 参数若不使用则传入NULL即可。
当地时区的信息则放到timeval结构体中。

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所指的内存空间超出存取权限。
它获得的时间精确到微秒(1e-6 s)量级。
示例:

#include <stdio.h> 
#include <sys/time.h>
int main(){
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;
}

执行结果:

tv_sec: 974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0

(10). int settimeofday ( const struct timeval *tv,const struct timezone *tz);
头文件:#include <sys/time.h>
函数说明:settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。注意,只有root权限才能使用此函数修改时间。
返回值:成功则返回0,失败返回-1,错误代码存于errno。
错误代码:
EPERM 并非由root权限调用settimeofday(),权限不够。
EINVAL 时区或某个数据是不正确的,无法正确设置时间。

(11).时间格式化函数 strftime
函数:size_t strftime(char *str,size_t max,char *farmat,struct tm *timeptr);
头文件:#include <time.h>
strftime有点像sprintf,其格式由fmt来指定。
输入:struct tm *timeptr,const char *format
输出:const char *buf

    %a : 本第几天名称,缩写
     %A : 本第几天名称,全称
     %b : 月份名称,缩写
     %B : 月份名称,全称
     %c : 与ctime/asctime格式相同
     %d : 本月第几日名称,由零算起
     %H : 当天第几个小时,24小时制,由零算起
     %I : 当天第几个小时,12小时制,由零算起
     %j : 当年第几天,由零算起
     %m : 当年第几月,由零算起
     %M : 该小时的第几分,由零算起
     %p : AM或PM
     %S : 该分钟的第几秒,由零算起
     %U : 当年第几,由第一个日开始计算
     %W : 当年第几,由第一个一开始计算
     %w : 当第几日,由零算起
     %x : 当地日期
     %X : 当地时间
     %y : 两位数的年份
     %Y : 四位数的年份
     %Z : 时区名称的缩写
     %% : %符号
 示例:
int main(void)  
{  
    char strtime[20] = {0};  
    time_t timep;  
    struct tm *p_tm;  
    timep = time(NULL);  
    p_tm = localtime(&timep);  
    strftime(strtime, sizeof(strtime), "%Y-%m-%d %H:%M:%S", p_tm);  
    return 0;  
}  

(12).时间格式化函数 strptime
函数:char * strptime(char *buf,char *format,struct tm *timeptr);
如同scanf一样,解译字串成为tm格式
format字符串的构建方式和strftime的format字符串完全一样,strptime返回一个指针,
指向转换过程处理的最后一个字符后面的那个字符,
输入:const char *buf,const char *format
输出:struct tm *timeptr

     %h : 与%b%B%c : 读取%x%X格式
     %C : 读取%C格式
     %e : 与%d%D : 读取%m/%d/%y格式
     %k : 与%H%l : 与%I%r : 读取"%I:%M:%S %p"格式
     %R : 读取"%H:%M"格式
     %T : 读取"%H:%M:%S"格式
     %y : 读取两位数年份
     %Y : 读取四位数年份
 示例:
#include <time.h>  
#include <stdio.h>  
#include <string.h>  
int main() {  
    struct tm tm;   
    char buf[255];    
    strptime("24/Aug/2011:09:42:35", "%d/%b/%Y:%H:%M:%S" , &tm);  
    printf("asctime:%s",asctime(&tm)); 
    memset(buf,0,sizeof(buf));  
    strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm);  
    printf("%s\n",buf);  
    return 0;  
   }   

(13).获得ns级时间clock_gettime
函数: int clock_gettime (clock_t which_clock, struct timespec *tp);
头文件:#include<time.h>
which_clock参数解释
CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时
CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间
CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间
struct timespec结构:

     struct timespec{
         time_t tv_sec;    //秒
         long int tv_nsec;//纳秒
     };
示例:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
    struct timespec time_start={0, 0},time_end={0, 0};
    clock_gettime(CLOCK_REALTIME, &time_start);
    printf("start time %llus,%llu ns\n", time_start.tv_sec, time_start.tv_nsec);
    clock_gettime(CLOCK_REALTIME, &time_end);
    printf("endtime %llus,%llu ns\n", time_end.tv_sec, time_end.tv_nsec);
    printf("duration:%llus %lluns\n", time_end.tv_sec-time_start.tv_sec, time_end.tv_nsec-time_start.tv_nsec);
    return 0;
}

执行结果:

start time 1397395863s,973618673 ns
endtime 1397395863s,973633297 ns
duration:0s 14624ns
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值