无论当前系统时间如何,我如何获得UTC。 [英] How can I get UTC ,regardless of the current system Time.

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

问题描述

您好,

我想向用户显示时钟,无论系统时间如何。可能未设置系统时间。

当我使用此代码时:



Hello,
I want to show clock to users regardless system time.it may system time is not set.
when i use this code:

DateTime.UtcNow





和我的系统时间不正确,这次也不正确。



我如何使用保存在数据库中的时区来确认。



i希望得到utcNow.TimeOfDay并与timeZone相加并向用户显示,正确的时间。



怎么办?





提前谢谢。



and my system time is not correct,this time too is not correct.

how do i utcNow with a time zone that save in database.

i want to get utcNow.TimeOfDay and sum with timeZone and show to user,correct time.

how can do it?


thank you in advance.

推荐答案

要执行此功能,您需要一个NTP客户。有几种实现方式,但随着时间的推移,由于操作系统中的内部任务切换器,它会漂移,因此您需要定期更新过度NTP以获得正确的值。



要回答上一个问题,服务器UTC时间将是正确的,无论您当前的系统时间如何。这就是NTP的重点。让你的生活更轻松:不要试图重新实现轮子:)
To do this functionality you will need a NTP client. There are several implementations, but over time it will drift, due to the internal task switcher in the OS, so periodically you will have to update the over NTP to get the correct value.

To answer your previous question, the server UTC time will be correct, regardless of your current system time. That's the whole point of NTP. Make your life easier: don't try to reimplement the wheel :)


来自 http://stackoverflow.com/questions/1193955/how-to-query-an-ntp-server-using-c [ ^ ]

Code from http://stackoverflow.com/questions/1193955/how-to-query-an-ntp-server-using-c[^]
public static DateTime GetNetworkTime()
{
    //default Windows time server
    const string ntpServer = "time.windows.com";

    // NTP message size - 16 bytes of the digest (RFC 2030)
    var ntpData = new byte[48];

    //Setting the Leap Indicator, Version Number and Mode values
    ntpData[0] = 0x1B; //LI = 0 (no warning), VN = 3 (IPv4 only), Mode = 3 (Client Mode)

    var addresses = Dns.GetHostEntry(ntpServer).AddressList;

    //The UDP port number assigned to NTP is 123
    var ipEndPoint = new IPEndPoint(addresses[0], 123);
    //NTP uses UDP
    var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

    socket.Connect(ipEndPoint);

    //Stops code hang if NTP is blocked
    socket.ReceiveTimeout = 3000;     

    socket.Send(ntpData);
    socket.Receive(ntpData);
    socket.Close();

    //Offset to get to the "Transmit Timestamp" field (time at which the reply 
    //departed the server for the client, in 64-bit timestamp format."
    const byte serverReplyTime = 40;

    //Get the seconds part
    ulong intPart = BitConverter.ToUInt32(ntpData, serverReplyTime);

    //Get the seconds fraction
    ulong fractPart = BitConverter.ToUInt32(ntpData, serverReplyTime + 4);

    //Convert From big-endian to little-endian
    intPart = SwapEndianness(intPart);
    fractPart = SwapEndianness(fractPart);

    var milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L);

    //**UTC** time
    var networkDateTime = (new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddMilliseconds((long)milliseconds);

    return networkDateTime.ToLocalTime();
}

// stackoverflow.com/a/3294698/162671
static uint SwapEndianness(ulong x)
{
    return (uint) (((x & 0x000000ff) << 24) +
                   ((x & 0x0000ff00) << 8) +
                   ((x & 0x00ff0000) >> 8) +
                   ((x & 0xff000000) >> 24));
}


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

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