你如何在Windows中任意时区信息? [英] How do you get info for an arbitrary time zone in Windows?

查看:249
本文介绍了你如何在Windows中任意时区信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在理想情况下,我希望能够做的就是一个时区的名称,并询问Windows中其相应的时区信息(UTC的偏移量,DST偏移量,日期DST开关等)。它看起来像Windows使用 TIME_ZONE_INFORMATION 结构持有这种的信息。所以,presumably,我希望有一个函数,它接受的时区名称的字符串,并返回一个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.

不过,所有我能找到的功能,如<一个href=\"http://msdn.microsoft.com/en-us/library/ms724421%28v=VS.85%29.aspx\">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 页面显示了它在注册表中,因此它应该有可能从那里获取的信息,但我更preFER一个正确的系统功能做了。请问这样的功能存在,还是我去潜水注册表来获取任意时间区的时区信息?

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_MACHINE \\ SOFTWARE \\微软\\的Windows NT \\ CURRENTVERSION \\ Time区域包含在注册表中的二进制数据\\ (区域名称)\\ TZI 。的数据的结构的TIME_ZONE_INFORMATION文档中给出​​:

The time zone information is contained as binary data in the registry under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time 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;
};

和这里的例子code阅读的关键是:

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.

这篇关于你如何在Windows中任意时区信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆