' Balthasar Indermuehle ' SNTP implementation ' 'JPRO_COMPILER=PB/CC 'Tiny implementation of the Time Protocol (RFC-868) 'Put into public domain by Balthasar Indermuehle balt3@inside.net on July 9th 2005 'Feel free to use, modify as you see fit. As usual, no warranties are given. 'NOTE: This code only produces useful results if your computer clock is no more than 24h offset! #COMPILE EXE #INCLUDE "win32api.inc" FUNCTION PBMAIN LOCAL hTCP AS LONG LOCAL sBuf AS STRING LOCAL sNTP AS STRING LOCAL dNTP AS DOUBLE LOCAL st AS SYSTEMTIME LOCAL dTime AS DOUBLE hTCP = FREEFILE TCP OPEN PORT 37 AT "swisstime.ethz.ch" AS #hTCP TIMEOUT 4000 DO TCP RECV #hTCP, 4, sBuf sNTP = sNTP & sBuf LOOP WHILE ISTRUE LEN(sBuf) AND ISFALSE ERR TCP CLOSE #hTCP IF NOT ERR THEN 'Get the amount of days since Jan 1 1900 GetSystemTime st SystemTimeToVariantTime st, dTime 'Microsoft variant time for some reason has Midnight of Jan 1 1900 = 2.0, so we need to subtract that dTime = dTime - 2 'dNTP is the number of seconds elapsed since midnight jan 1 1900 dNTP = ASC(LEFT$(sNTP, 1)) * 256 ^ 3 + ASC(MID$(sNTP, 2, 1)) * 256 ^ 2 + ASC(MID$(sNTP, 3, 1)) * 256 ^ 1 + ASC(RIGHT$(sNTP, 1)) 'now reduce to the number of seconds since midnight: dNTP = dNTP - FIX(dTime) * 86400 'and fill in the systemtime struct with the derived hours, minutes and seconds elapsed since midnight: st.wHour = FIX(dNTP/3600) st.wMinute = FIX((dNTP - st.wHour * 3600) / 60) st.wSecond = dNTP - st.wHour * 3600 - st.wMinute * 60 'output: STDOUT "UTC: " & FORMAT$(st.wYear,"0000") & "-" & FORMAT$(st.wMonth,"00") & "-" & FORMAT$(st.wDay,"00") & " " & _ FORMAT$(st.wHour, "00") & ":" & _ FORMAT$(st.wMinute, "00") & ":" & _ FORMAT$(st.wSecond, "00") ELSE STDOUT "Error occurred: " & ERROR$ END IF END FUNCTION