WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

s... Socket Functions

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Get the Time from an Internet Time Server


Here's a script to get the time from an internet time server. (Uses the WINSOCK extender.)

Note: time is returned as universal time (UTC).


; Connect to a remote server. 
; Can be any server that responds to the "daytime" protocol,
; using the time format defined by NIST. 
; See... http://tf.nist.gov/service/time-servers.html
;
AddExtender("wwwsk34i.dll")
Socket = sOpen()
if !Socket 
err=wxGetLastErr()
Message("Error",StrCat("Winsock Open Error """,err,"""",,@CRLF,wxGetErrDesc(err))
return
endif
ok = sConnect(Socket,"time.nist.gov","daytime")
if (!ok)
err=wxGetLastErr()
Message("Error",StrCat("Winsock Connect Error """,err,"""",,@CRLF,wxGetErrDesc(err))
return
endif

;
; Some servers return blank line first, then return time on the 2nd try.
; That's why this read statement is in a loop.
;
for x = 1 to 3
Msg=StrTrim(sRecvLine(socket,256)) 
Err = wxGetLastErr()
if Err != @sOK
sClose(socket) 
Message("Error",StrCat("Winsock Receive Error """,err,"""",,@CRLF,wxGetErrDesc(err))
return
endif
if Msg != "" then break
next
sClose(socket) 

;
; Extract the date and time from the message
;
Msg = ItemRemove(1,Msg," ")
Date = StrReplace(ItemExtract(1,Msg," "),"-",":")
Msg = StrTrim(ItemRemove(1,Msg," "))
Time = StrCat("20",Date,":",StrTrim(ItemExtract(1,Msg," ")))

Message("",StrCat("UTC Time From Server = ",Time))
return

Article ID:   W16899
File Created: 2007:07:03:14:26:48
Last Updated: 2007:07:03:14:26:48