LinuxSir.cn,穿越时空的Linuxsir!

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

关于linux 下多线程 localtime 或 localtime_r 异常问题,提供一个类似的函数,仅供参

[复制链接]
发表于 2008-1-8 15:58:16 | 显示全部楼层 |阅读模式
前一段时间,发现系统提供的  localtime 或 localtime_r 在多线程下,根本没办法用,就自己写了一个,供各位同仁参考.
#include <public.h>
//void GetTime_tm(time_t current_time,struct tm *tempp);
//void GetTimeString(time_t current_time,char *mytime);
/*
int main()
{
    char mystring[30];
    time_t currtime;
    struct tm tempp;
    time(&currtime);
    GetTime_tm(currtime,&tempp);
cout << "time" << (1970+tempp.tm_year) << "month"<< (tempp.tm_mon+1) <<"day"<< tempp.tm_mday <<"hour"<< tempp.tm_hour <<"min"<< tempp.tm_min <<"sec"<< tempp.tm_sec << endl;
    GetTimeString(currtime,mystring);
    cout << "mytme="<<mystring<<endl;
}
*/
void GetTime_tm(time_t current_time,struct tm *tempp)
{
     int totalsecond = current_time;
//    int fouryear = (365*4+1);
         int fouryear = 1461;
        int oneday = 86400;
         int totalfouryears=0;
         int tempint0 =0;
         int tempint1 =0;
         int tempint2 =0;
         int tempint3 =0;
         int year = 0;
         int month =0;
         int day =0;
         int totaldays = (totalsecond /oneday);
         totalfouryears= totaldays/fouryear;
         tempint0 = (totaldays % fouryear);
         tempint1 = tempint0/365;
         tempint2 = (tempint0 % 365);
         tempint3 = (totalsecond % oneday);
//         cout<<"tempint1="<<tempint1<<  "tempint2="<<tempint2<<endl;
         if(tempint1<3)
         {
            year = 1970 + totalfouryears*4 + tempint1;
         }else if( tempint1 ==3 && tempint2==0)
        {
             year = 1970+totalfouryears*4+tempint1-1;
   
             tempint2 =365;
        }else if( tempint1 ==4 && tempint2==0)
        {
             year = 1970+totalfouryears*4+tempint1-1;
             tempint2 =364;
        }else
        {
            year = 1970+totalfouryears*4+tempint1;
            tempint2 -=1;
        }
        if(year % 4 != 0)
        {
            if(tempint2 < 31)
            {
                month=0;
                day = tempint2;
            }else if(tempint2 <59)  //31+28
            {
                month=1;
                 day=tempint2-31;
            }else if(tempint2 <90) //31+28+31
            {
                month=2;
                  day=tempint2-59; //-31-28
            }else if(tempint2 <120) //31+28+31+30
            {
                 month=3;
                  day=tempint2-90;//-31-28-31
            }else if(tempint2 <151) //31+28+31+30+31
            {
                 month=4;
                  day=tempint2-120; //-31-28-31-30
            }else if(tempint2 <181) //31+28+31+30+31+30
            {
                 month=5;
                  day=tempint2-151; //-31-28-31-30-31
            }else if(tempint2 <212) //31+28+31+30+31+30+31
            {
                 month=6;
                  day=tempint2-181; //-31-28-31-30-31-30
            }else if(tempint2 <243)  //31+28+31+30+31+30+31+31
            {
                 month=7;
                  day=tempint2-212; //-31-28-31-30-31-30-31
            }else if(tempint2 <273) //31+28+31+30+31+30+31+31+30
            {
                 month=8;
                  day=tempint2-243; //-31-28-31-30-31-30-31-31
            }else if(tempint2 <304) //31+28+31+30+31+30+31+31+30+31
            {
                 month=9;
                  day=tempint2-273; //-31-28-31-30-31-30-31-31-30
            }else if(tempint2 <334)//31+28+31+30+31+30+31+31+30+31+30
            {
                 month=10;
                  day=tempint2-304; //-31-28-31-30-31-30-31-31-30-31
            }else
            {
                 month=11;
                  day=tempint2-334; //-31-28-31-30-31-30-31-31-30-31-30
            }
   
        }else
        {  
            if(tempint2 < 31)
            {
                month=0;
                day = tempint2;
            }else if(tempint2 < 60)  // 31+29
            {
                month=1;
                 day=tempint2-31;
            }else if(tempint2 <91) //(31+29+31)
            {
                month=2;
                  day=tempint2-60; //-31-29
            }else if(tempint2 <121) //(31+29+31+30)
            {
                 month=3;
                  day=tempint2-91; //-31-29-31
            }else if(tempint2 <152) //(31+29+31+30+31)
            {
                 month=4;
                  day=tempint2-121; //-31-29-31-30
            }else if(tempint2 <182)//(31+29+31+30+31+30)
            {
                 month=5;
                  day=tempint2-152; //-31-29-31-30-31
            }else if(tempint2 <213) //(31+29+31+30+31+30+31)
            {
                 month=6;
                  day=tempint2-182;//31-29-31-30-31-30
            }else if(tempint2 <244)//(31+29+31+30+31+30+31+31)
            {
                 month=7;
                  day=tempint2-213; //-31-29-31-30-31-30-31
            }else if(tempint2 <274) //(31+29+31+30+31+30+31+31+30)
            {
                 month=8;
                  day=tempint2-244; //-31-29-31-30-31-30-31-31
            }else if(tempint2 <305) //(31+29+31+30+31+30+31+31+30+31)
            {
                 month=9;
                  day=tempint2-274; //-31-29-31-30-31-30-31-31-30
            }else if(tempint2 <335) //(31+29+31+30+31+30+31+31+30+31+30)
            {
                 month=10;
                  day=tempint2-305; //-31-29-31-30-31-30-31-31-30-31
            }else
            {
                 month=11;
                  day=tempint2-335; //-31-29-31-30-31-30-31-31-30-31-30
            }
        }
       tempp->tm_year = year-1970;
        tempp->tm_mon = month;
        tempp->tm_mday = day+1;
        tempp->tm_hour = tempint3 /3600;
        tempp->tm_min = (tempint3 % 3600) /60;
        tempp->tm_sec = (tempint3 % 60);
         
}
void GetTimeString(time_t current_time,char *mytime)
{
     int totalsecond = current_time;
//    int fouryear = (365*4+1);
         int fouryear = 1461;
        int oneday = 86400;
         int totalfouryears=0;
         int tempint0 =0;
         int tempint1 =0;
         int tempint2 =0;
         int tempint3 =0;
         int year = 0;
         int month =0;
         int day =0;
         int hour=0;
         int min = 0;
         int sec=0;
         int totaldays = (totalsecond /oneday);
         totalfouryears= totaldays/fouryear;
         tempint0 = (totaldays % fouryear);
         tempint1 = tempint0/365;
         tempint2 = (tempint0 % 365);
         tempint3 = (totalsecond % oneday);
         if(tempint1<3)
         {
            year = 1970 + totalfouryears*4 + tempint1;
         }else if( tempint1 ==3 && tempint2==0)
        {
             year = 1970+totalfouryears*4+tempint1-1;
             tempint2 =365;
        }else if( tempint1 ==4 && tempint2==0)
        {
             year = 1970+totalfouryears*4+tempint1-1;
             tempint2 =364;
        }else
        {
            year = 1970+totalfouryears*4+tempint1;
            tempint2 -=1;
        }
        if(year % 4 != 0)
        {
            if(tempint2 < 31)
            {
                month=0;
                day = tempint2;
            }else if(tempint2 <59)  //31+28
            {
                month=1;
                 day=tempint2-31;
            }else if(tempint2 <90) //31+28+31
            {
                month=2;
                  day=tempint2-59; //-31-28
            }else if(tempint2 <120) //31+28+31+30
            {
                 month=3;
                  day=tempint2-90;//-31-28-31
            }else if(tempint2 <151) //31+28+31+30+31
            {
                 month=4;
                  day=tempint2-120; //-31-28-31-30
            }else if(tempint2 <181) //31+28+31+30+31+30
            {
                 month=5;
                  day=tempint2-151; //-31-28-31-30-31
            }else if(tempint2 <212) //31+28+31+30+31+30+31
            {
                 month=6;
                  day=tempint2-181; //-31-28-31-30-31-30
            }else if(tempint2 <243)  //31+28+31+30+31+30+31+31
            {
                 month=7;
                  day=tempint2-212; //-31-28-31-30-31-30-31
            }else if(tempint2 <273) //31+28+31+30+31+30+31+31+30
            {
                 month=8;
                  day=tempint2-243; //-31-28-31-30-31-30-31-31
            }else if(tempint2 <304) //31+28+31+30+31+30+31+31+30+31
            {
                 month=9;
                  day=tempint2-273; //-31-28-31-30-31-30-31-31-30
            }else if(tempint2 <334)//31+28+31+30+31+30+31+31+30+31+30
            {
                 month=10;
                  day=tempint2-304; //-31-28-31-30-31-30-31-31-30-31
            }else
            {
                 month=11;
                  day=tempint2-334; //-31-28-31-30-31-30-31-31-30-31-30
            }
   
        }else
        {  
            if(tempint2 < 31)
            {
                month=0;
                day = tempint2;
            }else if(tempint2 < 60)  // 31+29
            {
                month=1;
                 day=tempint2-31;
            }else if(tempint2 <91) //(31+29+31)
            {
                month=2;
                  day=tempint2-60; //-31-29
            }else if(tempint2 <121) //(31+29+31+30)
            {
                 month=3;
                  day=tempint2-91; //-31-29-31
            }else if(tempint2 <152) //(31+29+31+30+31)
            {
                 month=4;
                  day=tempint2-121; //-31-29-31-30
            }else if(tempint2 <182)//(31+29+31+30+31+30)
            {
                 month=5;
                  day=tempint2-152; //-31-29-31-30-31
            }else if(tempint2 <213) //(31+29+31+30+31+30+31)
            {
                 month=6;
                  day=tempint2-182;//31-29-31-30-31-30
            }else if(tempint2 <244)//(31+29+31+30+31+30+31+31)
            {
                 month=7;
                  day=tempint2-213; //-31-29-31-30-31-30-31
            }else if(tempint2 <274) //(31+29+31+30+31+30+31+31+30)
            {
                 month=8;
                  day=tempint2-244; //-31-29-31-30-31-30-31-31
            }else if(tempint2 <305) //(31+29+31+30+31+30+31+31+30+31)
            {
                 month=9;
                  day=tempint2-274; //-31-29-31-30-31-30-31-31-30
            }else if(tempint2 <335) //(31+29+31+30+31+30+31+31+30+31+30)
            {
                 month=10;
                  day=tempint2-305; //-31-29-31-30-31-30-31-31-30-31
            }else
            {
                 month=11;
                  day=tempint2-335; //-31-29-31-30-31-30-31-31-30-31-30
            }
        }
        hour = tempint3 /3600;
        min = (tempint3 % 3600) /60;
        sec = (tempint3 % 60);
       sprintf(mytime,"%.4d%.2d%.2d%.2d%.2d%.2d",year,(month+1),(day+1),hour,min,sec);

//       tempp->tm_year = year-1970;
//        tempp->tm_mon = month;
//        tempp->tm_mday = day;
//        tempp->tm_hour = tempint3 /3600;
//        tempp->tm_min = (tempint3 % 3600) /60;
//        tempp->tm_sec = (tempint3 % 60);
         
}
发表于 2008-1-8 21:26:52 | 显示全部楼层
localtime返回一个指向静态变量的指针,这样是线程不安全的,但localtime_r应该是线程安全的吧
回复 支持 反对

使用道具 举报

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

本版积分规则

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