Get international Standard Time from the network

Source: Internet
Author: User
When public static DateTime datastandardtime ()//is used, the static keyword is deleted and can be used in other locations? 2010-11-24
{//Return to international Standard Time
Use only the IP address of the time server, not using the domain name
string[,] Time server =new string [14,2];
Int[] Search Order =new int []{3,2,4,8,9,6,11,5,10,0,1,7,12};
Time server [0,0]= "time-a.nist.gov";
Time server [0,1]= "129.6.15.28";
Time server [1,0]= "time-b.nist.gov";
Time server [1,1]= "129.6.15.29";
Time server [2,0]= "time-a.timefreq.bldrdoc.gov";
Time server [2,1]= "132.163.4.101";
Time server [3,0]= "time-b.timefreq.bldrdoc.gov";
Time server [3,1]= "132.163.4.102";
Time server [4,0]= "time-c.timefreq.bldrdoc.gov";
Time server [4,1]= "132.163.4.103";
Time server [5,0]= "utcnist.colorado.edu";
Time server [5,1]= "128.138.140.44";
Time server [6,0]= "time.nist.gov";
Time server [6,1]= "192.43.244.18";
Time server [7,0]= "time-nw.nist.gov";
Time server [7,1]= "131.107.1.10";
Time server [8,0]= "nist1.symmetricom.com";
Time server [8,1]= "69.25.96.13";
Time server [9,0]= "nist1-dc.glassey.com";
Time server [9,1]= "216.200.93.8";
Time server [10,0]= "nist1-ny.glassey.com";
Time server [10,1]= "208.184.49.9";
Time server [11,0]= "nist1-sj.glassey.com";
Time server [11,1]= "207.126.98.204";
Time server [12,0]= "nist1.aol-ca.truetime.com";
Time server [12,1]= "207.200.81.113";
Time server [13,0]= "nist1.aol-va.truetime.com";
Time server [13,1]= "64.236.96.53";
int portnum=13;
String HostName;
byte[] Bytes=new byte [1024];
int bytesread=0;
System.Net.Sockets. TcpClient client=new System.Net.Sockets.TcpClient ();
for (int i=0;i<13;i++)
{
Hostname= time Server [search order [i],1];
Try
{
Client. Connect (Hostname,portnum);
System.Net. Sockets. NetworkStream ns=client. GetStream ();
Bytesread=ns. Read (bytes,0,bytes. Length);
Client. Close ();
Break
}
catch (System.Exception)
{
}
}
char[] Sp=new char [1];
Sp[0]= ';
System.DateTime dt=new DateTime ();
String str1;
Str1=system.text. Encoding. Ascii. GetString (Bytes,0,bytesread);

String[] s;
S=str1. Split (SP);
Dt=system.datetime. Parse (s[1]+ "" +s[2])/Get Standard Time
Dt=dt. AddHours (8)//Get Beijing time * *
return DT;

}

Get time from 14 servers in turn, as long as there is a response. The search order is set according to how quickly we get here, and may vary slightly.



Reproduced from: http://www.360doc.com/content/10/1124/12/2482712_71976347.shtml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.