Time Server

Mark Schonewille m.schonewille at economy-x-talk.com
Sun Jul 27 16:00:17 EDT 2008


Thanks for the good advice, Gordon.

Although I would like to be able to talk to SNTP server directly, it  
is sufficient for now. Here is the script. I'll probably put a little  
libary on-line on the Economy-x-Talk website later. Mind line wraps.

-- !!! start here !!!
/* simple NTP client over Telnet
Copyright©2008 by Economy-x-Talk
and Mark Schonewille, Nijmegen
the Netherlands

Copy this button (script) to your own project or attach
this stack as a substack. Inser the script of this
button into back - use it as a backscript.

Call the getTime handler as follows:

getTime <callbackMessage>[,server[,port]

- callbackMessage is the name of the handler that will trap
the callback message
- the optional server parameter is the address of a time server
- the port parameter is optional as well and contains the port
number at which the server is contacted; the port number is only
necessary if not included with the server address.

Examples:

getTime "timeReceived"
getTime "timeReceived","time.nist.gov:13"
getTime "timeReceived","time.nist.gov",13

Above examples need a handler names "timeReceived". The
callback message comes with one parameter. This parameter is
an array with 9 keys.

d-st: a code indicating daylight saving or standart time
julian date: (modified) Julian date
error: any error messages
compensated delay: time adjustment, which makes sure that the time is  
right on arrival
leap: whether a 1 second time adjustment was applied
raw: the original time stamp use to compose the array
server health: whether the server is running without problems
date: date in American format
time: long time

See http://tf.nist.gov/service/its.htm for more information.

The object, card or stack that calls the getTime handler
must have a handler to trap the callback message.

Credits:
Thanks to Gordon Tillman for the hint to use Telnet and to
whoever made the Telnet.mc stack for MetaCard.
*/


constant kTimeServer = "time.nist.gov:13"

local lCallbackMessage, lButtonID

on getTime theCallbackMessage,theServer,thePort
    if theServer is empty then put kTimeServer into theServer
    if thePort is not empty then put colon & thePort after theServer
    put theCallbackMessage into lCallbackMessage
    put the long id of the target into lButtonID
    open socket to theServer
    if the result is not empty then return false
    read from socket theServer with message "rcvTime"
    if the result is not empty then return false
    else return true
end getTime

on rcvTime theSock,theTime
    put the long id of the target
    filter theTime without empty
    put theTime into myTimeArray["raw"]
    put word 1 of theTime into myTimeArray["julian date"] -- modified  
julian date
    put word 2 of theTime into myTimeArray["date"]
    put word 3 of theTime into myTimeArray["time"]
    put word 4 of theTime into myTimeArray["d-st"] --tt
    put word 5 of theTime into myTimeArray["leap"]
    put word 6 of theTime into myTimeArray["server health"]
    put word 7 of theTime into myTimeArray["compensated delay"] -- msADV
    if not ((word 6 of theTime) is 0 and last char of theTime is "*")  
then
       put "Error: server may return invalid time" into  
myTimeArray["error"]
    else
       put empty into myTimeArray["error"]
    end if
    do "send" && quote & lCallbackMessage && "myTimeArray" & quote &&  
"to lButtonID"
    close socket theSock -- normally unnecessary
end rcvTime

-- the following handler is useful
-- of you have your script in a checkbox
on mouseUp
    if the id of the target is the id of me then
       if the long id of me is in the backscripts then
          remove the script of me from back
          set the hilite of me to false
       else
          insert the script of me into back
          set the hilite of me to true
       end if
    end if
end mouseUp

on socketError theSock,theErr
    put "error:" && theErr into myTimeArray["error"]
    do "send" && quote & lCallbackMessage && "myTimeArray" & quote &&  
"to lButtonID"
end socketError

on socketTimeout theSock
    put "error:" && "socket timeout" into myTimeError["error"]
    do "send" && quote & lCallbackMessage && "myTimeArray" & quote &&  
"to lButtonID"
end socketTimeout
-- !!! finish !!! ------------


--
Best regards,

Mark Schonewille

Economy-x-Talk Consulting and Software Engineering
http://economy-x-talk.com
http://www.salery.biz

Benefit from our inexpensive hosting services. See http://economy-x-talk.com/server.html 
  for more info.

On 27 jul 2008, at 16:51, Gordon Tillman wrote:

> Mark I know you can talk to an SNTP server with telnet.  Here I just  
> did the following:
>
>
> $ telnet time-a.nist.gov 13
> Trying 129.6.15.28...
> Connected to time-a.nist.gov.
> Escape character is '^]'.
>
> 54674 08-07-27 14:50:55 50 0 0 340.9 UTC(NIST) *
> Connection closed by foreign host.
>
> --gordon




More information about the use-livecode mailing list