Skip to content

Instantly share code, notes, and snippets.

@swbuehler
Last active September 9, 2016 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swbuehler/acb90c53745c967a73c4470fc3767c0a to your computer and use it in GitHub Desktop.
Save swbuehler/acb90c53745c967a73c4470fc3767c0a to your computer and use it in GitHub Desktop.
Set of types and functions that use the System time to return a value of given date/time in UTC. This is the 64-bit version; to convert to 32-bit search for and remove any occurence of "PtrSafe". I found this on the Internet written by someone else (forget who) but include it here for ease of use.
Public Declare PtrSafe Function SystemTimeToFileTime Lib _
"kernel32" (lpSystemTime As SYSTEMTIME, _
lpFileTime As FILETIME) As Long
Public Declare PtrSafe Function LocalFileTimeToFileTime Lib _
"kernel32" (lpLocalFileTime As FILETIME, _
lpFileTime As FILETIME) As Long
Public Declare PtrSafe Function FileTimeToSystemTime Lib _
"kernel32" (lpFileTime As FILETIME, lpSystemTime _
As SYSTEMTIME) As Long
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Public Function LocalTimeToUTC(ByVal dteTime As Date) As Date
Dim dteLocalFileTime As FILETIME
Dim dteFileTime As FILETIME
Dim dteLocalSystemTime As SYSTEMTIME
Dim dteSystemTime As SYSTEMTIME
dteLocalSystemTime.wYear = CInt(Year(dteTime))
dteLocalSystemTime.wMonth = CInt(Month(dteTime))
dteLocalSystemTime.wDay = CInt(Day(dteTime))
dteLocalSystemTime.wHour = CInt(Hour(dteTime))
dteLocalSystemTime.wMinute = CInt(Minute(dteTime))
dteLocalSystemTime.wSecond = CInt(Second(dteTime))
Call SystemTimeToFileTime(dteLocalSystemTime, _
dteLocalFileTime)
Call LocalFileTimeToFileTime(dteLocalFileTime, _
dteFileTime)
Call FileTimeToSystemTime(dteFileTime, dteSystemTime)
LocalTimeToUTC = CDate(dteSystemTime.wMonth & "/" & _
dteSystemTime.wDay & "/" & _
dteSystemTime.wYear & " " & _
dteSystemTime.wHour & ":" & _
dteSystemTime.wMinute & ":" & _
dteSystemTime.wSecond)
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment