(转载)C++两次调用localtime函数出错

来源:互联网 发布:斗鱼看弹幕的软件 编辑:程序博客网 时间:2024/06/10 16:10

(转载)C++两次调用localtime函数出错

#include <stdio.h>#include <time.h>#include <sys/timeb.h>int main(){struct timeb tp_early;struct timeb tp_late;ftime ( &tp_early );sleep (5);ftime ( &tp_late );struct tm * tm_early;struct tm * tm_late;tm_early = localtime (& (tp_early.time));tm_late = localtime (& (tp_late.time ));printf ( "%02d:%02d:%02d:%d\n", ( tm_early -> tm_hour ) ,        ( tm_early -> tm_min ) ,( tm_early -> tm_sec ),                ( tp_early.millitm ) );printf ( "%02d:%02d:%02d:%d\n", ( tm_late -> tm_hour ) ,        ( tm_late -> tm_min ) ,( tm_late -> tm_sec ),                ( tp_late.millitm ) );return 0;}

输出范例:
07:35:11:499
07:35:11:503

程序在Linux下,总是时分秒位一样,但毫秒位不一样。按说应该秒位差5,又发现tm_early和tm_late地址相同,为什么会这样?

原因解答:

localtime
Converts a time value and corrects for the local time zone.

struct tm *localtime( const time_t *timer );

Routine Required Header Compatibility
localtime <time.h> ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

localtime returns a pointer to the structure result. If the value in timer represents a date before midnight, January 1, 1970, localtime returns NULL. The fields of the structure type tm store the following values, each of which is an int:

tm_sec
Seconds after minute (0 – 59)

tm_min
Minutes after hour (0 – 59)

tm_hour
Hours after midnight (0 – 23)

tm_mday
Day of month (1 – 31)

tm_mon
Month (0 – 11; January = 0)

tm_year
Year (current year minus 1900)

tm_wday
Day of week (0 – 6; Sunday = 0)

tm_yday
Day of year (0 – 365; January 1 = 0)

tm_isdst
Positive value if daylight saving time is in effect; 0 if daylight saving time is not in effect; negative value if status of daylight saving time is unknown. The C run-time library assumes the United States’s rules for implementing the calculation of Daylight Saving Time (DST).

Parameter

timer
Pointer to stored time

Remarks
The localtime function converts a time stored as a time_t value and stores the result in a structure of type tm. The long value timer represents the seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time (UTC). This value is usually obtained from the time function.

Warning!

gmtime, mktime, and localtime all use a single statically allocated tm structure for the conversion. Each call to one of these routines destroys the result of the previous call.

localtime corrects for the local time zone if the user first sets the global environment variable TZ. When TZ is set, three other environment variables (_timezone, _daylight, and _tzname) are automatically set as well. See _tzset for a description of these variables. TZ is a Microsoft extension and not part of the ANSI standard definition of localtime.

Note The target environment should try to determine whether daylight saving time is in effect.

看到终点标注出来的那一句了吧,我再简单的说一下,localtime函数返回的地址是在time.h里面自己定义的一个静态变量的地址,每次都是一样的。也就是第二次调用,会把第一调用时的值擦除,写入第二次的值。
修改方案:调整一下输出函数位置就可以了。即:

#include <stdio.h>#include <time.h>#include <sys/timeb.h>int main(){struct timeb tp_early;struct timeb tp_late;ftime ( &tp_early );sleep (5);ftime ( &tp_late );struct tm * tm_early;struct tm * tm_late;tm_early = localtime (& (tp_early.time));printf ( "%02d:%02d:%02d:%d\n", ( tm_early -> tm_hour ) ,( tm_early -> tm_min ) ,( tm_early -> tm_sec ), ( tp_early.millitm ) );tm_late = localtime (& (tp_late.time ));printf ( "%02d:%02d:%02d:%d\n", ( tm_late -> tm_hour ) , ( tm_late -> tm_min ) ,( tm_late -> tm_sec ), ( tp_late.millitm ) );return 0;