6 Answers
6

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.

The above are abbreviated Posix timezones, defined in terms of offset from UTC and not stable over time (there's a longer form that can include DST transitions, but not political and historical transitions).

ICU is portable and has logic for retrieving the system timezone as an Olson timezone (snippet by sumwale):

On Linux, ICU is implemented to be compatible with tzset and looks at TZ and /etc/localtime, which on recent Linux systems is specced to be a symlink containing the Olson identifier (here's the history). See uprv_tzname for implementation details.

Boost doesn't know how to use the Olson identifier. You could build a posix_time_zone using the non-DST and DST offsets, but at this point, it's best to keep using the ICU implementation. See this Boost FAQ.

I can't speak for other environments, but MSVS 2008's implementation of _get_tzname() returns a name, like "Pacific Standard Time"; Boost's posix_time_zone wants something like "PST". Also, btw, tzname (POSIX, standard calls it _tzname) is a 2-D char array. [MSVC also supplies a TZNAME_MAX, if you #define _POSIX_, but set to 10, apparently an oversight from an earlier implementation.]
–
Mike CMay 15 '12 at 16:50

1

I wrote "Boost wants something like 'PST'" which was slightly inaccurate; Boost wants something like "PST-8". You can use a bogus label; since I don't intend to display the TZ, I just figure out the bias and use "XXT-8".
–
Mike CMay 15 '12 at 17:49

Another thing is, even working around @MikeC 's problem, this still won't work on MSVC. Even using _tzset() works only for UTC offset, but doesn't correctly interpret DST change definitions from the TZ variable (some particular US rules are hardcoded).
–
BartoszKPJan 29 at 12:29

"You could build a posix_time_zone using the non-DST and DST offsets" - yes that's exactly what I would want to do (and what would answer the question). The advantage is that it would hide the ICU dependencies (timezone and unicode related classes), when I work with boost everywhere already.
–
BartoszKPFeb 3 at 17:22

In order to properly answer this question, it's important to understand that the time zone support in Boost is severely limited.

It's primarily focused on POSIX time zones, which have several limitations. These limitations are discussed in the POSIX section of the timezone tag wiki, so I won't repeat them here.

It has functions that work with IDs of IANA/Olson time zones, but it artificially maps these to POSIX values - which has the effect of flattening the time zone to a single point in history. These mappings are stored in a csv file in the Boost source code.

The csv file hasn't been updated since April 2011, and there have been many changes to time zones since then. So, the mappings it does have are somewhat inaccurate.

In general, I would not recommend Boost for working with time zones. Instead, consider the ICU TimeZone Classes, which are part of the ICU project. You will find these to be fully portable, and they have full and correct time zone support.

It's worth mentioning that ICU is used in many popular applications. For example, the Google Chrome web browser gets it's time zone support from ICU.

Thanks. While this input is valuable it unfortunately doesn't answer the question at all :) I know about boost's limitations and I know about ICU (in fact, boost documentation itself recommends using it instead of boost :) ).
–
BartoszKPFeb 1 at 16:08

@BartoszKP could you please elaborate on what kind of answer your looking for then? Thanks.
–
Matt JohnsonFeb 1 at 20:00

What do you mean by "what kind"? Any kind that demonstrates how to have boost's time_zone_ptr correctly set to machine's current timezone in a portable way - that's what the question asks. You can imagine that I'd rather not add another dependency, and the question is about boost, but if the only possibility is to use ICU then so be it. Unfortunately, this part of your answer, the only part that's directly relevant, is link-only.
–
BartoszKPFeb 2 at 0:11