当前位置: 技术问答>linux和unix
帮忙看下linux c下获取系统时间出现的bug
来源: 互联网 发布时间:2016-10-29
本文导语: 源程序如下: #include #include struct tm *g_timefields;//Global time int get_cur_time() { time_t timeticks; struct tm *timefields; timeticks = time(NULL); timefields = localtime(&timeticks); ...
源程序如下:
运行时首先出现如下情况:
Global time is 20101220_06
Test time is 20101220_06
Global time is 20101220_06
Test time is 20101220_06
Global time is 20101220_06
Test time is 20101220_06
。。。
这个时候我通过更改系统时间用命令“sudo date -s 07:00:01”,然后就开始打印如下的结果:
Global time is 20101220_06
Test time is 20101220_07
Global time is 20101220_07 //注意这一行
Test time is 20101220_07
Global time is 20101220_07
Test time is 20101220_07
Global time is 20101220_07
Test time is 20101220_07
Global time is 20101220_07
Test time is 20101220_07
Global time is 20101220_07
Test time is 20101220_07
Global time is 20101220_07
Test time is 20101220_07
Global time is 20101220_07
Test time is 20101220_07
。。。
请问我加粗的那一行中,为什么Global time 就变成“Global time is 20101220_07”了,其他地方我也没有动它啊?
谢谢指教!!!!
#include
#include
struct tm *g_timefields;//Global time
int get_cur_time()
{
time_t timeticks;
struct tm *timefields;
timeticks = time(NULL);
timefields = localtime(&timeticks);
return timefields;
}
int main()
{
struct tm *test;//Test time
g_timefields = get_cur_time();
while(1)
{
printf("Global time is %d%02d%02d_%02d n",g_timefields->tm_year + 1900, g_timefields->tm_mon + 1,
g_timefields->tm_mday, g_timefields->tm_hour);
test= get_cur_time();
printf("Test time is %d%02d%02d_%02d n",test->tm_year + 1900, test->tm_mon + 1,
test->tm_mday, test->tm_hour);
sleep(5);
}
return 0;
}
运行时首先出现如下情况:
Global time is 20101220_06
Test time is 20101220_06
Global time is 20101220_06
Test time is 20101220_06
Global time is 20101220_06
Test time is 20101220_06
。。。
这个时候我通过更改系统时间用命令“sudo date -s 07:00:01”,然后就开始打印如下的结果:
Global time is 20101220_06
Test time is 20101220_07
Global time is 20101220_07 //注意这一行
Test time is 20101220_07
Global time is 20101220_07
Test time is 20101220_07
Global time is 20101220_07
Test time is 20101220_07
Global time is 20101220_07
Test time is 20101220_07
Global time is 20101220_07
Test time is 20101220_07
Global time is 20101220_07
Test time is 20101220_07
Global time is 20101220_07
Test time is 20101220_07
。。。
请问我加粗的那一行中,为什么Global time 就变成“Global time is 20101220_07”了,其他地方我也没有动它啊?
谢谢指教!!!!
|
localtime()返回的是一个指针 所以global time依然指向被改变的时间结构体
你把g_timefields声明为一个结构体而不是结构体指针就好拉
你把g_timefields声明为一个结构体而不是结构体指针就好拉
|
int get_cur_time()
{
time_t timeticks; //
struct tm *timefields;
timeticks = time(NULL);
timefields = localtime(&timeticks);
return timefields;
}
这地方的问题!你的test和 g_timefields指向的是glibc的全局变量。
还有这个返回。。。。
你需要使用localtime_r
{
time_t timeticks; //
struct tm *timefields;
timeticks = time(NULL);
timefields = localtime(&timeticks);
return timefields;
}
这地方的问题!你的test和 g_timefields指向的是glibc的全局变量。
还有这个返回。。。。
你需要使用localtime_r
|
楼主你返回局部变量了,可以把
timefields = localtime(&timeticks);
修改为
g_timefields = localtime(&timeticks);
timefields = localtime(&timeticks);
修改为
g_timefields = localtime(&timeticks);