LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Closed Thread
  Search this Thread
Old 09-14-2012, 10:05 AM   #1
cbaurand
LQ Newbie
 
Registered: Sep 2012
Posts: 6

Rep: Reputation: Disabled
Thumbs down 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 ?
 
Old 09-18-2012, 05:32 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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/
 
  


Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Possibility to change local time zone SkyerSK LQ Suggestions & Feedback 4 01-22-2011 09:33 AM
change time zone ust Linux - Newbie 13 05-07-2010 09:45 PM
change NTP Time Zone eliufoo General 4 10-12-2008 05:52 AM
Change time zone procfs Linux - General 14 05-03-2006 10:16 PM
Time zone stuck in UTC - how to change? Ook Slackware 2 04-24-2005 10:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration