Get Windows NT Server Time

By | 2002-06-01

Returns the time of day from a Windows NT workstation or server. Accounts for time zones. Requires Windows NT.

Original Author: Matthew Ruffell

Inputs

ServerName [string] = name of target server.

Assumptions

Requires Windows NT.

Returns

Return the time of day.

Side Effects

Noen.

API Declarations

Private Type TIME_OF_DAY
t_elapsedt As Long
t_msecs As Long
t_hours As Long
t_mins As Long
t_secs As Long
t_hunds As Long
t_timezone As Long
t_tinterval As Long
t_day As Long
t_month As Long
t_year As Long
t_weekday As Long
End Type
Private Declare Function NetRemoteTOD Lib “netapi32.dll” (ByVal server As String, buffer As Any) As Long
Private Declare Sub CopyMem Lib “kernel32” Alias “RtlMoveMemory” (pTo As Any, uFrom As Any, ByVal lSize As Long)
Private Declare Function NetApiBufferFree Lib “netapi32.dll” (ByVal Ptr As Long) As Long

Code

Public Function Get_ServerTime(ByVal strServerName As String) As String
  
  Dim lngBuffer As Long
  Dim strServer As String
  Dim lngNet32ApiReturnCode As Long
  Dim days As Date
  Dim TOD As TIME_OF_DAY
  
  On Error Resume Next
  
  '// Get server time
  strServer = StrConv(strServerName, vbUnicode) '// Convert the server name to unicode
  lngNet32ApiReturnCode = NetRemoteTOD(strServer, lngBuffer)
  If lngNet32ApiReturnCode = 0 Then
    CopyMem TOD, ByVal lngBuffer, Len(TOD)
    days = DateSerial(70, 1, 1) + (TOD.t_elapsedt / 60 / 60 / 24) '// Convert the elapsed time since 1/1/70 to a date
    days = days - (TOD.t_timezone / 60 / 24) '// Adjust for TimeZone differences
    Get_ServerTime = days
  End If
  
  '// Free pointers from memory
  Call NetApiBufferFree(lngBuffer)
End Function

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.