C#通过NTP服务器获取NTP时间
注意事项:
- 如果NTP服务器地址是域名,如阿里云的NTP服务器地址。需要DNS解析。
- NTP使用UDP通讯,默认端口是123
- NTP经过很多年的发展,有4个版本号,目前常用的3和4。NTP区分客户端和服务端,客户端角色标志为3。
- NTP发送的时间戳是第41到48个字节。获取到的字节需要转为大端序列。
- NTP标准协议返回的时间是自1900.1.1 00:00:00开始的毫秒时间数值,需要字节转换为你需要的日期时间。
以下是通过NTP服务器获取NTP时间的代码:
/// <summary>
/// 获取NTP时间
/// </summary>
/// <param name="serverList"></param>
/// <param name="timeoutMilliseconds"></param>
/// <param name="isToLocal"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static async Task<DateTime> GetNtpTime(List<string> serverList, bool isToLocal = false, int timeoutMilliseconds = 2000)
{List<string> realServerList = new List<string>();bool needUseLastIp = false;if (serverList is null || !serverList.Any()){serverList = NtpServers;needUseLastIp = true;if (!string.IsNullOrEmpty(lastIpAddress)){realServerList.Add(lastIpAddress);if (serverList.Contains(lastIpAddress)){serverList.Remove(lastIpAddress);}}}realServerList.AddRange(serverList);Log.Information($"GetNtpTime realServerList: {JsonConvert.SerializeObject(realServerList)}");foreach (var server in realServerList){try{IPAddress? ipAddress;List<IPAddress> addressList = new List<IPAddress>();if (!IPAddress.TryParse(server, out ipAddress)){// 如果不是IP地址,则尝试DNS解析try{var hostEntry = await Dns.GetHostEntryAsync(server);addressList = hostEntry.AddressList.ToList();}catch (Exception e){Log.Error($"DNS resolution failed: {e.Message} {e.StackTrace}");}}else{addressList.Add(ipAddress);}if (addressList is null || !addressList.Any()){continue;}foreach (var address in addressList){using (var client = new UdpClient()){client.Client.ReceiveTimeout = timeoutMilliseconds;client.Client.SendTimeout = timeoutMilliseconds;// 发送NTP请求var request = new byte[48];request[0] = 0x1B; // 设置版本号4,模式3(客户端)var endPoint = new IPEndPoint(address, 123);client.Send(request, request.Length, endPoint);// 发送请求并接收响应var response = await client.ReceiveAsync();// 验证响应数据长度if (response.Buffer.Length < 48 || !IsValidNtpResponse(response.Buffer))continue;// 解析时间戳(注意:BitConverter默认是小端,需要反转)ulong intPart = BitConverter.ToUInt32(response.Buffer, 40);ulong fractPart = BitConverter.ToUInt32(response.Buffer, 44);// 转换为大端序(NTP使用大端)intPart = SwapEndianness((uint)intPart);fractPart = SwapEndianness((uint)fractPart);// 计算总时间戳ulong ntpTimestamp = (intPart << 32) | (fractPart & 0xFFFFFFFF);double totalSeconds = (double)ntpTimestamp / (double)(1UL << 32);if (needUseLastIp){lastIpAddress = endPoint.Address.ToString();Log.Information($"记录本次获取NTP时间的IP为:{lastIpAddress}");}// 转换为DateTimeDateTime epoch = new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc);DateTime ntpTime = epoch.AddSeconds(totalSeconds);if (isToLocal){ntpTime = ntpTime.ToLocalTime();}return ntpTime;}}}catch (Exception ex){Log.Error($"连接服务器 {server} 失败: {ex.Message} {ex.StackTrace}");continue;}}throw new Exception("所有NTP服务器均无法连接!");
}
获取到的时间,如果需要自己进行时区转换,可以通过C#代码从电脑本机获取到标准的时区列表,然后将获取到的NTP时间加上时区的BaseUtcOffset,即可转换到所需要的时区时间。
C#获取时区列表代码如下:
var allZones = TimeZoneInfo.GetSystemTimeZones().ToList();