C # obtain Internet time

Source: Internet
Author: User
Tags rfc

There is a free us standard time on the Internet, that is, NIST time. In Windows, apart from the Internet time provided by Microsoft, it also supports multiple internet times provided by NIST, the authority of this time can be seen.

The method for obtaining NIST is very simple. The following is a closed class for obtaining NIST time:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. net;
Using system. net. Sockets;
Using system. text;

Namespace felomeng
{
Public class nisttime
{
Private Static char [] separatorarray = new char [] {''};
Public datetime getnisttime ()
{
// String whost = "time.nist.gov"; // The following IP address does not respond as well.
// Iphostentry iphostinfo = DNS. gethostentry (whost );
VaR IPE = new ipendpoint (IPaddress. parse ("132.163.4.101"), 13 );
VaR c = new socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP)
{Receivetimeout = 10*1000}; // create a socket
C. Connect (IPE );
VaR recvbuffer = new byte [1024];
Int nbytes;
VaR sb = new stringbuilder ();
While (nbytes = C. Receive (recvbuffer, 0, 1024, socketflags. None)> 0)
{
SB. append (encoding. utf8.getstring (recvbuffer, 0, nbytes ));
}
VaR dt = parsedatetimefromstring (sb. tostring ());
C. Close ();
Return DT;
}
Private Static datetime parsedatetimefromstring (string daytimestring)
{
Daytimestring = daytimestring. Replace ("\ n", String. Empty)
. Replace ("\ 0", String. Empty );
// 54708 08-08-30 18:53:02 50 0 0 770.8 UTC (NIST )*
// 0 1 2 3 4 5 6 7 8
// Http://tf.nist.gov/service/its.htm
// See "daytime protocol (RFC-867 )"

String [] resulttokens = daytimestring. Split (separatorarray, stringsplitoptions. removeemptyentries );
If (resulttokens [7]! = "UTC (NIST)" | resulttokens [8]! = "*")
{
Throw new exception (string. Format ("invalid RFC-867 daytime protocol string: '{0}'", daytimestring ));
}

VaR MJD = int. parse (resulttokens [0]); // "jjjj is the Modified Julian Date (MJD). The MJD has a starting point of midnight on November 17,185 8 ."
VaR now = new datetime (1858, 11, 17 );
Now = now. adddays (MJD );

VaR timetokens = resulttokens [2]. Split (':');
VaR hours = int. parse (timetokens [0]);
VaR minutes = int. parse (timetokens [1]);
VaR seconds = int. parse (timetokens [2]);
VaR millis = double. parse (resulttokens [6], new system. Globalization. cultureinfo ("En-us"); // This is speculative: official documentation seems out of date!

Now = now. addhours (hours );
Now = now. addminutes (minutes );
Now = now. addseconds (seconds );
Now = now. addmilliseconds (-millis );

Return now;
}
}
}

With this class, it is easy to get the Internet time. The following is a button event to get the Internet instance, and finally the Internet time value is assigned to the text attribute of the button, that is, the obtained time is displayed on the button:

Private void button2_click (Object sender, eventargs E)
{
Felomeng. nisttime clock = new felomeng. nisttime ();
This. button2.text = clock. getnisttime (). tolocaltime (). tostring ();
}

 

 

Related Article

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.