如何在 Windows 中获取任意时区的信息?

2022-01-16 00:00:00 windows c timezone c++

理想情况下,我想做的是获取时区的名称并询问 Windows 其相应的时区信息(与 UTC 的偏移量、DST 偏移量、DST 切换的日期等).看起来 Windows 使用 TIME_ZONE_INFORMATION 结构持有这类信息.所以,大概,我想要一个函数,它接受一个带有时区名称的字符串并返回一个 TIME_ZONE_INFORMATION 结构.

Ideally, what I'd like to be able to do is take the name of a time zone and ask Windows for its corresponding time zone info (offset from UTC, DST offset, dates for DST switch, etc.). It looks like Windows uses a TIME_ZONE_INFORMATION struct to hold this sort of info. So, presumably, I want a function which takes a string with the time zone's name and returns a TIME_ZONE_INFORMATION struct.

但是,我只能找到诸如 GetTimeZoneInformation() 这给了我当地时间的 TIME_ZONE_INFORMATION.我需要的是一个函数,它可以为我提供任意时区的信息,而不管本地时区是什么.

However, all I can find are functions such as GetTimeZoneInformation() which give me the TIME_ZONE_INFORMATION for the local time. What I need is a function which will give me that information for an arbitrary time zone regardless of what the local time zone is.

我看到获取该信息的唯一方法是直接从注册表中获取它,这不太理想.TIME_ZONE_INFORMATION 页面显示了它的位置注册表,所以应该可以从那里获取信息,但我更喜欢适当的系统功能来做这件事.是否存在这样的功能,还是我必须去注册表潜水以获得任意时区的时区信息?

The only way that I see to get that information is to go grab it directly from the registry, which is less than ideal. The TIME_ZONE_INFORMATION page shows where it is in the registry, so it should be possible to fetch the information from there, but I'd much prefer a proper system function for doing it. Does such a function exist, or do I have to go registry diving to get the time zone info for an arbitrary time zone?

推荐答案

时区信息以二进制数据的形式包含在注册表中HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionTime Zones(时区名称)TZI.TIME_ZONE_INFORMATION 文档中给出了数据的结构:

The time zone information is contained as binary data in the registry under HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionTime Zones(zone name)TZI. The structure of the data is given in the TIME_ZONE_INFORMATION documentation:

struct STimeZoneFromRegistry
{
 long  Bias;
 long  StandardBias;
 long  DaylightBias;
 SYSTEMTIME StandardDate;
 SYSTEMTIME DaylightDate;
};

下面是读取密钥的示例代码:

And here's example code to read the key:

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;
}

对不起,这个答案是多余的 - 我相信你可以使用你在问题中链接到的文档来解决所有这些问题.我只需要这样做一次,这是我能找到的唯一方法.

Sorry, this answer is redundant - I'm sure you could have figured all this out using the documentation you linked to in the question. I've only had to do this once, and this is the only method I could find.

相关文章