LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   problem with time zone change in c++ (https://www.linuxquestions.org/questions/linux-newbie-8/problem-with-time-zone-change-in-c-4175427252/)

cbaurand 09-14-2012 10:05 AM

problem with time zone change in c++
 
Hi all,

I have write a little function which can get the time zone value from a time zone name.
Here is the code:
Code:

  string getTimeZone(const string& zoneName)
    {
        struct tm *t1, *t2;
        time_t tm1, tm2;

        // LOCAL TIME (Europe/Zurich)
        tm1 = time(NULL);
        string tz = "TZ=";
       
        // set the new TZ value (TZ=zoneName)
        putenv((char *)(tz+zoneName).c_str()); 
       
        // check
        envTz = getenv("TZ");
        if (envTz != NULL)
                cout << "TZ2 = "<<envTz<<endl;
       
        long dtime = 0;

        // GMT time
        t2 = gmtime(&tm1);
        tm2 = mktime(t2);
        t1 = localtime(&tm1);
        dtime = (long)(tm1 - tm2);
        short min = (short)(abs(dtime) %3600/60);

        ostringstream os;
        os <<boost::format("%|0+3d|")%(short)(dtime/3600)<<":"<< boost::format("%|02|")%min;
       
        return os.str();
       
    }

int main(int        argc,char *        argv[])
{
    cout << "America/Denver : "<<getTimeZone("America/Denver")<<endl;
    cout << "America/Denver : "<<getTimeZone("America/Denver")<<endl;
    cout << "Indian/Comoro : " <<getTimeZone("Indian/Comoro")<<endl;
    cout << "Asia/Iran : " << getTimeZone("Asia/Iran")<<endl;;
    cout << "Canada/Atlantic : "<<getTimeZone("Canada/Atlantic")<<endl;
   
    return 0;

}

Result :
Code:

TZ2 = America/Denver
America/Denver : +01:00    <---- Problem
TZ2 = America/Denver
America/Denver : -07:00    <---- Same is correct !!!??!!
TZ2 = Indian/Comoro
Indian/Comoro : +03:00
TZ2 = Asia/Iran
Asia/Iran : +00:00
TZ2 = Canada/Atlantic
Canada/Atlantic : -04:00

As you can see, the first result is bad !

Any idea ?

colucix 09-18-2012 05:32 AM

Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate of https://www.linuxquestions.org/quest...-c-4175427768/


All times are GMT -5. The time now is 04:20 PM.