Redux - Getting the Date and Time From the Internet
Gregory Lypny
gregory.lypny at videotron.ca
Thu Aug 18 17:42:22 EDT 2011
Hello everyone,
Here is a small enhancement to Stephen Barncard’s handler. If you call the function with the option “report” as in
put nistTime(“report”) into field “Date and Time”
you’ll get a summary. Set the field’s tabstops to 380 for a nice display.
After playing with it, I’ve come to the conclusion that the only correct, or most nearly correct, system-independent timestamp is the seconds as converted from a time server such as NIST. However, even that won’t display correctly in any other date-time format on a local machine if the time zone is set incorrectly. That means that the only display format that is sure to be correct is NIST’s GMT, uncontaminated by local time zone settings.
Regards,
Gregory
function nistTime pOption
-- Returns UTC time in RFC-867 (Daytime) format from the NIST
-- pOption is "array" or "report"
/*
N.B., The only datestamp that is independent of local system settings is the seconds converted
from NIST date and time (see nistSeconds). The only system-independent date
and time for display purposes on a local LiveCode app is NIST GMT date (see nistGMTInternetDate).
Converting nistSeconds to any date format will not display the correct date and time if the time zone
in the local machine is incorrect!
*/
-- This part by Stephen Barncard
put "time-C.timefreq.bldrdoc.gov:13" into pServerURL
set the socketTimeoutInterval to 1000
open socket to pServerURL
write "" to socket pServerURL
read from socket pServerURL for 49
put it into nistDateTime
put the result into theResult
close socket pServerURL
delete line 1 of nistDateTime -- UTC time in RFC-867
-- From the local computer's system settings
put the seconds into systemSeconds
put the internet date into systemInternetDate
-- Convert NIST date and time to other formats
get word 2 to 3 of nistDateTime
replace "-" with comma in it
replace ":" with comma in it
replace space with comma in it
add 2000 to the first item in it -- Kludge! Assume it is the 21st century.
put comma & 0 after it
convert it from dateitems to internet date -- Will have the correct GMT date and time but incorrectly the system's time zone
put 0000 into the last word of it -- Correct time zone GMT
put it into nistGMTInternetDate
convert it from internet date to seconds
put it into nistSeconds -- The correct seconds which should be "close" to systemSeconds
-- Correct system Internet date
put nistSeconds into correctedSystemInternetDate
convert correctedSystemInternetDate from seconds to internet date
-- Compare NIST and system seconds
put (systemSeconds - nistSeconds) into dSeconds -- The simple difference generally be less than ten seconds apart if NIST server is working optimally
switch pOption
case empty
return nistDateTime -- default
break
case "array"
put nistDateTime into theOutput["NIST Date and Time"]
put nistGMTInternetDate into theOutput["NIST Date"]
put nistSeconds into theOutput["NIST Seconds"]
put systemSeconds into theOutput["System Seconds"]
put dSeconds into theOutput["Difference"]
put systemInternetDate into theOutput["System Date"]
put correctedSystemInternetDate into theOutput["Corrected System Date"]
return theOutput
break
case "report"
put "NIST date and time:" & tab & nistDateTime & return & \
"NIST GMT date:" & tab & nistGMTInternetDate & return & \
"NIST seconds since midnight 1 Jan 1970:" & tab & nistSeconds & return & \
"Local system seconds since midnight 1 Jan 1970:" & tab & systemSeconds & return & \
"Difference (local minus NIST):" & tab & dSeconds & return & \
"Local system date:" & tab & systemInternetDate & return & \
"Corrected local date:" & tab & correctedSystemInternetDate into theOutput
return theOutput
break
default
return nistDateTime
break
end switch
end nistTime
More information about the use-livecode
mailing list