CodeItBetter Programming Another VB Programming Blog

How to get GMT time from local system time

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'Date & Time - How to get GMT time from local system time
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
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.