• 使用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
    6                            foreach ( string zoneName in zoneNames )
    7                            {
    8                                using ( RegistryKey subKey = key.OpenSubKey( zoneName ) )

    等等,这里的TZI是二进制的,这种又代表什么呢?仍然是上面的pinvoke的链接,可以将这些二进制数据转换为里面所定义的结构体。那么可以通过如下方法枚举出Time Zones的所有子键信息:

     1public 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

  • 相关阅读:
    linux内核同步机制相关收集
    【转载】关于终端和控制台的一些解释
    【转】linux内核调试方法总结
    misc汇总
    关于proc的介绍,比较详细
    linux内核启动流程分析
    linux通用中断子系统介绍
    ftrace在mips上的验证
    线程死锁demo
    BeanFactory与FactoryBean
  • 原文地址:https://www.cnblogs.com/gamix/p/303559.html
Copyright © 2020-2023  润新知