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 ?