LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 936|回复: 3

请教关于tm结构体的奇怪问题

[复制链接]
发表于 2005-12-30 09:08:30 | 显示全部楼层 |阅读模式
#include<time.h>
void main(void)
{
        static time_t t1,t2;
        struct tm *tm1,*tm2;
        int long n;
        time(&t1);
        time(&t2);
        tm1=localtime(&t1);
        tm2=localtime(&t2);

        tm1->tm_year=110;
        tm1->tm_mon=1;
        tm1->tm_mday=2;
        tm1->tm_hour=3;
        tm1->tm_min=5;
        tm1->tm_sec=6;
        n=mktime(tm1);//用中间变量保存一下
        printf("time1()=%d\n",mktime(tm1));
        printf("time1()=%d\n",n);
        tm2->tm_year=110;
        tm2->tm_mon=1;
        tm2->tm_mday=2;
        tm2->tm_hour=3;
        tm2->tm_min=5;
        tm2->tm_sec=16;
        printf("%d\n",tm1->tm_year);
        printf("%d\n",tm1->tm_mon);
        printf("%d\n",tm1->tm_mday);
        printf("%d\n",tm1->tm_hour);
        printf("%d\n",tm1->tm_min);
        printf("%d\n",tm1->tm_sec);//此处输出值应该为6,实际输出16,感觉上是tm1被tm2覆盖了
        printf("%d\n",tm2->tm_year);
        printf("%d\n",tm2->tm_mon);
        printf("%d\n",tm2->tm_mday);
        printf("%d\n",tm2->tm_hour);
        printf("%d\n",tm2->tm_min);
        printf("%d\n",tm2->tm_sec);
        printf("time2()=%d\n",mktime(tm2)-mktime(tm1));//此处同样,输出值应该为10,实际输出0,
        printf("time2()=%d\n",mktime(tm2)-n);//用中间变量保存的输出正确
}

请大侠指教,谢谢!
发表于 2005-12-30 09:59:01 | 显示全部楼层
Post by neighborinfo
#include<time.h>
void main(void)
{
        static time_t t1,t2;
        struct tm *tm1,*tm2;
        int long n;
        time(&t1);
        time(&t2);
        tm1=localtime(&t1);
        tm2=localtime(&t2);

        /* 3 lines inserted here */
        if(tm1 = tm2) {
            printf("tm1 = tm2: %d\n", tm1);
        }

        tm1->tm_year=110;
        tm1->tm_mon=1;
        tm1->tm_mday=2;
        tm1->tm_hour=3;
        tm1->tm_min=5;
        tm1->tm_sec=6;
        n=mktime(tm1);//用中间变量保存一下
        printf("time1()=%d\n",mktime(tm1));
        printf("time1()=%d\n",n);
        tm2->tm_year=110;
        tm2->tm_mon=1;
        tm2->tm_mday=2;
        tm2->tm_hour=3;
        tm2->tm_min=5;
        tm2->tm_sec=16;
        printf("%d\n",tm1->tm_year);
        printf("%d\n",tm1->tm_mon);
        printf("%d\n",tm1->tm_mday);
        printf("%d\n",tm1->tm_hour);
        printf("%d\n",tm1->tm_min);
        printf("%d\n",tm1->tm_sec);//此处输出值应该为6,实际输出16,感觉上是tm1被tm2覆盖了
        printf("%d\n",tm2->tm_year);
        printf("%d\n",tm2->tm_mon);
        printf("%d\n",tm2->tm_mday);
        printf("%d\n",tm2->tm_hour);
        printf("%d\n",tm2->tm_min);
        printf("%d\n",tm2->tm_sec);
        printf("time2()=%d\n",mktime(tm2)-mktime(tm1));//此处同样,输出值应该为10,实际输出0,
        printf("time2()=%d\n",mktime(tm2)-n);//用中间变量保存的输出正确
}

请大侠指教,谢谢!

hi,
i think you should carefully read the man pages on those time functions. the return value of localtime() is a  pointer  to  static data  and hence are not thread-safe.Why dont you add these lines after calls to localtime():
                if(tm1 = tm2) {
                        printf("tm1 = tm2: %d\n", tm1);
                }
and then everything is clear.
回复 支持 反对

使用道具 举报

发表于 2005-12-30 10:01:15 | 显示全部楼层
localtime的返回值是内部静态变量,这里确实tm1被tm2覆盖了
这种情况还是用localtime_r吧
回复 支持 反对

使用道具 举报

发表于 2005-12-30 10:05:56 | 显示全部楼层
And another thing is that "void main()" should NOT even compile. It violates the standard(c and c++). main() is defined to have a "int" return value. So use int main(), never use void.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表