[转]使用C#获取当前Windows所设定的时区
2005-12-25 00:43
387 查看
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" ) )
3public 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
11 using ( RegistryKey key =
12 Registry.LocalMachine.OpenSubKey(
13 @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones" ) )
14 {
15 string[] zoneNames = key.GetSubKeyNames();
16
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
28 tzi.InitTzi( (byte[]) subKey.GetValue( "Tzi" ) );
29
30 zones.Add( tzi );
31 }
32 }
33 }
34
35 s_zones = new TimeZoneInformation[ zones.Count ];
36
37 zones.CopyTo( s_zones );
38 }
39 }
40 }
41
42 return s_zones;
43 }
这样我们便获取到了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
咋一看,好像比较简单,真做的时候才发现我怎么去获取我系统的当前时区呢?上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" ) )
3public 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
11 using ( RegistryKey key =
12 Registry.LocalMachine.OpenSubKey(
13 @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones" ) )
14 {
15 string[] zoneNames = key.GetSubKeyNames();
16
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
28 tzi.InitTzi( (byte[]) subKey.GetValue( "Tzi" ) );
29
30 zones.Add( tzi );
31 }
32 }
33 }
34
35 s_zones = new TimeZoneInformation[ zones.Count ];
36
37 zones.CopyTo( s_zones );
38 }
39 }
40 }
41
42 return s_zones;
43 }
这样我们便获取到了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
相关文章推荐
- 使用C#获取当前Windows所设定的时区
- 使用C#获取当前Windows所设定的时区
- windows下使用C#获取特定进程网络流量
- C#判断当前系统当前时区是否使用夏令时(夏时制)
- windows下使用C#获取特定进程网络流量
- 使用C#获取Windows版本(源码)
- C#使用StackTrace、StackFrame、MethodBase获取当前属性时遇到的麻烦
- 使用 C# 获取当前用户的SID
- windows下使用C#获取特定进程网络流量
- windows下使用C#获取特定进程网络流量
- C# 获取当前正在使用的网卡地址
- Windows获取系统当前时区的两种方法
- C# 系统应用之获取Windows最近使用记录
- C#获取时间戳的封装方法函数+使用获取当前时间时间戳
- [笔记]使用API函数 GetACP 获取Windows系统当前代码页
- C#获取当前时区
- ASP.NET中使用Razor语法(C#)怎样获取当前浏览器的cookie
- c# 根据当前时间获取,本周,本月,本季度,月初,月末,各个时间段(转但是都是使用过)
- C# 使用定时任务 之 获取当前时间及格式化串
- C# 系统应用之获取Windows最近使用记录