Monday, November 08, 2010 at 12:06 AM.


on getCurrentTime (server="", flMessages=true) {
		<<10/2/01; 9:17:54 PM by DW
			<<Created. Use the RFC 868 protocol to talk to a NIST server to get the current date-time. 
			<<It returns a binary number of seconds since 1/1/1900. Frontier's internal time format is expressed in the number of seconds since 1/1/04.
				<<date (0)
					<<"1/1/1904; 12:00:00 AM"
				<<So, our mission is to subtract from the number returned by NIST the number of seconds in four years.
				<<4 * 365 * 24 * 60 * 60
				<<Aha, that worked. Now we have to adjust for GMT.
			<<date.getCurrentTimeZone ()
				<<"This simple protocol returns a 32-bit unformatted binary number that represents the time in UTC seconds since January 1, 1900. The server listens for Time Protocol requests on port 37, and responds in either tcp/ip or udp/ip formats. Conversion to local time (if necessary) is the responsibility of the client program. The 32-bit binary format can represent times over a span of about 136 years with a resolution of 1 second. There is no provision for increasing the resolution or increasing the range of years.
				<<"The strength of the time protocol is its simplicity. Since many computers keep time internally as the number of seconds since January 1, 1970 (or another date), converting the received time to the necessary format is often a simple matter of binary arithmetic. However, the format does not allow any additional information to be transmitted, such as advance notification of leap seconds or daylight savings time, or information about the health of the server."
				<< NIST, Gaithersburg, Maryland 
				<< NIST, Gaithersburg, Maryland 
				<< NIST, Boulder, Colorado 
				<< NIST, Boulder, Colorado 
				<< NIST, Boulder, Colorado 
				<< University of Colorado, Boulder 
				<< NCAR, Boulder, Colorado 
				<< Microsoft, Redmond, Washington 
				<< Datum, San Jose, California 
				<< Abovenet, Virginia 
				<< Abovenet, New York City 
				<< Abovenet, San Jose, California 
				<< TrueTime, AOL facility, Sunnyvale, California 
				<< TrueTime, AOL facility, Virginia 
	on displayMsg (s) {
		msg ("tcp.getCurrentTime: " + s)};
	if tcp.isOffline () {
		scriptError ("Can't get the current time because TCP is offline.")};
	if flMessages {
		displayMsg ("Connecting to " + server + ".")};
	local (stream = tcp.openStream (server, 37));
	if flMessages {
		displayMsg ("Waiting for data from " + server + ".")};
	local (d = date (number (tcp.readStream (stream, 4)) - 126144000 + date.getCurrentTimeZone ()));
	tcp.closeStream (stream);
	if flMessages { //clean up the message area
		msg ("")};
	return (d)}
<<bundle //test code
	<<dialog.alert (tcp.getCurrentTime ())

This listing is for code that runs in the OPML Editor environment. I created these listings because I wanted the search engines to index it, so that when I want to look up something in my codebase I don't have to use the much slower search functionality in my object database. Dave Winer.