Home > How-To Library > Date & Time
How to get GMT time from local system time
**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * **************************************************************** Option Explicit Private Const TIME_ZONE_ID_DAYLIGHT As Long = 2 Private Const VER_PLATFORM_WIN32_NT = 2 Private Const HKEY_LOCAL_MACHINE = &H80000002 Private Const KEY_ENUMERATE_SUB_KEYS As Long = &H8 Private Const KEY_NOTIFY As Long = &H10 Private Const KEY_QUERY_VALUE As Long = &H1 Private Const REG_BINARY = 3 Private Const REG_DWORD As Long = 4 Private Const REG_SZ As Long = 1 Private Const STANDARD_RIGHTS_READ As Long = &H20000 Private Const SYNCHRONIZE As Long = &H100000 Private Const KEY_READ As Long = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or _ KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE)) Private Type OSVERSIONINFO OSVSize As Long dwVerMajor As Long dwVerMinor As Long dwBuildNumber As Long PlatformID As Long szCSDVersion As String * 128 End Type Private 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 Private Type REG_TIME_ZONE_INFORMATION Bias As Long StandardBias As Long DaylightBias As Long StandardDate As SYSTEMTIME DaylightDate As SYSTEMTIME End Type Private Type TIME_ZONE_INFORMATION Bias As Long StandardName(63) As Byte StandardDate As SYSTEMTIME StandardBias As Long DaylightName(63) As Byte DaylightDate As SYSTEMTIME DaylightBias As Long End Type Declare Sub GetLocalTime Lib "kernel32" (lpSystemTime As SYSTEMTIME) Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME) Declare Function GetTimeZoneInformation Lib "kernel32" _ (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _ (lpVersionInformation As OSVERSIONINFO) As Long Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _ (ByVal hKey As Long, ByVal lpsSubKey As String, ByVal ulOptions As Long, _ ByVal samDesired As Long, phkResult As Long) As Long Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _ (ByVal hKey As Long, ByVal lpszValueName As String, ByVal lpdwReserved As Long, _ lpdwType As Long, lpData As Any, lpcbData As Long) As Long Public Function IsDST() As Boolean Dim udtTimezoneInfo As TIME_ZONE_INFORMATION IsDST = (GetTimeZoneInformation(udtTimezoneInfo) = TIME_ZONE_ID_DAYLIGHT) End Function Public Function IsWinNT() As Boolean Dim udtVersion As OSVERSIONINFO ' initialize UDT udtVersion.OSVSize = Len(udtVersion) ' try get the data If GetVersionEx(udtVersion) <> 1 Then Exit Function ' check for information IsWinNT = (udtVersion.PlatformID = VER_PLATFORM_WIN32_NT) End Function Public Function NowGMT() As Date Dim udtSysTime As SYSTEMTIME, udtRegTZInfo As REG_TIME_ZONE_INFORMATION Dim lngKey As Long, strTimezone, lngBias As Long If IsWinNT Then strTimezone = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\GMT Standard Time" Else strTimezone = "SOFTWARE\Microsoft\Windows\CurrentVersion\Time Zones\GMT Standard Time" End If If RegOpenKeyEx(HKEY_LOCAL_MACHINE, strTimezone, 0, KEY_READ, lngKey) = 0 Then If RegQueryValueEx(lngKey, "TZI", 0&, ByVal 0&, udtRegTZInfo, Len(udtRegTZInfo)) = 0 Then If IsDST Then lngBias = udtRegTZInfo.DaylightBias Else lngBias = udtRegTZInfo.StandardBias End If End If RegCloseKey lngKey End If GetSystemTime udtSysTime With udtSysTime NowGMT = DateSerial(.wYear, .wMonth, .wDay) + _ TimeSerial(.wHour, .wMinute - lngBias, .wSecond) End With End Function
If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.