Documente Academic
Documente Profesional
Documente Cultură
vbs
timechk = Now
http.send
localdate = Now
lag = DateDiff("s", timechk, localdate)
'
'Key concept for script is reading header date.
gmttime = http.getResponseHeader("Date")
'
'Trim results to valid date format.
gmttime = right(gmttime, len(gmttime) - 5)
gmttime = left(gmttime, len(gmttime) - 3)
'
'If less than 2 seconds lag we can use the results.
If lag < 2 Then Exit For
Next
'
'If still too much lag after 5 attemps, quit.
If n = 4 then
ws.Popup "Unable to establish a reliable connection " & _
"with time server. This could be due to the " & _
"time server being too busy, your connection " & _
"already in use, or a poor connection." & vbcrlf & _
vbcrlf & "Please try again later.", 5, strTitle
Cleanup
End If
'
'Time and date error calculations.
Dim remotedate, diff, newnow, newdate, newtime, ddiff, sdiff
'Add local time zone offset to GMT returned from USNO server.
remotedate = DateAdd("h", timeoffset, gmttime)
'Calculate seconds difference betweed remote and local.
diff = DateDiff("s",localdate,remotedate)
'Adjust for difference and lag to get actual time.
newnow = DateAdd("s", diff + lag, now)
'Split out date and calculate any difference.
newdate = DateValue(newnow)
ddiff = DateDiff("d", Date, newdate)
'Split out time.
newtime = TimeValue(newnow)
'Convert time to 24 hr format required for OS compatibility.
newtime = Right(0 & Hour(newtime), 2) & ":" & _
Right(0 & Minute(newtime), 2) & ":" & _
Right(0 & Second(newtime), 2)
'Calculate time difference.
sdiff = DateDiff("s", time, newtime)
'
'If off by 1 or more seconds, adjust local time
Dim tmsg
If sdiff < 2 and sdiff > -2 Then
tmsg = "System is accurate to within " & _
"1 second. System time not changed."
Else
'Run DOS Time command in hidden window.
-2-
0
_
to " & _
0
" & _
-3-