logo
vbRad Home
Source Code
Book Reviews
Forum
Links
About Us
Contribute

Compare Databases with SQL Effects Clarity
 
 How to get local time based on the GMT or GPS time

Posted on
2/18/2001
Author:
Robert Gelb
Email:
Not Shown
Applies To OS:
All
Product:
5, 6



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

Add Your Comment  

Name: Email Address: all fields optional
Notify me via email when someone responds to this message (valid email required).

Enter the word:
 



Comments
#1. By Gopal Aryal. Posted on 3/20/2007 7:30:41 PM
You have done a nice job.
Keep it on .

#2. By Safizan. Posted on 4/30/2007 1:23:48 PM
Hello... I'm from Malaysia.In Borneo.... Some of the topo maps are in GMT. How can I convert it to R.S.O so I can pin point a location that I need. E.g: I want to pin point my office from GPS (the unit is R.S.O) to Map coordinate(Cassini) or vise versa. Can anyone solve my problem?

#3. By Sudesh. Posted on 12/29/2007 8:02:05 AM
Sir,
Please send me Time comparison chart between India& all country in world specialy Ukraine

#4. By Sudesh. Posted on 12/29/2007 8:02:08 AM
Sir,
Please send me Time comparison chart between India& all country in world specialy Ukraine

#5. By Sudesh. Posted on 12/29/2007 8:02:16 AM
Sir,
Please send me Time comparison chart between India& all country in world specialy Ukraine