time.h头文件中有如下几个常用函数:gmtime(),localtime(),ctime(),asctime(),mktime(),difftime(),time(),_mkgmtime()
- double difftime(time_t time1, time_t time0);
Finds the difference between two times.
获得两个time_t 时间的差(time1-time0),返回的是秒数。
- time_t mktime(struct tm * timeptr);
Convert the local time to a calendar value.
把一个时间(默认是本地时间)转换为UTC时间。(使用这个函数之后,它会根据当前时区是否使用夏令时,把tm结构体中的tm_isdst的值改变,当前为夏令时值为1,否则为0)。
也就是说比如本地时区是北京即+8时区,就算你的tm时间是UTC时间,转换结果也会比原来时间少8小时。
- time_t time(time_t * timer);
Return the time as seconds elapsed since midnight, January 1, 1970(UTC).
获得系统时间即(UTC时间)。
- char * asctime(const struct tm * timeptr);
Convert a tm time structure to a character string.
将tm结构体时间转换成一个字符串,格式如:Wen Jan 02 02:03:55 1980\n\0
- struct tm * gmtime(const time_t *timer);
Convert a time value to a structure.
把一个time_t 时间转化为tm结构体时间
当需要用转换之后的tm结构体中的tm_year时,注意这值是当前年减去1900,
tm_year Year (current year minus 1900).
- struct tm * localtime(const time_t * timer);
Convert a time value and correct for the local time zone.
把一个time_t 时间转化为tm结构体时间同时调整为本地时间。
- char * ctime(const time_t *timer);
Convert a time value to a string and adjust for local time zone settings.
把一个time_t 调整为本地时间,并转化为字符串形式,格式如:Wen Jan 02 02:03:55 1980\n\0
- time_t _mkgmtime(struct tm * timeptr);
Converts a UTC time represented by a tm struct to a UTC time represented by a time_t type.
-
注意事项:
1、gmtime(),localtime(),mktime(),mkgmtime(),使用这几个函数时,共用的是一个静态分配的tm结构体内存,也就是说调用任一函数时
会覆盖此内存中的tm结构体值,所以在使用时最好不要用指针指向这个空间。
//Both the 32-bit and 64-bit versions of
//gmtime, mktime, mkgmtime, and localtime all
//use a single tm structure per thread for the conversion.
//Each call to one of these routines destroys the result of the previous call.
比如下面的程序的运行结果:
(1)、
time_t t;
time(&t);
struct tm *tm1;
struct tm *tm2;
tm1 = gmtime(&t);
tm2 = localtime(&t);
cout<<"tm1: "<<asctime(tm1)<<endl;
cout<<"tm2: "<<asctime(tm2)<<endl;
结果:
(2)、
time_t t;
time(&t);
struct tm tm1;
struct tm tm2;
tm1 = *gmtime(&t);
tm2 = *localtime(&t);
cout<<"tm1: "<<asctime(&tm1)<<endl;
cout<<"tm2: "<<asctime(&tm2)<<endl;
结果:
这是你就能看到差距了,第一个结果一样是因为调用localtime函数时,覆盖了共用静态内存空间的值,所以输出的时候用的是
指向此内存空间的指针取值,所以结果一样。第二个,由于调用函数之后,马上将此静态存储空间的值赋值给一个tm变量,所以输出结果相差8小时。
2、asctime和ctime函数共用一个字符串buffer。
time_t t1;
char* ctm;
char* asctm;
struct tm tm1;
time(&t1);
cout<<"t1: "<<t1<<endl;
ctm = ctime(&t1);
cout<<"ctm立即输出: "<<ctm<<endl;
tm1 = *gmtime(&t1);
asctm = asctime(&tm1);
cout<<"asctm立即输出: "<<asctm<<endl;
cout<<"ctm在asctime函数调用之后输出: "<<ctm<<endl;
结果:
结果显示,立即输出的cmt和在调用asctime函数之后输出cmt结果相差8小时。
A call to ctime modifies the single statically allocated buffer used by thegmtime andlocaltime functions. Each call to one of these routines destroys the result of the previous call.ctime shares a static buffer with theasctime function. Thus, a call toctime destroys the results of any previous call toasctime,localtime, orgmtime.
3、mktime和_mkgmtime的区别
time_t t1;
time_t t2;
time_t t3;
struct tm tm1;
time(&t1);
cout<<"t1: "<<t1<<endl;
tm1 = *gmtime(&t1);
t2 = mktime(&tm1);
cout<<"t2: "<<t2<<endl;
t3 = _mkgmtime(&tm1);
cout<<"t3: "<<t3<<endl;结果:
从上面可以看出,t2时间比t1和t3时间相差的秒数刚好是8小时的秒数。注意其中_mkgmtime函数为C函数,而在C++中没有mkgmtime函数。