C#获取北京时间与设置系统时间


获取北京时间

 1 public static DateTime GetBeijingTime()
 2 {
 3     DateTime dt;
 4 
 5     // 返回国际标准时间
 6     // 只使用 timeServers 的 IP 地址,未使用域名
 7     try
 8     {
 9         string[,] timeServers = new string[14, 2];
10         int[] searchOrder = { 3, 2, 4, 8, 9, 6, 11, 5, 10, 0, 1, 7, 12 };
11         timeServers[0, 0] = "time-a.nist.gov";
12         timeServers[0, 1] = "129.6.15.28";
13         timeServers[1, 0] = "time-b.nist.gov";
14         timeServers[1, 1] = "129.6.15.29";
15         timeServers[2, 0] = "time-a.timefreq.bldrdoc.gov";
16         timeServers[2, 1] = "132.163.4.101";
17         timeServers[3, 0] = "time-b.timefreq.bldrdoc.gov";
18         timeServers[3, 1] = "132.163.4.102";
19         timeServers[4, 0] = "time-c.timefreq.bldrdoc.gov";
20         timeServers[4, 1] = "132.163.4.103";
21         timeServers[5, 0] = "utcnist.colorado.edu";
22         timeServers[5, 1] = "128.138.140.44";
23         timeServers[6, 0] = "time.nist.gov";
24         timeServers[6, 1] = "192.43.244.18";
25         timeServers[7, 0] = "time-nw.nist.gov";
26         timeServers[7, 1] = "131.107.1.10";
27         timeServers[8, 0] = "nist1.symmetricom.com";
28         timeServers[8, 1] = "69.25.96.13";
29         timeServers[9, 0] = "nist1-dc.glassey.com";
30         timeServers[9, 1] = "216.200.93.8";
31         timeServers[10, 0] = "nist1-ny.glassey.com";
32         timeServers[10, 1] = "208.184.49.9";
33         timeServers[11, 0] = "nist1-sj.glassey.com";
34         timeServers[11, 1] = "207.126.98.204";
35         timeServers[12, 0] = "nist1.aol-ca.truetime.com";
36         timeServers[12, 1] = "207.200.81.113";
37         timeServers[13, 0] = "nist1.aol-va.truetime.com";
38         timeServers[13, 1] = "64.236.96.53";
39         int portNum = 13;
40         byte[] bytes = new byte[1024];
41         int bytesRead = 0;
42         System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
43         for (int i = 0; i < 13; i++)
44         {
45             string hostName = timeServers[searchOrder[i], 1];
46             try
47             {
48                 client.Connect(hostName, portNum);
49                 System.Net.Sockets.NetworkStream ns = client.GetStream();
50                 bytesRead = ns.Read(bytes, 0, bytes.Length);
51                 client.Close();
52                 break;
53             }
54             catch (Exception)
55             {
56                 // ignored
57             }
58         }
59         char[] sp = new char[1];
60         sp[0] = ' ';
61         dt = new DateTime();
62         string str1 = System.Text.Encoding.ASCII.GetString(bytes, 0, bytesRead);
63 
64         string[] s = str1.Split(sp);
65         if (s.Length >= 2)
66         {
67             dt = DateTime.Parse(s[1] + " " + s[2]); // 得到标准时间
68             dt = dt.AddHours(8);                    // 得到北京时间
69         }
70         else
71         {
72             dt = DateTime.Parse("2016-1-1");
73         }
74     }
75     catch (Exception)
76     {
77         dt = DateTime.Parse("2016-1-1");
78     }
79     return dt;
80 }            

设置本地系统时间

 1 [DllImport("kernel32.dll")]
 2 private static extern bool SetLocalTime(ref Systemtime time);
 3 
 4 [StructLayout(LayoutKind.Sequential)]
 5 private struct Systemtime
 6 {
 7     public short year;
 8     public short month;
 9     public short dayOfWeek;
10     public short day;
11     public short hour;
12     public short minute;
13     public short second;
14     public short milliseconds;
15 }
16 
17 public static bool SetDate(DateTime dt)
18 {
19     Systemtime st;
20 
21     st.year = (short)dt.Year;
22     st.month = (short)dt.Month;
23     st.dayOfWeek = (short)dt.DayOfWeek;
24     st.day = (short)dt.Day;
25     st.hour = (short)dt.Hour;
26     st.minute = (short)dt.Minute;
27     st.second = (short)dt.Second;
28     st.milliseconds = (short)dt.Millisecond;
29 
30     bool rt = SetLocalTime(ref st);
31     return rt;
32 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM