Reputation: 3705
Have a Unix timestamp and I need equivalents of localtime_s, but where I can pass in the timezone. Alternately I'm looking for a way to change the timezone programatically for the app (not the whole system), so that localtime_s returns the correct values.
Strangely enough, if I set TZ environment variable in a shell to GMT, and then launch my application, localtime_s returns values in GMT.
I've tried:
::SetEnvironmentVariable("TZ", "GMT");
tzset();
But it does not change the results of later calls to localtime_s
Note that I'm running the application on Windows 7, using VS 2013.
Upvotes: 0
Views: 459
Reputation: 219325
If you are willing to use a free, open-source 3rd party library, this works on VS-2013. There are many examples on how to use it here:
https://github.com/HowardHinnant/date/wiki/Examples-and-Recipes
For example here is how to output the current time in New York and Chicago:
#include "tz.h"
#include <iostream>
int
main()
{
auto now = std::chrono::system_clock::now();
std::cout << "Current time in New York: "
<< date::make_zoned("America/New_York", now) << '\n';
std::cout << "Current time in Chicago : "
<< date::make_zoned("America/Chicago", now) << '\n';
}
This just output for me:
Current time in New York: 2016-08-12 10:36:24.670966 EDT
Current time in Chicago : 2016-08-12 09:36:24.670966 CDT
Upvotes: 1