79162703

Date: 2024-11-06 13:07:41
Score: 0.5
Natty:
Report link

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());
            }
        }
    }
}
Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: lalaki