Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

Is there any API in C++ which can convert the GMT/UTC offsets to TimeZone names?

Thanks in advance.

e.g. -5 = Eastern Time
Posted
Comments
Bernhard Hiller 13-Feb-12 4:23am    
There can be several names for one offset - while the other way round is unique.

hello

you can get time zone value from the below code


C++
TIME_ZONE_INFORMATION tz = {0};
STimeZoneFromRegistry binary_data;
DWORD size = sizeof(binary_data);
HKEY hk = NULL;
TCHAR zone_key[] = _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\Central Standard Time");
if ((RegOpenKeyEx(HKEY_LOCAL_MACHINE, zone_key, 0, KEY_QUERY_VALUE, &hk) == ERROR_SUCCESS) &&
 (RegQueryValueEx(hk, "TZI", NULL, NULL, (BYTE *) &binary_data, &size) == ERROR_SUCCESS))
{
 tz.Bias = binary_data.Bias;
 tz.DaylightBias = binary_data.DaylightBias;
 tz.DaylightDate = binary_data.DaylightDate;
 tz.StandardBias = binary_data.StandardBias;
 tz.StandardDate = binary_data.StandardDate;
}

struct STimeZoneFromRegistry
{
 long  Bias;
 long  StandardBias;
 long  DaylightBias;
 SYSTEMTIME StandardDate;
 SYSTEMTIME DaylightDate;
};
 
Share this answer
 
Hi Gourav

Thanks.

But my requirement is a little different. Just by having the GMT offset, i need to get the Time zone name. Iterating all keys available in the registry to check the offset might help in mapping the name.

Instead i hardcoded the mapping for all time zones from registry and retrieved what i require at runtime.

Thanks again.
 
Share this answer
 
Comments
Richard MacCutchan 13-Feb-12 11:02am    
But how do you know you got the right one, since they are different depending on which location they apply to? For example, UTC+4 could be Abu Dhabi, Baku, Moscow, Port Moresby etc, all in different countries.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900