Very good, based on the above answers, I have summarized this answer, and I believe my implementation has the best performance.
Since the above answers have been explained quite clearly, I did not add any comments.
I think this is what you need:
using System;
using System.Net;
using System.Net.Sockets;
internal static class Program
{
// stackoverflow.com/a/3294698/162671
private static ulong SwapEndianness(ulong x)
{
return (((x >> 24) & 0x000000ff) | ((x >> 8) & 0x0000ff00) | ((x << 8) & 0x00ff0000) | ((x << 24) & 0xff000000)) * 1000;
}
private static void Main(string[] args)
{
var time = new DateTime(1900, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var data = new byte[48];
data[0] = 0x1B;
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
socket.Connect(Dns.GetHostAddresses("time.apple.com"), 123);
socket.Send(data);
socket.Receive(data);
}
unsafe
{
fixed (byte* ptr = data)
{
var intPart = (uint*)(ptr + 40);
var fractPart = (uint*)(ptr + 44);
time = time.AddMilliseconds((SwapEndianness(*fractPart) >> 32) + SwapEndianness(*intPart));
Console.WriteLine(time);
Console.WriteLine(time.ToLocalTime());
}
}
}
}