|How to get local time based on the GMT or GPS time||
|NT, 9x, 2000
With the proliferation of GPS devices, many developers would like to integrate their functionality into their applications. One of the hurdles to overcome is the time. The GPS unit normally reports the time in the GMT time zone, leaving the developer to have to convert it to the local time. The conversion must take into account the time zone difference between GMT and your local time zone, plus it must account for daylight savings time changes. Two functions are provided: GetTimeDifference and GetTimeHere .
Function GetTimeDifference provides the difference in seconds between local & GMT time. If the result is negative, that means your time zone is lagging behind GMT (US, Canada, South America). Positive result indicates that your time zone is ahead of GMT (Russia, Ukraine, China, India).
Function GetTimeHere takes GMT time as parameter and returns the time & date here. What is it good for then, I hear you asking. Well, when going over the GPS updates and wanting to know when exactly at your time they happened, this function will come in pretty handy.
Note, do not ever copy the UDTs below from the API Viewer - it contains an error. Copy them from here.
|Add the following to a .BAS, .FRM or .CLS|
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 TIME_ZONE_INFORMATION Bias As Long StandardName(31) As Integer StandardDate As SYSTEMTIME StandardBias As Long DaylightName(31) As Integer DaylightDate As SYSTEMTIME DaylightBias As Long End Type Private Declare Function GetTimeZoneInformation Lib "kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long Private Const TIME_ZONE_ID_INVALID& = &HFFFFFFFF Private Const TIME_ZONE_ID_STANDARD& = 1 Private Const TIME_ZONE_ID_UNKNOWN& = 0 Private Const TIME_ZONE_ID_DAYLIGHT& = 2 Public Function GetGmtTime(Optional StartingDate As Variant) As Date 'Parameters: StartingDate (Optional). The function will figure 'out GMT time based on StartingDate 'If StartingDate is not provided, the current time will be used Dim Difference As Long Difference = GetTimeDifference() If IsMissing(StartingDate) Then 'use current time GetGmtTime = DateAdd("s", -Difference, Now) Else 'use StartingDate GetGmtTime = DateAdd("s", -Difference, StartingDate) End If End Function Public Function GetTimeDifference() As Long 'Returns the time difference between 'local & GMT time in seconds. 'If the result is negative, your time zone 'lags behind GMT zone. 'If the result is positive, your time zone is ahead. Dim tz As TIME_ZONE_INFORMATION Dim retcode As Long Dim Difference As Long 'retrieve the time zone information retcode = GetTimeZoneInformation(tz) 'convert to seconds Difference = -tz.Bias * 60 'cache the result GetTimeDifference = Difference 'if we are in daylight saving time, apply the bias. If retcode = TIME_ZONE_ID_DAYLIGHT& Then If tz.DaylightDate.wMonth <> 0 Then 'if tz.DaylightDate.wMonth = 0 then the daylight 'saving time change doesn't occur GetTimeDifference = Difference - tz.DaylightBias * 60 End If End If End Function Public Function GetTimeHere(gmtTime As Date) As Date 'Parameters: gmtTime - Provides the time & date 'from which to make calculations 'Returns the time in your local time zone 'which corresposponds to GMT time Dim Differerence As Long Differerence = GetTimeDifference() GetTimeHere = DateAdd("s", Differerence, gmtTime) End Function