各种time函数用法总结

来源:互联网 发布:叶璇金星知乎 编辑:程序博客网 时间:2024/06/10 12:31

一 、C语言gettimeofday()函数:获取当前时间
头文件:

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

定义函数:int gettimeofday (struct timeval * tv, struct timezone * tz);
函数说明:gettimeofday()会把目前的时间有tv 所指的结构返回,当地时区的信息则放到tz 所指的结构中。
timeval 结构定义为:

structtimeval{  longtv_sec; //秒  longtv_usec; //微秒};

timezone 结构定义为:

structtimezone{  inttz_minuteswest; //和Greenwich 时间差了多少分钟  inttz_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 <sys/time.h>#include <unistd.h>main(){  structtimeval tv;  structtimezone 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);}

执行结果:

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

补充:
1、它获取 time_zone 时区 当前 的时间值, 时间值 等于 1/1/1970 到现在的秒数
2、它获得的时间精确到微秒(1e-6 s)量级。在一段代码前后分别使用gettimeofday可以计算代码执行时间
3、C语言编写程序需要获得当前精确时间(1970年1月1日到现在的时间),或者为执行计时,可以使用gettimeofday()函数
4、第二个形参是基于平台实现的,使用的时候最好用NULL
5、gettimeofday()会把目前的时间用tv 结构体返回,当地时区的信息则放到tz所指的结构中
这里写图片描述

如图上所示:
当需要每个5s检查或发送一下某个状态时可以采用,gettimeofday函数两次相减,再将tv_end赋值给tv_start的方式进行。

二、C语言settimeofday()函数:设置当前时间戳
头文件:
1

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

定义函数:

int settimeofday(const struct timeval *tv, const struct timezone *tz);

函数说明:settimeofday()会把目前时间设成由tv 所指的结构信息,当地时区信息则设成tz 所指的结构。详细的说明请参考gettimeofday()。
注意,在Linux下,只有root 权限才能使用此函数修改时间。
返回值:成功则返回0,失败返回-1,错误代码存于errno。
错误代码:
EPERM 并非由root 权限调用settimeofday(),权限不够。
EINVAL 时区或某个数据是不正确的,无法正确设置时间。

三、time函数:获取当前时间(以秒数表示)

头文件:#include <time.h>定义函数:time_t time(time_t *t);

函数说明:此函数会返回从公元 1970 年1 月1 日的UTC 时间从0 时0 分0 秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t 指针所指的内存。
返回值:成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno 中。

功能: 获取当前的系统时间,返回的结果是一个time_t类型,其实就是一个大整数,其值表示从CUT(Coordinated Universal Time)时间1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数。然后调用localtime将time_t所表示的CUT时间转换为本地时间(我们是+8区,比CUT多8个小时)并转成struct tm类型,该类型的各数据成员分别表示年月日时分秒。
补充说明:time函数的原型也可以理解为 long time(long *tloc),即返回一个long型整数

#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;  }  

四、其他时间

头文件time.h @函数名称:     localtime 函数原型:     struct tm *localtime(const time_t *timer) 函数功能:     返回一个以tm结构表达的机器时间信息 函数返回:     以tm结构表达的时间,结构tm定义如下: [cpp] view plain copystruct  tm{         int tm_sec;         int tm_min;         int tm_hour;         int tm_mday;         int tm_mon;         int tm_year;         int tm_wday;         int tm_yday;         int tm_isdst;       };   参数说明:     timer-使用time()函数获得的机器时间 [cpp] view plain copy#include <time.h>   #include <stdio.h>   #include <dos.h>   int main() {       time_t timer;       struct tm *tblock;       timer=time(NULL);       tblock=localtime(&timer);       printf("Local time is: %s",asctime(tblock));       return 0;   }   @函数名称:     asctime 函数原型:     char* asctime(struct tm * ptr) 函数功能:     得到机器时间(日期时间转换为ASCII码) 函数返回:     返回的时间字符串格式为:星期,月,日,小时:分:秒,年 参数说明:     结构指针ptr应通过函数localtime()和gmtime()得到 所属文件:     <time.h> [cpp] view plain copy#include <stdio.h>   #include <string.h>   #include <time.h>   int main() {       struct tm t;       char str[80];       t.tm_sec=1;       t.tm_min=3;       t.tm_hour=7;       t.tm_mday=22;       t.tm_mon=11;       t.tm_year=56;       t.tm_wday=4;       t.tm_yday=0;       t.tm_isdst=0;       strcpy(str,asctime(&t));       printf("%s",str);       return 0;   }   @函数名称:     ctime 函数原型:     char *ctime(long time) 函数功能:     得到日历时间 函数返回:     返回字符串格式:星期,月,日,小时:分:秒,年 参数说明:     time-该参数应由函数time获得 所属文件:     <time.h> [cpp] view plain copy#include <stdio.h>   #include <time.h>   int main() {       time_t t;       time(&t);       printf("Today's date and time: %s",ctime(&t));       return 0;   }   @函数名称:     difftime 函数原型:     double difftime(time_t time2, time_t time1) 函数功能:     得到两次机器时间差,单位为秒 函数返回:     时间差,单位为秒 参数说明:     time1-机器时间一,time2-机器时间二.该参数应使用time函数获得 所属文件:     <time.h> [cpp] view plain copy#include <time.h>   #include <stdio.h>   #include <dos.h>   #include <conio.h>   int main() {       time_t first, second;       clrscr();       first=time(NULL);       delay(2000);       second=time(NULL);       printf("The difference is: %f seconds",difftime(second,first));       getch();       return 0;   }   @函数名称:     gmtime 函数原型:     struct tm *gmtime(time_t  *time) 函数功能:     得到以结构tm表示的时间信息 函数返回:     以结构tm表示的时间信息指针 参数说明:     time-用函数time()得到的时间信息 所属文件:     <time.h> [cpp] view plain copy#include <stdio.h>   #include <stdlib.h>   #include <time.h>   #include <dos.h>   char *tzstr="TZ=PST8PDT";   int main() {       time_t t;       struct tm *gmt, *area;       putenv(tzstr);       tzset();       t=time(NULL);       area=localtime(&t);       printf("Local time is:%s", asctime(area));       gmt=gmtime(&t);       printf("GMT is:%s", asctime(gmt));       return 0;   }   @函数名称:     time 函数原型:     time_t time(time_t *timer) 函数功能:     得到机器的日历时间或者设置日历时间 函数返回:     机器日历时间 参数说明:     timer=NULL时得到机器日历时间,timer=时间数值时,用于设置日历时间,time_t是一个long类型 所属文件:     <time.h> [cpp] view plain copy#include <time.h>   #include <stdio.h>   #include <dos.h>   int main() {       time_t t;       t=time();       printf("The number of seconds since January 1,1970 is %ld",t);       return 0;   }   @函数名称:     tzset 函数原型:     void tzset(void) 函数功能:     UNIX兼容函数,用于得到时区,在DOS环境下无用途 函数返回: 参数说明: 所属文件:     <time.h> [cpp] view plain copy#include <time.h>   #include <stdlib.h>   #include <stdio.h>   int main() {       time_t td;       putenv("TZ=PST8PDT");       tzset();       time(&td);       printf("Current time=%s",asctime(localtime(&td)));       return 0;   }  
0 0
原创粉丝点击