C# Asp.Net 获取标准北京时间

       #region 获取北京时间
        /// <summary>
        /// 获取北京时间
        /// </summary>
        /// <returns></returns>
        public DateTime GetBeiJinTime()
        {
            string[,] arrList = new string[14, 2];
            arrList[0, 0] = "time-a.nist.gov";
            arrList[0, 1] = "129.6.15.28";
            arrList[1, 0] = "time-b.nist.gov";
            arrList[1, 1] = "129.6.15.29";
            arrList[2, 0] = "time-a.timefreq.bldrdoc.gov";
            arrList[2, 1] = "132.163.4.101";
            arrList[3, 0] = "time-b.timefreq.bldrdoc.gov";
            arrList[3, 1] = "132.163.4.102";
            arrList[4, 0] = "time-c.timefreq.bldrdoc.gov";
            arrList[4, 1] = "132.163.4.103";
            arrList[5, 0] = "utcnist.colorado.edu";
            arrList[5, 1] = "128.138.140.44";
            arrList[6, 0] = "time.nist.gov";
            arrList[6, 1] = "192.43.244.18";
            arrList[7, 0] = "time-nw.nist.gov";
            arrList[7, 1] = "131.107.1.10";
            arrList[8, 0] = "nist1.symmetricom.com";
            arrList[8, 1] = "69.25.96.13";
            arrList[9, 0] = "nist1-dc.glassey.com";
            arrList[9, 1] = "216.200.93.8";
            arrList[10, 0] = "nist1-ny.glassey.com";
            arrList[10, 1] = "208.184.49.9";
            arrList[11, 0] = "nist1-sj.glassey.com";
            arrList[11, 1] = "207.126.98.204";
            arrList[12, 0] = "nist1.aol-ca.truetime.com";
            arrList[12, 1] = "207.200.81.113";
            arrList[13, 0] = "nist1.aol-va.truetime.com";
            arrList[13, 1] = "64.236.96.53";

            int port = 13;
            byte[] array3 = new byte[1024];
            int len = 0;
            int[] array2 = new int[] { 3, 2, 4, 8, 9, 6, 11, 5, 10, 0, 1, 7, 12 };
            TcpClient tcpClient = new TcpClient();
            for (int i = 0; i < 13; i++)
            {
                string hostname = arrList[array2[i], 0];
                try
                {
                    tcpClient.Connect(hostname, port);
                    NetworkStream stream = tcpClient.GetStream();
                    len = stream.Read(array3, 0, array3.Length);
                    tcpClient.Close();
                    break;
                }
                catch { }
            }

            DateTime result = default(DateTime);
            if (len != 0)
            {
                string data = Encoding.ASCII.GetString(array3, 0, len);
                string[] arr = data.Split(new char[] { ' ' });
                result = DateTime.Parse(arr[1] + " " + arr[2]);
            }
            return result;
        }
        #endregion

  

posted @ 2021-04-30 11:10  懒人境界  阅读(218)  评论(0编辑  收藏  举报