(转载)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.
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;
- (转载)C++两次调用localtime函数出错
- 拷贝构造函数执行后析构函数调用两次出错问题
- C语言中localtime函数
- c语言中localtime函数用法
- c/c++ 时间函数 localtime 坑
- localtime ()函数
- localtime函数
- C语言函数的调用约定-转载
- linux c 两次使用localtime,结果第一个被改变了
- c函数之时间函数time\localtime\mktime
- 调用 move_uploaded_file()函数出错
- C语言中localtime函数功能及用法
- C/C++中关于时间的函数 localtime()
- C语言中的时间函数localtime和gmtime
- C/C++时间函数time/localtime/mktime/ctime/strftime使用方法
- 小心C语言localtime和asctime时间函数陷阱
- C/C++时间函数time/localtime/mktime/ctime/strftime使用方法
- 为什么会两次调用析构函数
- ALU LDS
- 硅谷创业教父Paul Graham:如何获得创业idea (3)
- Java 连接MySQL
- 取得dropdown里面的optgroup 的值
- VC中OnPaint()
- (转载)C++两次调用localtime函数出错
- vs 团队模式 改回 开发模式
- STM32中断优先级
- [iOS]GCD小结
- Web Application UI(一):Web List Component: Beyond Simple HTML Table
- 关于select count(*)的讨论
- 选中文字就会弹出分享到QQ空间、或微博...
- HQL语言入门
- TLD动态跟踪系统中的学习策略—P-N Learning