从NTP服务器获取时间(C#)

来源:互联网 发布:java完整项目案例 编辑:程序博客网 时间:2024/05/25 19:56

       从NTP服务器可以获取准确的时间,常常被用于校准本地时间。子丰采用阿里的NTP服务器ntp1.aliyun.com进行介绍,需要注意的是从NTP服务器获得的时间是UTC时间,其与北京时间相差8个小时(北京时间 = UTC时间 + 8小时)。

       在C#中可以使用DateTime的成员函数ToLocalTime()自动将UTC时间转换为本地时间(根据本地计算机设置的时区不同,其转换得到的时间也不同),只需要将本地计算机的时区设置为中国即可。

using System;using System.Net;using System.Net.Sockets;// 获取网络时间public DateTime getWebTime(){    // default ntp server    const string ntpServer = "ntp1.aliyun.com";    // NTP message size - 16 bytes of the digest (RFC 2030)    byte[] 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)    IPAddress[] addresses = Dns.GetHostEntry(ntpServer).AddressList;    // The UDP port number assigned to NTP is 123    IPEndPoint ipEndPoint = new IPEndPoint(addresses[0], 123);    // NTP uses UDP    Socket 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 = swapEndian(intPart);    fractPart = swapEndian(fractPart);    ulong milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000UL);    // UTC time    DateTime webTime = (new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddMilliseconds(milliseconds);    // Local time    return webTime.ToLocalTime();}// 小端存储与大端存储的转换private uint swapEndian(ulong x){    return (uint)(((x & 0x000000ff) << 24) +    ((x & 0x0000ff00) << 8) +    ((x & 0x00ff0000) >> 8) +    ((x & 0xff000000) >> 24));}

注:这里使用小端存储与大端存储的转换,这是因为阿里服务器和本地计算机使用了不同的存储方式。因此,在实际使用过程中,还需要根据自己本地计算机判断是否需要进行小端与大端的转换。

原创粉丝点击