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

Time - Timer and Date Functions
plus

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

Setting Time from Time Servers

Keywords: 	 setting time from time servers

I found some previous posts for setting time from time servers. However, it does not work if your proxy does not allow it. The attached always works, provided you find a web (http) server whose time stamp you trust (I used tycho.usno.navy.mil).

The script only works on W9x platform. I heard that the NT registry entry for the time bias is different, the script would probably fail on NT/2000.


;
; copyright Alex.Fung.Ho-san@graduate.hku.hk November 2000
; set Windows clock from web server or command line argument
;
; usage:
;   settime
;     set clock from web server
;   settime hh:mm:ss
;     change the time to param1
;   settime hh:mm:ss dd/mm/yy
;     change date and time to param2 and param1
;     always assume dd/mm/yy irrespective of international settings
;
; does not yet work on NT/2000
;

timeServer="tycho.usno.navy.mil" 
; actually the site is accessed via http 
; and the header date is extracted for use

select param0
  case 0
    goSub p0
    break
  case 1
    newtime=strCat(strSub(timeYmdHms(),1,11),param1)
    intControl(58,newtime,0,0,0)
    display(5,strCat("set time from",timeYmdHms()),newtime)
    break
  case 2
    if itemCount(param2,"/") == 3
      newd=itemExtract(1,param2,"/")
      newm=itemExtract(2,param2,"/")
      newy=itemExtract(3,param2,"/")
      intControl(58,strCat("20",newy,":",newm,":",newd,":",param1),0,0,0)
      display(5,strCat("set date from",timeYmdHms()),strCat("20",newy,":",newm,":",newd,":",param1))
    else
      message("Incorrect argument to settime",param2)
    endIf
    break
  case param0
    message("Incorrect argument to settime","%param0% arguments received")
endSelect
exit

:p0
  AddExtender("WWINT34I.DLL")
  tophandle=iBegin(2,"","")
  for j = 1 to 5 ; retry 5 times
    connecthandle=iHostConnect(tophandle, timeServer ,@HTTP,"", "")
    t1=getTickCount()
    datahandle=iHttpInit(connecthandle, "GET", "/cgi-bin/timer.pl", "",0)
    rslt=iHttpOpen(datahandle, "" , 0, 0)
    h=iHttpHeaders(datahandle)
    t2=getTickCount()
    if t2-t1 > 2000
      continue
    else
      d=@tab
      n=itemCount(h,d)
      for i = 1 to n
        ni=itemExtract(i,h,d)
        if strFix(ni," ",5) == "Date:"
          parseData(strSub(ni,6,-1))
          ; eg "Mon, 16 Oct 2000 08:15:16 GMT"
          break
        endIf
      next i
    endIf
    iClose(datahandle)
    iClose(connecthandle)
    iClose(tophandle)
    if param0 == 6 then break
  next j
  if param0 != 6 
    message("set time","cannot get time reliably from server")
    exit
  endIf
  drop(tophandle, connecthandle, datahandle, rslt, h, d, n, i)
  if param6 != "GMT"
    message("Do not know how to handle this date stamp",ni)
    eit
  endIf
  months="JanFebMarAprMayJunJulAugSepOctNovDec"
  monthstr=param3
  month=((StrIndexNC(months,param3,0,@fwdscan) - 1) / 3) +1
  YmdHms=Strcat(param4,":",month,":",param2,":",param5)
  drop (months, month, ni)

  kTimeBias=regOpenKey(@regMachine,"System\CurrentControlSet\control\TimeZoneInformation")
  if regEntryType(kTimeBias,"[ActiveTimeBias]") != 3
    ; NT returns something else
    ; let me know what really happens there
    message("Cannot extra time bias on this system",strCat("format is :",regEntryType(kTimeBias,"[ActiveTimeBias]")))
    exit
  endIf
  hTimeBias=regQueryBin(kTimeBias,"[ActiveTimeBias]") 
  regCloseKey(kTimeBias)
  drop(kTimeBias)

  addextender("wilx32i.dll")
  mTimeBias = -xHex(strCat(strSub(hTimeBias,10,2),strSub(hTimeBias,7,2),strSub(hTimeBias,4,2),strSub(hTimeBias,1,2)))
  ; minutes faster than GMT
  if mTimeBias >= 0
    newtime=timeAdd(YmdHms,strCat("0:0:0:0:",mTimeBias,":",getTickCount()/1000 - (t1+t2)/2000)) ; time since mid point of t1 and t2.
  else
    newtime=timeAdd(YmdHms,strCat("0:0:0:0:0:",getTickCount()/1000 - (t1+t2)/2000)) ; time since mid point of t1 and t2.
    newtime=timeSubtract(YmdHms,strCat("0:0:0:0:",-mTimeBias,":0")
  endIf
  intControl(58,newtime,0,0,0)
  drop(mTimeBias,YmdHms,t1,t2)
  display(5,strCat("set time from",timeYmdHms()),newtime)
  return
  

Article ID:   W14982
File Created: 2002:07:15:12:58:06
Last Updated: 2002:07:15:12:58:06