DODONG

博客园 首页 新随笔 联系 订阅 管理
View Code
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using System.Runtime.InteropServices;
  6 using Microsoft.Win32;
  7 
  8 namespace WebApplication3
  9 {
 10     public class Class1
 11     {
 12         [StructLayout(LayoutKind.Sequential)]
 13       public  struct SYSTEMTIME
 14         {
 15             public short Year;
 16             public short Month;
 17             public short DayOfWeek;
 18             public short Day;
 19             public short Hour;
 20             public short Minute;
 21             public short Second;
 22             public short Milliseconds;
 23             /// <summary>
 24             /// 
 25             /// </summary>
 26             /// <param name="dt"></param>
 27             /// <returns></returns>
 28             public static implicit operator SYSTEMTIME(DateTime dt)
 29             {
 30                 SYSTEMTIME st = new SYSTEMTIME();
 31 
 32                 st.Year = (short)dt.Year;
 33                 st.Month = (short)dt.Month;
 34                 st.DayOfWeek = (short)dt.DayOfWeek;
 35                 st.Day = (short)dt.Day;
 36                 st.Hour = (short)dt.Hour;
 37                 st.Minute = (short)dt.Minute;
 38                 st.Second = (short)dt.Second;
 39                 st.Milliseconds = (short)dt.Millisecond;
 40                 return st;
 41             }
 42             /// <summary>
 43             /// 
 44             /// </summary>
 45             /// <param name="st"></param>
 46             /// <returns></returns>
 47             public static implicit operator DateTime(SYSTEMTIME st)
 48             {
 49                 return new DateTime(st.Year, st.Month, st.Day, st.Hour, st.Minute, st.Second, st.Milliseconds);
 50             }
 51         }
 52 
 53         [StructLayout(LayoutKind.Sequential)]
 54         public struct TIME_ZONE_INFORMATION
 55         {
 56             public int Bias;
 57             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
 58             public char[] StandardName;
 59             public SYSTEMTIME StandardDate;
 60             public int StandardBias;
 61             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
 62             public char[] DaylightName;
 63             public SYSTEMTIME DaylightDate;
 64             public int DaylightBias;
 65             /// <summary>
 66             /// 
 67             /// </summary>
 68             /// <param name="tzi"></param>
 69             public TIME_ZONE_INFORMATION(TIME_ZONE_INFORMATION tzi)
 70             {
 71                 this = tzi;
 72                 DaylightName = new char[32];
 73                 tzi.DaylightName.CopyTo(DaylightName, 0);
 74                 StandardName = new char[32];
 75                 tzi.StandardName.CopyTo(StandardName, 0);
 76             }
 77         }
 78         /// <summary>
 79         //
 80 
 81         /// </summary>
 82     public   struct REG_TIME_ZONE_INFORMATION
 83         {
 84             public readonly int Bias;
 85             public readonly int DaylightBias;
 86             public readonly SYSTEMTIME DaylightDate;
 87             public readonly int StandardBias;
 88             public readonly SYSTEMTIME StandardDate;
 89 
 90             /// <summary>
 91             /// 
 92             /// </summary>
 93             /// <param name="stream"></param>
 94             public REG_TIME_ZONE_INFORMATION(byte[] stream)
 95             {
 96                 try
 97                 {
 98                     Bias = stream[0+ (stream[1<< 8+ (stream[2<< 16+ (stream[3<< 24);
 99                     StandardBias = stream[4+ (stream[5<< 8+ (stream[6<< 16+ (stream[7<< 24);
100                     DaylightBias = stream[8+ (stream[9<< 8+ (stream[10<< 16+ (stream[11<< 24);
101                     StandardDate.Year = (short)(stream[12+ (stream[13<< 8));
102                     StandardDate.Month = (short)(stream[14+ (stream[15<< 8));
103                     StandardDate.DayOfWeek = (short)(stream[16+ (stream[17<< 8));
104                     StandardDate.Day = (short)(stream[18+ (stream[19<< 8));
105                     StandardDate.Hour = (short)(stream[20+ (stream[21<< 8));
106                     StandardDate.Minute = (short)(stream[22+ (stream[23<< 8));
107                     StandardDate.Second = (short)(stream[24+ (stream[25<< 8));
108                     StandardDate.Milliseconds = (short)(stream[26+ (stream[27<< 8));
109                     DaylightDate.Year = (short)(stream[28+ (stream[29<< 8));
110                     DaylightDate.Month = (short)(stream[30+ (stream[31<< 8));
111                     DaylightDate.DayOfWeek = (short)(stream[32+ (stream[33<< 8));
112                     DaylightDate.Day = (short)(stream[34+ (stream[35<< 8));
113                     DaylightDate.Hour = (short)(stream[36+ (stream[37<< 8));
114                     DaylightDate.Minute = (short)(stream[38+ (stream[39<< 8));
115                     DaylightDate.Second = (short)(stream[40+ (stream[41<< 8));
116                     DaylightDate.Milliseconds = (short)(stream[42+ (stream[43<< 8));
117                 }
118                 catch (Exception e)
119                 {
120                     throw new ArgumentException("REG_TIME_ZONE_INFORMATION   initialization   failed ", e);
121                 }
122             }
123         }
124 
125 
126       public  struct TimeZoneInfo
127         {
128             public readonly string DaylightName;
129             public readonly string Display;
130             public readonly string Name;
131             public readonly string StandardName;
132             TIME_ZONE_INFORMATION m_TZI;
133 
134             public TimeZoneInfo(string name, string display, string daylightName, string standardName, REG_TIME_ZONE_INFORMATION rtzi)
135             {
136                 DaylightName = daylightName;
137                 Display = display;
138                 Name = name;
139                 StandardName = standardName;
140                 m_TZI = new TIME_ZONE_INFORMATION();
141                 m_TZI.Bias = rtzi.Bias;
142                 m_TZI.DaylightBias = rtzi.DaylightBias;
143                 m_TZI.DaylightDate = rtzi.DaylightDate;
144                 m_TZI.DaylightName = new char[32];
145                 DaylightName.ToCharArray().CopyTo(m_TZI.DaylightName, 0);
146                 m_TZI.StandardBias = rtzi.StandardBias;
147                 m_TZI.StandardDate = rtzi.StandardDate;
148                 m_TZI.StandardName = new char[32];
149                 StandardName.ToCharArray().CopyTo(m_TZI.StandardName, 0);
150             }
151 
152             public TIME_ZONE_INFORMATION TZI
153             {
154                 get
155                 {
156                     return new TIME_ZONE_INFORMATION(m_TZI);
157                 }
158             }
159         }
160     }
161 }

----------------------------------------------------------------------------测试--------------------------------------
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title></title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
      
        
<asp:Label ID="Label2"
            runat
="server" Text="Label"  ForeColor=Red></asp:Label>

            
<BR>
            
<BR>
              
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        
<br />
    
</div>
    
</form>
</body>
</html>

View Code
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using System.Web.UI;
  6 using System.Web.UI.WebControls;
  7 using System.Runtime.InteropServices;
  8 using Microsoft.Win32;
  9 using System.Collections;
 10 
 11 namespace WebApplication3
 12 {
 13     public partial class _Default : System.Web.UI.Page
 14     {
 15 
 16         const string c_WinNTKey = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones";
 17         const string c_Win9xKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Time Zones";
 18         const string c_KeyDisplay = "Display ";
 19         const string c_KeyDayLightName = "Dlt ";
 20         const string c_KeyStandardName = "Std ";
 21         const string c_KeyTZI = "TZI ";
 22         static ArrayList s_TimeZones = new ArrayList();
 23         ///   <summary> 
 24         ///   The   main   entry   point   for   the   application. 
 25         ///   </summary> 
 26         [DllImport("Kernel32.dll ")]
 27         static extern bool TzSpecificLocalTimeToSystemTime(ref   Class1.TIME_ZONE_INFORMATION lpTimeZoneInformation, ref   Class1.SYSTEMTIME lpLocalTime, ref    Class1.SYSTEMTIME lpUniversalTime);
 28 
 29         [DllImport("Kernel32.dll ")]
 30         static extern bool SystemTimeToTzSpecificLocalTime(ref   Class1.TIME_ZONE_INFORMATION lpTimeZone, ref   Class1.SYSTEMTIME lpUniversalTime, ref    Class1.SYSTEMTIME lpLocalTime);
 31 
 32 
 33         protected void Page_Load(object sender, EventArgs e)
 34         {
 35 
 36             switch (Environment.OSVersion.Platform)
 37             {
 38                 case PlatformID.Win32NT:
 39                     RetrieveWinNTTimeZones();
 40                     break;
 41                 case PlatformID.Win32Windows:
 42                     break;
 43                 default:
 44                     break;
 45             }
 46             DateTime dtUTC = DateTime.Now.ToUniversalTime();
 47             Class1.SYSTEMTIME stUTC = dtUTC;
 48             Class1.SYSTEMTIME st = new Class1.SYSTEMTIME();
 49             Class1.TIME_ZONE_INFORMATION tzi;
 50 
 51             Label2.Text = string.Format("{0,-50}{1} ""UTC ", dtUTC.ToString());
 52 
 53             foreach (Class1.TimeZoneInfo dzi in s_TimeZones)
 54             {
 55                 tzi = dzi.TZI;
 56                 SystemTimeToTzSpecificLocalTime(ref   tzi, ref   stUTC, ref   st);
 57  
 58                 Label1.Text += string.Format("{0,   -50}{1} ", dzi.Name, ((DateTime)st).ToString())+"<BR>";
 59             }
 60 
 61 
 62         }
 63 
 64 
 65         static void RetrieveWinNTTimeZones()
 66         {
 67             using (RegistryKey baseKey = Registry.LocalMachine.OpenSubKey(c_WinNTKey))
 68             {
 69                 if (baseKey == null)
 70                 {
 71                     throw new InvalidOperationException(string.Format("Registry   key  {0} open   failed ", c_WinNTKey));
 72                 }
 73                 string[] tzNames = baseKey.GetSubKeyNames();
 74                 string tzDisplay;
 75                 string tzDaylightName;
 76                 string tzStandardName;
 77                 byte[] tzTZI;
 78 
 79                 foreach (string tzName in tzNames)
 80                 {
 81                     using (RegistryKey tzKey = baseKey.OpenSubKey(tzName))
 82                     {
 83                         tzDisplay = tzKey.GetValue("Display"string.Empty) as string;
 84                         tzDaylightName = tzKey.GetValue("Dlt"string.Empty) as string;
 85                         tzStandardName = tzKey.GetValue("Std"string.Empty) as string;
 86                         tzTZI = tzKey.GetValue("TZI"string.Empty) as byte[];
 87                         s_TimeZones.Add(
 88                                 new Class1.TimeZoneInfo
 89                                 (
 90                                 tzName, tzDisplay,
 91                                 tzDaylightName,
 92                                 tzStandardName,
 93                                 new Class1.REG_TIME_ZONE_INFORMATION
 94                                     (tzTZI)
 95                                     )
 96                                 );
 97                     }
 98                 }
 99             }
100         }
101         //-----------------
102     }
103 }
posted on 2011-04-26 18:02  DODONG  阅读(1399)  评论(0编辑  收藏  举报