[转]使用C#获取当前Windows所设定的时区
2ndgatechina的笔试project:写一个.NET API,能使其他devs能够调用他来将任意时区的时间转换为当前时间或者将当前时间转换为任意时区的时间。
咋一看,好像比较简单,真做的时候才发现我怎么去获取我系统的当前时区呢?上http://www.pinvoke.net上查,发现需要使用kernel32.dll中的GetTimeZoneInformation的系统API。
现成的例子参看这里,我就不详细说了:http://www.pinvoke.net/default.aspx/kernel32.GetTimeZoneInformation
在这个基础上我们来扩展一下:如何去取得windows所给我们设定的全部时区呢?
其实windows所设定的时区全部保存在 \KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones的目录里,那么很自然我们有代码:
1
RegistryKey key = Registry.LocalMachine.OpenSubKey(
2
@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones" ) )
3
{
4
string[] zoneNames = key.GetSubKeyNames();
5![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
foreach ( string zoneName in zoneNames )
7
{
8
using ( RegistryKey subKey = key.OpenSubKey( zoneName ) )
![](https://gamix.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://gamix.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://gamix.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://gamix.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
等等,这里的TZI是二进制的,这种又代表什么呢?仍然是上面的pinvoke的链接,可以将这些二进制数据转换为里面所定义的结构体。那么可以通过如下方法枚举出Time Zones的所有子键信息:
1
public static TimeZoneInformation[] EnumZones()
2
{
3
if ( s_zones == null )
4
{
5
lock( s_lockZones )
6
{
7
if ( s_zones == null )
8
{
9
ArrayList zones = new ArrayList();
10![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
using ( RegistryKey key =
12
Registry.LocalMachine.OpenSubKey(
13
@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones" ) )
14
{
15
string[] zoneNames = key.GetSubKeyNames();
16![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
foreach ( string zoneName in zoneNames )
18
{
19
using ( RegistryKey subKey = key.OpenSubKey( zoneName ) )
20
{
21
TimeZoneInformation tzi = new TimeZoneInformation();
22
tzi.m_name = zoneName;
23
tzi.m_displayName = (string) subKey.GetValue( "Display" );
24
tzi.m_standardName = (string) subKey.GetValue( "Std" );
25
tzi.m_daylightName = (string) subKey.GetValue( "Dlt" );
26
tzi.m_index = (int)( subKey.GetValue( "Index" ) );
27![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
tzi.InitTzi( (byte[]) subKey.GetValue( "Tzi" ) );
29
30
zones.Add( tzi );
31
}
32
}
33
}
34![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
s_zones = new TimeZoneInformation[ zones.Count ];
36![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
zones.CopyTo( s_zones );
38
}
39
}
40
}
41![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
return s_zones;
43
}
![](https://gamix.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://gamix.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://gamix.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
5
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://gamix.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://gamix.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://gamix.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://gamix.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://gamix.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://gamix.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
32
![](https://gamix.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](https://gamix.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://gamix.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
39
![](https://gamix.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
40
![](https://gamix.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
41
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://gamix.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://gamix.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
这样我们便获取到了windows所给我们定义好的时区信息,其中TimeZoneInformation.bias代表我做API中所需要的时间偏移量(单位:分钟),至此,我们就可以做出windows所提供的时区之间的任意转换了。
给一个很好的与此相关的代码资料:http://www.codeproject.com/dotnet/WorldClock.asp
参考资料:
http://www.pinvoke.net
http://www.codeproject.com/dotnet/WorldClock.asp
文章转自:http://gamix.cnblogs.com/archive/2005/12/23/303559.html