C++ time_t to std::string for a specific time zone
In a Linux environment working in C++, I need to convert a time_t value into an English string representation for various time zones. e.g. 1305750080 -> "2011-05-18 13:21:20 PST". I am able to use gmtime() or localtime() combined w开发者_如何学运维ith strftime() to generate strings for GMT and my local timezone. How would I select an alternative time zone?
gmtime()
reference link here has an example showing a very simple way of applying time zone offsets. Naturally you could create a class with an enum or something and come up with something a little nicer.
Alternately boost::date_time
has a pretty extensive implementation that works on linux and windows.
I think you can do this with tzset.
setenv("TZ", "EST5EDT", 1);
tzset();
You can use putenv
to change the TZ
environment variable to whatever timezone you want and then use localtime
or preferably localtime_r
. You can use getenv
to cache off the old copy if needed.
精彩评论