Use UDP services from various NTP servers to get exact time

Initializations

In[]:=
gridOpts={Alignment{Left,Center},ItemSizeFull,FrameAll,BackgroundLightBlue};
In[]:=
HyperlinkButton[text_,url_]:=​​Hyperlink[Button[Style["Hyperlink: "<>text,"Subsection",Black]],url]

References

In[]:=
Grid[{{HyperlinkButton["NTP Pool - List of timeservers","https://www.ntppool.org/de/"],HyperlinkButton["Wikipedia - NTP Time Protocol","https://en.wikipedia.org/wiki/Network_Time_Protocol"]},{HyperlinkButton["Microsoft - UDP Client Class","http://msdn.microsoft.com/de-de/library/system.net.sockets.udpclient.aspx#Y2926"],HyperlinkButton["IETF - NTP V4 Specification","http://tools.ietf.org/html/rfc5905"]}},gridOpts]
Out[]=

UDP NTP Access

https://forum.arduino.cc/index.php?PHPSESSID=ht6thhm49pa2fji8gij90iu733&topic=197637.msg1458539#msg1458539
NTP & DST handling
http://www.webexhibits.org/daylightsaving/b2.html
In the European Union, Summer Time begins and ends at 1:00 a.m. Universal Time (Greenwich Mean Time). It begins the last Sunday in March and ends the last Sunday in October. In the EU, all time zones change at the same moment.
2019 March 31 October 272020 March 29 October 252021 March 28 October 312022 March 27 October 30
In[]:=
LocalTime[]
Out[]=
Sun 8 Dec 2019 14:08:45GMT+1.
In[]:=
LocalTimeZone[]
Out[]=
Europe/Berlin
In[]:=
Needs["NETLink`"]
In[]:=
InstallNET[]
Out[]=
LinkObject
Name:
C:\Program Files\Wolfram Research\Mathematica\12.0\SystemFiles\Links\NETLink\InstallableNET.exe
Link mode: Listen

In[]:=
myPadding[x_]:=If[x≥100,PaddedForm[x,{5,2},NumberPadding{"","0"}],PaddedForm[x,{4,2},NumberPadding{"0","0"}]]

NTP interpreted values

In[]:=
leapIndicatorList={"No warning","Last minute of day has 61 sec","Last minute of day has 59 sec","Alarm"};​​modeList={"Unknown","Symmetric Active","Symmetric Passive","Client","Server","Broadcast","NTP control message"};​​stratumList={"Unspecified","Primary Server","Secondary Server","Unsynchronized","Reserved"};
In[]:=
(*ntpServer="64.90.182.55";(*NISTserver,NY,USA*)*)
In[]:=
ntpServer="192.53.103.108";(*PTBserverBraunschweigGermany*)
In[]:=
ntpServer="pool.ntp.org";(*World-widepool*)
In[]:=
ntpServer="europe.pool.ntp.org";(*Europe—europe.pool.ntp.org*)
In[]:=
ntpServer="2.europe.pool.ntp.org";(*OneoffourserversinEurope—europe.pool.ntp.org*)
Prepares the NTP UDP request package. which consist of 48 bytes.
In[]:=
packetLength=48;​​packet=Table[0,{packetLength}];​​packet[[1;;4]]={2^^11100011,10^^0,10^^6,16^^EC};​​packet[[13;;16]]={10^^49,16^^4E,10^^49,10^^52};
In[]:=
sendPacket=NETNew["System.Byte[]",packetLength];​​sendPacket=packet;​​receivePacket=NETNew["System.Byte[]",packetLength];
In[]:=
udp=NETNew["System.Net.Sockets.UdpClient"];​​endPoint=NETNew["System.Net.IPEndPoint",1,1];
In[]:=
udp@Connect[ntpServer,123]
In[]:=
err=udp@Send[sendPacket,packetLength]
Out[]=
48
In[]:=
available=udp@Available[]
Out[]=
0
In[]:=
broadcatsQ=udp@EnableBroadcast[];
In[]:=
ttlNumber=udp@Ttl[];
In[]:=
receivePacket=udp@Receive[endPoint];
In[]:=
endPoint@Address@ToString[]
Out[]=
193.182.111.142
In[]:=
endPoint@ToString[]
Out[]=
193.182.111.142:123
In[]:=
NETObjectToExpression[endPoint@ToString[]]
Out[]=
193.182.111.142:123
In[]:=
dataPacket=NETObjectToExpression[receivePacket]
Out[]=
{36,2,6,236,0,0,0,72,0,0,6,143,192,36,143,130,225,151,113,176,102,41,195,44,0,0,0,0,0,0,0,0,225,151,116,94,172,64,160,89,225,151,116,94,172,64,198,16}
In[]:=
leapIndicator=BitShiftRight[BitAnd[dataPacket[[1]],2^^11000000],6]
Out[]=
0
In[]:=
versionNumber=BitShiftRight[BitAnd[dataPacket[[1]],2^^00111000],3]
Out[]=
4
In[]:=
mode=BitAnd[dataPacket[[1]],2^^00000111]
Out[]=
4
In[]:=
stratum=dataPacket[[2]]
Out[]=
2
In[]:=
stratumText=Which[​​stratum==0,stratumList[[1]],​​stratum1,stratumList[[2]],​​2≤stratum≤15,stratumList[[3]],​​stratum16,stratumList[[4]],​​stratum>16,stratumList[[5]]​​]
Out[]=
Secondary Server
In[]:=
poll=If[dataPacket[[3]]>127,-(255-dataPacket[[3]]),dataPacket[[3]]]
Out[]=
6
In[]:=
pollValue=2^poll
Out[]=
64
In[]:=
precision=If[dataPacket[[4]]>127,-(255-dataPacket[[4]]),dataPacket[[4]]]
Out[]=
-19
In[]:=
precisionValue=1000.02^precision
Out[]=
0.00190735
In[]:=
rootDelay=dataPacket[[5;;8]]
Out[]=
{0,0,0,72}
In[]:=
rootDelayValue=1000.0(Fold[256#1+#2&,0,dataPacket[[5;;6]]]+Fold[256#1+#2&,0,dataPacket[[7;;8]]]/2^16)
Out[]=
1.09863
In[]:=
rootDispersion=dataPacket[[9;;12]]

Dynamic NTP Access

Information on System.Net.Sockets.UdpClient

Copyright & Version

© Ernst H.K. Stelzer, 2012-2020.
ernst.stelzer@physikalischebiologie.de
Version 2.1, December 2019. Please do not hesitate to send me your comments and suggestions.​
​
Physical Biology (FB 15, IZN), www.physikalischebiologie.de
Buchmann Institute for Molecular Life Sciences (CEF-MC)
Goethe-Universität Frankfurt am Main (Campus Riedberg)
Max-von-Laue-Straße 15, D-60438 Frankfurt am Main, Germany
Tel   +49 (69) 798 42547, x42545, Fax +49 (69) 798 42546