Using ntp server for System Time?

Is there a simple example on how to use an NTP server to set the correct time on my Fez Hydra?

When you get a network connection call this


// get Internet Time using NTP
NTPTime("time.windows.com", -360);

-360 is my timezone offset

and this is the routine it calls


public static bool NTPTime(string TimeServer, int GmtOffset = 0)
        {
            Socket s = null;
            try
            {
                EndPoint rep = new IPEndPoint(Dns.GetHostEntry(TimeServer).AddressList[0], 123);
                s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                var ntpData = new byte[48];
                Array.Clear(ntpData, 0, 48);
                ntpData[0] = 0x1B; // Set protocol version
                s.SendTo(ntpData, rep); // Send Request   
                if (s.Poll(30 * 1000 * 1000, SelectMode.SelectRead)) // Waiting an answer for 30s, if nothing: timeout
                {
                    s.ReceiveFrom(ntpData, ref rep); // Receive Time
                    byte offsetTransmitTime = 40;
                    ulong intpart = 0;
                    ulong fractpart = 0;
                    for (var i = 0; i <= 3; i++) intpart = (intpart << 8) | ntpData[offsetTransmitTime + i];
                    for (var i = 4; i <= 7; i++) fractpart = (fractpart << 8) | ntpData[offsetTransmitTime + i];
                    var milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000L);
                    s.Close();
                    var dateTime = new DateTime(1900, 1, 1) +
                                   TimeSpan.FromTicks((long)milliseconds * TimeSpan.TicksPerMillisecond);

                    Utility.SetLocalTime(dateTime.AddMinutes(GmtOffset));

                    return true;
                }
                s.Close();
            }
            catch
            {
                try
                {
                    s.Close();
                }
                catch
                {
                }
            }
            return false;
        }

@ andre.m - So much for using the ‘code’ indicator to wrap code in. Quote doesn’t work so well either.

Thanks. I got a little comfused by the image tags in that code, here’s what I ended up with:


        public static DateTime NTPTime(string timeServer, int gmtOffset = 0)
        {
            // Port to use on UDP
            const int UDP_PORT_FOR_NTP = 123;
            
            var ntpData = new byte[48];
            ntpData[0] = 0x1B; //LeapIndicator = 0 (no warning), VersionNum = 3 (IPv4 only), Mode = 3 (Client Mode)

            var addresses  = Dns.GetHostEntry(timeServer).AddressList;
            var ipEndPoint = new IPEndPoint(addresses[0], UDP_PORT_FOR_NTP);
            var socket     = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

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

            ulong intPart = (ulong)ntpData[40] << 24 | (ulong)ntpData[41] << 16 | (ulong)ntpData[42] << 8 | (ulong)ntpData[43];
            ulong fractPart = (ulong)ntpData[44] << 24 | (ulong)ntpData[45] << 16 | (ulong)ntpData[46] << 8 | (ulong)ntpData[47];

            var milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L);
            var networkDateTime = (new DateTime(1900, 1, 1)).AddMilliseconds((long)milliseconds);
            var adjustedTime = networkDateTime.AddHours(gmtOffset);
            Utility.SetLocalTime(adjustedTime);
            return adjustedTime;
        }