;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       GetTimeNtp() 
;; 
;;ACTION         Get current time from NTP 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.0  - 2019/12/24 
;; 
;;HISTORY        1.0  - 2019/12/24 - Initial Release 
;; 
;;SYNTAX         GetTimeNtp([Target] [,Flag] [, Method]) 
;; 
;;PARAMETERS     Target - OPTIONAL - The NTP time server to query (default is pool.ntp.org) 
;; 
;;               Flag - OPTIONAL - Convert the time to UTC if true (default is local time) 
;; 
;;               Method - OPTIONAL - Control Operation 
;;                 0  = Use Kixforms DLL 
;;                 1  = Use WshPipe exec method if true (default is KixForms.DLL) 
;;                Obj = Use the passed KF object 
;; 
;;REMARKS        Queries the current time from a defined NTP server using KixForms DLL or WSH  
;;               to run the command. The KF version will not flash the command window and is 
;;               preferred for interactive utilities. 
;; 
;;RETURNS        String - YYYY/MM/DD HH:MM:SS 
;; 
;;DEPENDENCIES   TimeConvert() UDF 
;;               KixForms.DLL (if KF method selected) - DEFAULT 
;;               WSH support (if WSH method selected) 
;; 
;;TESTED WITH    W2K8, W2K12, W2K16 
;; 
;;EXAMPLES       ; get from default server in local-relative 
;;               $Time = GetTimeNtp() 
;;EXAMPLES       ; get from specific server in UTC-relative 
;;               $UTime = GetTimeNtp('eu.pool.ntp.org', 1) 
; 
Function GetTimeNtp(OPTIONAL $_NtpSrc, $_Format, $_Method)
 
  Dim $_oSys				                        ; System object 
  Dim $_aData, $_Data, $_I				        ; Data array & var 
  Dim $_Cmd                                                     ; Command to run 
  Dim $_oWMIService, $_cItems, $_oItem				; WMI object and collection vars 
  Dim $_Offset	                                                ; TimeZone Offset 
  
  ; Validate arguments 
  $_NtpSrc = IIf($_NtpSrc = '', 'pool.ntp.org', $_NtpSrc)
  $_Format = IIf($_Format = '', 0, Val($_Format))
  If VarType($_Method) = 9
    $_oSys = $_Method
    $_Method = -1
  Else
    $_Method = IIf($_Method = '', 0, Val($_Method))
  EndIf
 
  ; Define the command to run 
  $_Cmd = 'w32tm /stripchart /computer:' + $_NtpSrc + ' /dataonly /samples:1'
 
  ; Run the command using the requested method 
  Select
   Case $_Method = 0                                            ; Use KixForms.DLL 
    $_oSys = CreateObject('Kixtart.System')
    $_aData = Split($_oSys.Shell($_Cmd, 0, 3), @CRLF)           ; Return StdOut 
    $_oSys = 0
   Case $_Method = -1                                           ; Use existing KF SYSTEM var 
    $_aData = Split($_oSys.Shell($_Cmd, 0, 3), @CRLF)           ; Return StdOut 
   Case $_Method = 1                                            ; Use WSH 
    $_oSys = CreateObject("WScript.Shell").Exec($_Cmd)
    If Not VarType($_oSys) = 9                                  ; Check for correct vartype returned 
      Exit 10
    EndIf
    $_I = 0
    While Not $_oSys.Status And $_I < 200                       ; wait up to 2 seconds for the command to complete 
      Sleep 0.01
      $_I = $_I + 1
    Loop
    $_aData = Split($_oSys.StdOut.ReadAll, @CRLF)               ; Return StdOut 
    $_oSys = 0
  EndSelect
 
  If Not @ERROR
    $_Data = SubStr(Split($_aData[0], Chr(10))[2], 21)          ; extract and format the response 
    $_Data = Replace($_Data, '/', ' ')
    $_Data = Replace($_Data, ':', ' ')
    $_aData = Split($_Data, ' ')
 
    ; Handle midnight (12 AM => 00:) 
    If InStr($_aData[6], 'AM') And Int($_aData[3]) = 12 $_aData[3] = 0 EndIf
    ; Handle PM hours > 12 
    If InStr($_aData[6], 'PM') And Int($_aData[3]) < 12
      $_aData[3] = 12 + $_aData[3]
    EndIf
 
    For $_I = 0 to 1
      $_aData[$_I] = Right('00' +  $_aData[$_I], 2)             ; format date leading zeros 
    Next
    For $_I = 3 to 5
      $_aData[$_I] = Right('00' +  $_aData[$_I], 2)             ; format time leading zeros 
    Next
 
    ; Build the response data 
    $_Data =  $_aData[2] + '/' + $_aData[0] + '/' + $_aData[1] + ' ' + $_aData[3] + ':' + $_aData[4] + ':' + $_aData[5]
    
    If $_Format                                                 ; UTC selected - adjust time and date 
      $_oWMIService = GetObject('winmgmts:\\.\root\cimv2')
      $_cItems = $_oWMIService.ExecQuery('Select * from Win32_ComputerSystem',,48)
      For Each $_oItem in $_cItems
        $_Offset = -60 * $_oItem.CurrentTimeZone		; minutes to move toward UTC 
      Next
      $GetTimeNtp = TimeConvert(TimeConvert($_Data) + $_Offset)
    Else                                                        ; return local time 
      $GetTimeNtp = $_Data
    EndIf
 
    Exit 0
  Else
    $GetTimeNtp = ''                                            ; error - return nothing 
    Exit 1
  EndIf
 
EndFunction