从互联网上获取时间 [英] Get Time from internet
本文介绍了从互联网上获取时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有人有代码可以从互联网上获取时间?
解决方案
public static DateTime GetFastestNISTDate(){var result = DateTime.MinValue;
//初始化NIST时间服务器列表
// http://tf.nist.gov/tf-cgi/servers.cgi
string [] servers = new string [] {
nist1-ny.ustiming.org,
nist1-nj.ustiming.org,
nist1-pa.ustiming.org,
time-a .nist.gov,
time-b.nist.gov,
nist1.aol-va.symmetricom.com,
nist1.columbiacountyga.gov,
nist1-chi.ustiming.org,
nist.expertsmi.com,
nist.netservicesgroup.com
};
//以随机顺序尝试5台服务器以分散负载
Random rnd = new Random();
foreach(服务器中的字符串服务器.OrderBy(s => rnd.NextDouble())。Take(5))
{
try
{
//连接到服务器(在端口13)并获得响应
string serverResponse = string.Empty;
using(var reader = new StreamReader(new System.Net.Sockets.TcpClient(server,13).GetStream()))
{
serverResponse = reader.ReadToEnd();
}
//如果收到回复
if(!string.IsNullOrEmpty(serverResponse))
{
//拆分响应字符串( 55596 11-02-14 13:54:11 00 0 0 478.1 UTC(NIST)*)
string [] tokens = serverResponse.Split('');
//检查令牌数量
if(tokens.Length> = 6)
{
//检查健康状况
字符串健康状况=令牌[5];
if(health ==0)
{
//从服务器响应中获取日期和时间部分
string [] dateParts = tokens [1] .Split(' - ');
string [] timeParts = tokens [2] .Split(':');
//创建一个DateTime实例
DateTime utcDateTime = new DateTime(
Convert.ToInt32(dateParts [0])+ 2000,
Convert.ToInt32(dateParts [ 1]),Convert.ToInt32(dateParts [2]),
Convert.ToInt32(timeParts [0]),Convert.ToInt32(timeParts [1]),
Convert.ToInt32(timeParts [2] ));
//将收到的(UTC)DateTime值转换为本地时区
result = utcDateTime.ToLocalTime();
返回结果;
//成功收到回复;退出循环
}
}
}
}
捕获
{
/ /忽略异常并尝试下一个服务器
}
}
返回结果;
}
你可以查看这个,对你有帮助$
http://www.codeproject.com/Answers/140474/how-to-get-internet-time-from-vb-net-application.aspx
http://www.google.com/codesearch/
试试这个
var client = new TcpClient(64.90.182.55,13);
using(var streamReader = new StreamReader(client.GetStream()))
{
var response = streamReader.ReadToEnd();
var utcDateTimeString = response.Substring(7,17);
var localDateTime = DateTime.ParseExact(utcDateTimeString,yy-MM-dd hh:mm:ss,CultureInfo.InvariantCulture,DateTimeStyles.AssumeUniversal);
}
请通过此链接 http://stackoverflow.com/questions/6435099/get-datetime-from-internet-in-c-sharp
Does anyone have code to get time from internet?
解决方案
public static DateTime GetFastestNISTDate() { var result = DateTime.MinValue; // Initialize the list of NIST time servers // http://tf.nist.gov/tf-cgi/servers.cgi string[] servers = new string[] { "nist1-ny.ustiming.org", "nist1-nj.ustiming.org", "nist1-pa.ustiming.org", "time-a.nist.gov", "time-b.nist.gov", "nist1.aol-va.symmetricom.com", "nist1.columbiacountyga.gov", "nist1-chi.ustiming.org", "nist.expertsmi.com", "nist.netservicesgroup.com" }; // Try 5 servers in random order to spread the load Random rnd = new Random(); foreach (string server in servers.OrderBy(s => rnd.NextDouble()).Take(5)) { try { // Connect to the server (at port 13) and get the response string serverResponse = string.Empty; using (var reader = new StreamReader(new System.Net.Sockets.TcpClient(server, 13).GetStream())) { serverResponse = reader.ReadToEnd(); } // If a response was received if (!string.IsNullOrEmpty(serverResponse)) { // Split the response string ("55596 11-02-14 13:54:11 00 0 0 478.1 UTC(NIST) *") string[] tokens = serverResponse.Split(' '); // Check the number of tokens if (tokens.Length >= 6) { // Check the health status string health = tokens[5]; if (health == "0") { // Get date and time parts from the server response string[] dateParts = tokens[1].Split('-'); string[] timeParts = tokens[2].Split(':'); // Create a DateTime instance DateTime utcDateTime = new DateTime( Convert.ToInt32(dateParts[0]) + 2000, Convert.ToInt32(dateParts[1]), Convert.ToInt32(dateParts[2]), Convert.ToInt32(timeParts[0]), Convert.ToInt32(timeParts[1]), Convert.ToInt32(timeParts[2])); // Convert received (UTC) DateTime value to the local timezone result = utcDateTime.ToLocalTime(); return result; // Response successfully received; exit the loop } } } } catch { // Ignore exception and try the next server } } return result; }
You can check out this, would be helpful for u
http://www.codeproject.com/Answers/140474/how-to-get-internet-time-from-vb-net-application.aspx
http://www.google.com/codesearch/
Try this
var client = new TcpClient("64.90.182.55", 13); using (var streamReader = new StreamReader(client.GetStream())) { var response = streamReader.ReadToEnd(); var utcDateTimeString = response.Substring(7, 17); var localDateTime = DateTime.ParseExact(utcDateTimeString, "yy-MM-dd hh:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); }
pls go thru this linkhttp://stackoverflow.com/questions/6435099/get-datetime-from-internet-in-c-sharp
这篇关于从互联网上获取时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文