无论我的系统时间如何,如何获得通用时间。 [英] How to get Universal Time irrespective of my system time.

查看:70
本文介绍了无论我的系统时间如何,如何获得通用时间。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何在不使用系统时间的情况下获得通用时间。



例如,如果我的系统时间设置为某个错误的时间,那么在代码DateTime.UTCNow中也会给我错误的UTC时间。



因此,即使我的系统时间设置错误,我也需要获得正确的UTC时间。请帮助





感谢你...



MSK

Hi,

How can i get the Universal Time without using my System Time.

For example, If my system time is set to some wrong time, then in the code DateTime.UTCNow is giving me wrong UTC time as well.

Hence I need to get the correct UTC time even my system time is set wrong. Pls help


Thanking you...

MSK

推荐答案

这样做的唯一方法是使用互联网时间服务来返回实际时间 - 任何其他方法都将失败,因为必须使用PC时间。



有几种方法可以做到这一点,但它们都涉及与NIST交谈: http://stackoverflow.com/questions/6435099/c-how-to-get-datetime-from-the-internet [<一个href =http://stackoverflow.com/questions/6435099/c-how-to-get-datetime-from-the-internettarget =_ blanktitle =New Window> ^ ]
The only way to do that is to use an internet time service to return the actual time - any other approach will fail because has to use the PC time.

There are a couple of approaches to do this, but they all involve talking to a NIST: http://stackoverflow.com/questions/6435099/c-how-to-get-datetime-from-the-internet[^]


string host = Dns.GetHostName();
var client = new TcpClient(host, 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);
    TextBox1.Text = localDateTime.ToString();
}


如果您连接到互联网,OG的答案是正确的。



如果没有,那么如果你有一个gps还有另外一种方法。 Gps设备接收包含非常准确的UTC时间的数据,因为卫星时钟与非常准确的原子钟同步。如果您将gps设置为NMEA数据,例如您可以从数据中解析时间值。



此方法可用于远程位置以准确设置PC时钟。请参阅此处:

自己编写GPS应用:第一部分 [ ^ ]
OGs answer is the correct one if you are connected to the internet.

If not then there is another way if you have a gps. Gps devices receive data containing very accurate UTC time because the satellite clocks are synchronised to very accurate atomic clocks.. If you set gps to NMEA data for example you can parse the time value from the data.

This method can be used in remote locations to set the PC clock accurately. See here for example:
Writing Your Own GPS Applications: Part I[^]


这篇关于无论我的系统时间如何,如何获得通用时间。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆