tags:

views:

440

answers:

4

In most of the example I had seen,

time_zone_ptr zone( new posix_time_zone("MST-07") ); 

But, I just want to get the current time zone for my running machine. I do not want to hard code the time zone name.

May I know how can I do so in boost?

+2  A: 

Wouldn't this be an OS-dependent call?

John at CashCommons
Not if Boost was well-written.
amphetamachine
I guess, but it seems like that's a lot to ask of a generic C++ library. C++ doesn't care what OS its compiled code is running on.
John at CashCommons
@amphetamachine: According to some, portably discovering the local timezone is hard: "http://www.chronos-st.org/Discovering%20the%20Local%20Time%20Zone--Why%20It's%20a%20Hard%20Problem.html". If you know how to do so, please post your patch to the Boost developers mailing list: http://www.boost.org/community/groups.html#main. I'm sure it will be received with much interest.
Éric Malenfant
@John, a major reason Boost exists is to provide OS independence. I'm very surprised they don't already have a function for this. As you will see in my answer to this question, they do have something similar...
rmeador
@rmeador, I may be speaking out of ignorance since I only have passing familiarity with Boost, but there are two ways to be OS-independent. One way is to simply avoid anything that might be OS-dependent. The other way is to shoulder the burden of making OS-dependent functions work on some set of OS's. I had thought Boost was more the former, but it might be the latter.
John at CashCommons
OK. I surrender. There are no way to Get Current Timezone In Boost
Yan Cheng CHEOK
+1  A: 

Well, maybe you could do it using the GeoIP library. I know it's a bit of an overkill, but since most computers in the world are connected to the internet, you could probably get away with it. According to the guy I'm developing for, it's been over 99% accurate.

Note: This is a dumb idea. I am just stretching for answers.

amphetamachine
Yeah it's dumb, but it's damn creative!
Tom
A: 

I am also looking for a solution to this problem, and my research hasn't turned up much, but it did turn up your question.

The closest thing to an answer I have found is an (undocumented, it seems) Boost class. It provides a way of converting from local time to UTC. There's an example of its usage (the only place it appears in the documentation AFAICT). I have not yet discovered a way of creating the actual posix_time_zone object for the local time, which is what I really need. I also need one for UTC, but that I think I can figure out by just hardcoding the string.

rmeador
A: 

Plain posix: call tzset, use tzname.

#include <ctime>
tzset();
time_zone_ptr zone(new posix_time_zone(tzname));

glibc/bsd:

time_zone_ptr zone(new posix_time_zone(localtime(0)->tm_zone));
Tobu