




























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 }
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。