ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You can concatenate two std::string without any problem....
It's not the problem here.
Yes, you are correct... concatenating strings is possible using the + operator. However, your second statement is incorrect (the problem is with the usage of putenv()).
The putenv() function accepts a pointer to char (a string), which becomes part of the environment. In other words, it takes ownership of the pointer. You are passing pointer to char of a temporary object.
From the man-page:
Code:
DESCRIPTION
The putenv() function adds or changes the value of environment variables. The argument string is
of the form name=value. If name does not already exist in the environment, then string is added to
the environment. If name does exist, then the value of name in the environment is changed to
value. The string pointed to by string becomes part of the environment, so altering the string
changes the environment.
I've attempted to fix your code to the best of my ability, however valgrind complains whether I allocate on the heap using "new" or whether using the C function strdup(). Here's that code:
Code:
std::string getTimeZone(const std::string& zoneName)
{
// set the new TZ value (TZ=zoneName)
std::string tz = "TZ=" + zoneName;
/*
char* env_tz = new char[tz.size() + 1];
memcpy(env_tz, tz.c_str(), tz.size());
env_tz[tz.size()] = '\0';
*/
char* env_tz = strdup(tz.c_str());
putenv(env_tz);
// LOCAL TIME (Europe/Zurich)
time_t tm1 = time(NULL);
// GMT time
struct tm* t2 = gmtime(&tm1);
time_t tm2 = mktime(t2);
localtime(&tm1);
std::ostringstream os;
long dtime = (long)(tm1 - tm2);
short min = (short)(abs(dtime) % 3600/60);
os << boost::format("%|0+3d|") % (short)(dtime / 3600) << ":" << boost::format("%|02|") % min;
return os.str();
}
Last edited by dwhitney67; 09-18-2012 at 05:52 AM.
Tell you what's irritating: when you spend your morning trying to work out what the heck is wrong with the code (sorry for my wrong pointer earlier) only to find dwhitney67's done it first *eyeroll* At least I now think I know a lot more about C's timezone functions...
It seems to work "correctly" now. More one thing is bad now: the result for America/Denver should be -06:00. The function localtime() doesn't consider the Daylight Saving Time.
I need now investigate about that.
It seems to work "correctly" now. More one thing is bad now: the result for America/Denver should be -06:00. The function localtime() doesn't consider the Daylight Saving Time.
I need now investigate about that.
What value does the following report on your system? On my system, it reports that DST is 0 (which is correct). Maybe it is coincidence that it works. I was always under the impression that Linux developers gave up on trying to support DST readings because it changes often (by governments).
I got DST = 0.
After some investigations, i found that the problem comes from mktime. To avoid mismatch between winter and summer hour, just set the tm_isdst to -1.
It works fine now.
If you consider the problem solved, then please mark the thread 'SOLVED' (in thread tools, or a link at the top of the page) and mark any of dwhitney's posts that you found helpful as helpful
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.