c语言中时间的获取.docx
《c语言中时间的获取.docx》由会员分享,可在线阅读,更多相关《c语言中时间的获取.docx(13页珍藏版)》请在冰豆网上搜索。
c语言中时间的获取
c语言中时间的获取
c语言中时间的获取
C语言的标准库函数包括一系列日期和时间处理函数,它们都在头文件中说明。
下面列出了这些函数。
在头文件中定义了三种类型:
time_t,structtm和clock_t。
在头文件中说明的C语言时间函数time_ttime(time_t*timer);doubledifftime(time_ttime1,time_ttime2);structtm*gmtime(consttime_t*timer);structtm*localtime(consttime_t*timer);char*asctime(conststructtm*timeptr);char*ctime(consttime_t*timer);size_tstrftime(char*s,size_tmaxsize,constchar*format,conststructtm*timeptr);time_tmktime(structtm*timeptr);clock_tclock(void);
下面是我从网上收集到的时间函数集asctime(将时间和日期以字符串格式表示)
相关函数
time,ctime,gmtime,localtime
表头文件
#include<time.h>
定义函数
char*asctime(conststructtm*timeptr);
函数说明
asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。
此函数已经由时区转换成当地时间,字符串格式为:
"WedJun3021:
49:
081993\n"
返回值
若再调用相关的时间日期函数,此字符串可能会被破坏。
此函数与ctime不同处在于传入的参数是不同的结构。
附加说明
返回一字符串表示目前当地的时间日期。
范例
#include<time.h>
main()
{
time_ttimep;
time(&timep);
printf("%s",asctime(gmtime(&timep)));
}
执行
SatOct2802:
10:
062000ctime(将时间和日期以字符串格式表示)
相关函数
time,asctime,gmtime,localtime
表头文件
#include<time.h>
定义函数
char*ctime(consttime_t*timep);
函数说明
ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。
此函数已经由时区转换成当地时间,字符串格式为"WedJun3021:
49:
081993\n"。
若再调用相关的时间日期函数,此字符串可能会被破坏。
返回值
返回一字符串表示目前当地的时间日期。
范例
#include<time.h>
main()
{
time_ttimep;
time(&timep);
printf("%s",ctime(&timep));
}
执行
SatOct2810:
12:
052000gettimeofday(取得目前的时间)
相关函数
time,ctime,ftime,settimeofday
表头文件
#include<time.h>
定义函数
intgettimeofday(structtimeval*tv,structtimezone*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<time.h>
main(){
structtimevaltv;
structtimezonetz;
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:
974857339
tv_usec:
136996
tz_minuteswest:
-540
tz_dsttime:
0gmtime(取得目前时间和日期)
相关函数
time,asctime,ctime,localtime
表头文件
#include<time.h>
定义函数
structtm*gmtime(consttime_t*timep);
函数说明
gmtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。
结构tm的定义为
structtm
{
inttm_sec;
inttm_min;
inttm_hour;
inttm_mday;
inttm_mon;
inttm_year;
inttm_wday;
inttm_yday;
inttm_isdst;
};
inttm_sec代表目前秒数,正常范围为0-59,但允许至61秒
inttm_min代表目前分数,范围0-59
inttm_hour从午夜算起的时数,范围为0-23
inttm_mday目前月份的日数,范围01-31
inttm_mon代表目前月份,从一月算起,范围从0-11
inttm_year从1900年算起至今的年数
inttm_wday一星期的日数,从星期一算起,范围为0-6
inttm_yday从今年1月1日算起至今的天数,范围为0-365
inttm_isdst日光节约时间的旗标
此函数返回的时间日期未经时区转换,而是UTC时间。
返回值
返回结构tm代表目前UTC时间
范例
#include<time.h>
main(){
char*wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_ttimep;
structtm*p;
time(&timep);
p=gmtime(&timep);
printf("%d%d%d",(1900+p->tm_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);
}
执行
2000/10/28Sat8:
15:
38localtime(取得当地目前时间和日期)
相关函数
time,asctime,ctime,gmtime
表头文件
#include<time.h>
定义函数
structtm*localtime(consttime_t*timep);
函数说明
localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。
结构tm的定义请参考gmtime()。
此函数返回的时间日期已经转换成当地时区。
返回值
返回结构tm代表目前的当地时间。
范例
#include<time.h>
main(){
char*wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_ttimep;
structtm*p;
time(&timep);
p=localtime(&timep);/*取得当地时间*/
printf("%d%d%d",(1900+p->tm_year),(l+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);
}
执行
2000/10/28Sat11:
12:
22mktime(将时间结构数据转换成经过的秒数)
相关函数
time,asctime,gmtime,localtime
表头文件
#include<time.h>
定义函数
time_tmktime(strcuttm*timeptr);
函数说明
mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0秒算起至今的UTC时间所经过的秒数。
返回值
返回经过的秒数。
范例
/*用time()取得时间(秒数),利用localtime()
转换成structtm再利用mktine()将structtm转换成原来的秒数*/
#include<time.h>
main()
{
time_ttimep;
strcuttm*p;
time(&timep);
printf("time():
%d\n",timep);
p=localtime(&timep);
timep=mktime(p);
printf("time()->localtime()->mktime():
%d\n",timep);
}
执行
time():
974943297
time()->localtime()->mktime():
974943297settimeofday(设置目前时间)
相关函数
time,ctime,ftime,gettimeofday
表头文件
#include<time.h>
定义函数
intsettimeofday(conststructtimeval*tv,conststructtimezone*tz);
函数说明
settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。
详细的说明请参考gettimeofday()。
注意,只有root权限才能使用此函数修改时间。
返回值
成功则返回0,失败返回-1,错误代码存于errno。
错误代码
EPERM并非由root权限调用settimeofday(),权限不够。
EINVAL时区或某个数据是不正确的,无法正确设置时间。
time(取得目前的时间)
相关函数
ctime,ftime,gettimeofday
表头文件
#include<time.h>
定义函数
time_ttime(time_t*t);
函数说明
此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。
如果t并非空指针的话,此函数也会将返回值存到t指针所指的内存。
返回值
成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。
范例
#include<time.h>
mian()
{
intseconds=time((time_t*)NULL);
printf("%d\n",seconds);
}
执行
9.73E+08
------------------------------------------------------------------------------------------------
关键字:
c语言时间函数time.hc语言时间函数,时间头函数
1,时间的获取:
通过time()函数来获得日历时间(CalendarTime),其原型为:
time_ttime(time_t*timer);
#include"stdafx.h"
#include"time.h"
#include"stdio.h"
#include"stdlib.h"
intmain(void)
{
structtmt;//定义tm时间结构,用来存储时间格式的数据信息
time_tt_of_day;//定义time_t时间结构
t.tm_year=2006-1900;//以1900年为标准计算时间
t.tm_mon=6;//为结构体成员赋值
t.tm_mday=1;
t.tm_hour=0;
t.tm_min=0;
t.tm_sec=1;
t.tm_isdst=0;
t_of_day=mktime(&t);
//使用mktime()函数将用tm结构表示的时间转化为日历时间:
time_t型变量。
其函数原型如下:
time_tmktime(structtm*timeptr);ctime()函数(参数为time_t结构)将时间以固定的格式显示出来,返回值是char*型的字符串。
return0;
}
2,时间的储存,通过预定义的两种结构来存储:
1,日历时间(CalendarTime)是通过time_t数据类型来表示的,用time_t表示的时间(日历时间)是从一个时间点(例如:
1970年1月1日0时0分0秒)到此时的秒数。
在time.h中,我们也可以看到time_t是一个长整型数:
#ifndef_TIME_T_DEFINED
typedeflongtime_t;/*时间值*/
#define_TIME_T_DEFINED/*避免重复定义time_t*/
#endif
2,在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下:
structtm{
inttm_sec;/*秒–取值区间为[0,59]*/
inttm_min;/*分-取值区间为[0,59]*/
inttm_hour;/*时-取值区间为[0,23]*/
inttm_mday;/*一个月中的日期-取值区间为[1,31]*/
inttm_mon;/*月份(从一月开始,0代表一月)-取值区间为[0,11]*/
inttm_year;/*年份,其值等于实际年份减去1900*/
inttm_wday;/*星期–取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推*/
inttm_yday;/*从每年的1月1日开始的天数–取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推*/
inttm_isdst;/*夏令时标识符,实行夏令时的时候,tm_isdst为正。
不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。
*/
};
3,时间的显示:
time.h头文件中提供了asctime()函数(参数为tm结构指针)和ctime()函数(参数为time_t结构)将时间以固定的格式显示出来,两者的返回值都是char*型的字符串。
返回的时间格式为:
星期几月份日期时:
分:
秒年\n\0;time.h还提供了两种不同的函数将日历时间(一个用time_t表示的整数)转换为我们平时看到的把年月日时分秒分开显示的时间格式tm:
structtm*gmtime(consttime_t*timer);
gmtime()函数是将日历时间转化为世界标准时间(即格林尼治时间),并返回一个tm结构体来保存这个时间
structtm*localtime(consttime_t*timer);localtime()函数是将日历时间转化为本地时间
#include<stdafx.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
intmain(void)
{
structtm*local,*ptr;//定义tm结构指针存储时间信息
time_tt;//时间结构或者对象
t=time(NULL);//获取当前系统的日历时间
//通过time()函数来获得日历时间(CalendarTime),
//其原型为:
time_ttime(time_t*timer);
local=localtime(&t);//localtime()函数是将日历时间转化为本地时间
printf("Localhouris:
%d\n",local->tm_hour);//输出tm结构体的时间成员
printf("UTChouris:
%d\n",local->tm_hour);
//local=gmtime(&t);
//gmtime()函数是将日历时间转化为世界标准时间(即格林尼治时间),
//并返回一个tm结构体来保存这个时间
ptr=gmtime(&t);//将日历时间转化为世界标准时间
printf("TheUTCtimeis%s\n",asctime(ptr));//格式化输出世界标准时间
printf("Thelocaltimeis%s\n",ctime(&t));//输出本地时间
/*asctime()函数(参数为tm结构指针)和ctime()函数(参数为time_t结构)将时间以固定的格式显示出来,两者的返回值都是char*型的字符串。
返回的时间格式为:
星期几月份日期时:
分:
秒年\n\0*/
return0;
}
4,时间差的计算:
所用函数:
C/C++中的计时函数是clock(),而与其相关的数据类型是clock_t。
在MSDN中对clock函数定义如下:
clock_tclock(void);函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clocktick)数,clock_t是一个长整形数,保存时间的数据类型。
在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:
#defineCLOCKS_PER_SEC((clock_t)1000)
每过千分之一秒(1毫秒),调用clock()函数返回的值就加1,时钟计时单元的长度为1毫秒,那么计时的精度也为1毫秒,那么我们可不可以通过改变CLOCKS_PER_SEC的定义,通过把它定义的大一些,从而使计时精度更高呢?
这样是不行的。
在标准C/C++中,最小的计时单位是一毫秒。
doubledifftime(time_ttime1,time_ttime0);这个函数来计算时间差。
#include"stdafx.h"
#include"time.h"
#include"stdio.h"
#include"stdlib.h"
intmain(void)
{
time_tc_start,t_start,c_end,t_end;
c_start=clock();
t_start=time(NULL);
system("pause");
c_end=clock();
t_end=time(NULL);
printf("Thepauseused%fmsbytime().\n",difftime(c_end,c_start));
printf("Thepauseused%fsbyclock().\n",difftime(t_end,t_start));
system("pause");
return0;
}
5,时间的其他用途
用作随机数的种子,由于时间获得的实际上是一个double类型的长整数,通过time(NULL)函数获得,作为srand(time(NULL))的种子产生随机数比较好。
#include"stdafx.h"
#include"time.h"
#include"stdio.h"
#include"stdlib.h"
intmain(void)
{
srand(time(NULL));
//设置种子,如果将这个函数注释掉,每次运行程序得到的随机数十相同的
for(inti=0;i<100;i++)
{
printf("%d\t",rand());
}
system("pause");
return0;
}