ID do artigo: 221542 - Última revisão: segunda-feira, 8 de novembro de 2004 - Revisão: 3.1

Como alterar as informações de fuso horário utilizando o Visual Basic

Dica do SistemaEste artigo aplica-se a um sistema operativo diferente do que está a utilizar. Foi desactivado o conteúdo do artigo, que pode não ser relevante para si.

Nesta página

Expandir tudo | Recolher tudo

Sumário

Às vezes, é necessário alterar programaticamente o fuso horário para o sistema onde o aplicativo está sendo executado. Este artigo demonstra essa técnica.

Mais Informações

A maneira de implementar esse efeito é da seguinte maneira:
  1. Determine qual fuso horário você deseja alterar para.
  2. Localizar a chave no registro que contém as informações necessárias para preencher a estrutura TIME_ZONE_INFORMATION.
  3. Ler essas informações e carregue os valores em uma variável do tipo TIME_ZONE_INFORMATION.
  4. Chame a API SetTimeZoneInformation, passando a variável de estrutura TIME_ZONE_INFORMATION.
No registro que contém as informações de fuso horário local difere entre o Windows 9 x e Windows NT/Windows 2000.

Para o Windows NT e Windows 2000, está localizado em:
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones
Para Windows 9 x, ele está localizado em:
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Time zonas
Os nomes de chaves de fuso horário diferem entre Windows NT, Windows 2000 e Windows 9 x bem porque o Windows NT e Windows 2000 acrescentar a seqüência de caracteres "Hora padrão" para as chaves diferentes enquanto o Windows 9 x não.

Por exemplo:

No Windows NT e Windows 2000, você deverá ver esta chave:
Hora oficial do Pacífico
Embora no Windows 9 x, você verá essa chave:
Pacífico
O local do registro onde as configurações de data/hora atuais podem ser encontradas é:
HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation

Exemplo passo a passo

O exemplo de código abaixo requer um formulário com uma caixa de listagem. A caixa de listagem é carregada com os valores possíveis fuso horário que o usuário pode selecionar para alterar o fuso horário com um clique duplo. Ele mostrará uma messagebox que, uma vez descartado, será alterado as configurações do sistema volta para seus valores originais.
  1. Crie um novo projeto Standard EXE. O Form1 é criado por padrão.
  2. Adicione um controle ListBox ao Form1.
  3. Adicione o seguinte código à seção General Declarations do Form1:
    Option Explicit
    
    ' Operating System version information declares
    
    Private Const VER_PLATFORM_WIN32_NT = 2
    Private Const VER_PLATFORM_WIN32_WINDOWS = 1
    
    Private Type OSVERSIONINFO
            dwOSVersionInfoSize As Long
            dwMajorVersion As Long
            dwMinorVersion As Long
            dwBuildNumber As Long
            dwPlatformId As Long
            szCSDVersion As String * 128 ' Maintenance string
    End Type
    
    Private Declare Function GetVersionEx Lib "kernel32" _
    Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
    
    ' Time Zone information declares
    
    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 REGTIMEZONEINFORMATION
       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(0 To 63) As Byte ' used to accommodate Unicode strings
       StandardDate As SYSTEMTIME
       StandardBias As Long
       DaylightName(0 To 63) As Byte ' used to accommodate Unicode strings
       DaylightDate As SYSTEMTIME
       DaylightBias As Long
    End Type
    
    Private Const TIME_ZONE_ID_INVALID = &HFFFFFFFF
    Private Const TIME_ZONE_ID_UNKNOWN = 0
    Private Const TIME_ZONE_ID_STANDARD = 1
    Private Const TIME_ZONE_ID_DAYLIGHT = 2
    
    Private Declare Function GetTimeZoneInformation Lib "kernel32" _
    (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long
    
    Private Declare Function SetTimeZoneInformation Lib "kernel32" _
    (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long
    
    ' Registry information declares
    Private Const REG_SZ As Long = 1
    Private Const REG_BINARY = 3
    Private Const REG_DWORD As Long = 4
    
    Private Const HKEY_CLASSES_ROOT = &H80000000
    Private Const HKEY_CURRENT_USER = &H80000001
    Private Const HKEY_LOCAL_MACHINE = &H80000002
    Private Const HKEY_USERS = &H80000003
    
    Private Const ERROR_SUCCESS = 0
    Private Const ERROR_BADDB = 1
    Private Const ERROR_BADKEY = 2
    Private Const ERROR_CANTOPEN = 3
    Private Const ERROR_CANTREAD = 4
    Private Const ERROR_CANTWRITE = 5
    Private Const ERROR_OUTOFMEMORY = 6
    Private Const ERROR_ARENA_TRASHED = 7
    Private Const ERROR_ACCESS_DENIED = 8
    Private Const ERROR_INVALID_PARAMETERS = 87
    Private Const ERROR_NO_MORE_ITEMS = 259
    
    Private Const KEY_ALL_ACCESS = &H3F
    
    Private Const REG_OPTION_NON_VOLATILE = 0
    
    Private Declare Function RegOpenKeyEx Lib "advapi32.dll" _
       Alias "RegOpenKeyExA" ( _
       ByVal hKey As Long, _
       ByVal lpSubKey As String, _
       ByVal ulOptions As Long, _
       ByVal samDesired As Long, _
       phkResult As Long) _
    As Long
    
    Private 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
    
    Private Declare Function RegQueryValueExString Lib "advapi32.dll" _
       Alias "RegQueryValueExA" ( _
       ByVal hKey As Long, _
       ByVal lpValueName As String, _
       ByVal lpReserved As Long, _
       lpType As Long, _
       ByVal lpData As String, _
       lpcbData As Long) _
    As Long
    
    Private Declare Function RegEnumKey Lib "advapi32.dll" _
       Alias "RegEnumKeyA" ( _
       ByVal hKey As Long, _
       ByVal dwIndex As Long, _
       ByVal lpName As String, _
       ByVal cbName As Long) _
    As Long
    
    Private Declare Function RegCloseKey Lib "advapi32.dll" ( _
       ByVal hKey As Long) _
    As Long
    
    ' Registry Constants
    Const SKEY_NT = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones"
    Const SKEY_9X = "SOFTWARE\Microsoft\Windows\CurrentVersion\Time Zones"
    
    ' The following declaration is different from the one in the API viewer.
    ' To disable implicit ANSI<->Unicode conversion, it changes the 
    ' variable types of lpMultiByteStr and lpWideCharStr to Any.
    '
    Private Declare Function MultiByteToWideChar Lib "kernel32" ( _
        ByVal CodePage As Long, _
        ByVal dwFlags As Long, _
        lpMultiByteStr As Any, _
        ByVal cchMultiByte As Long, _
        lpWideCharStr As Any, _
        ByVal cchWideChar As Long) As Long
        
    ' The above Declare and the following Constants are used to make
    ' this sample compatible with Double Byte Character Systems (DBCS).
    Private Const CP_ACP = 0
    Private Const MB_PRECOMPOSED = &H1
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _ 
    ByRef Destination As Any, _
    ByRef Source As Any, _
    ByVal numbytes As Long)
    Dim SubKey As String
    
    Private Sub Form_Load()
       Dim lRetVal As Long, lResult As Long, lCurIdx As Long
       Dim lDataLen As Long, lValueLen As Long, hKeyResult As Long
       Dim strvalue As String
       Dim osV As OSVERSIONINFO
    
    ' Win9x and WinNT have a slightly different registry structure. Determine
    ' the operating system and set a module variable to the correct subkey.
    
       osV.dwOSVersionInfoSize = Len(osV)
       Call GetVersionEx(osV)
       If osV.dwPlatformId = VER_PLATFORM_WIN32_NT Then
          SubKey = SKEY_NT
       Else
          SubKey = SKEY_9X
       End If
    
       lRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, SubKey, 0, _
                              KEY_ALL_ACCESS, hKeyResult)
    
       If lRetVal = ERROR_SUCCESS Then
    
          lCurIdx = 0
          lDataLen = 32
          lValueLen = 32
    
          Do
             strvalue = String(lValueLen, 0)
             lResult = RegEnumKey(hKeyResult, lCurIdx, strvalue, lDataLen)
    
             If lResult = ERROR_SUCCESS Then
                List1.AddItem Left(strvalue, lValueLen)
             End If
    
             lCurIdx = lCurIdx + 1
    
          Loop While lResult = ERROR_SUCCESS
    
          RegCloseKey hKeyResult
       Else
          List1.AddItem "Could not open registry key"
       End If
    End Sub
    
    Private Sub List1_DblClick()
       Dim TZ As TIME_ZONE_INFORMATION, oldTZ As TIME_ZONE_INFORMATION
       Dim rTZI As REGTIMEZONEINFORMATION
       Dim bytDLTName(32) As Byte, bytSTDName(32) As Byte
       Dim cbStr As Long, dwType As Long
       Dim lRetVal As Long, hKeyResult As Long, lngData As Long
    
       lRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, SubKey & "\" & List1.Text, _
                               0, KEY_ALL_ACCESS, hKeyResult)
    
       If lRetVal = ERROR_SUCCESS Then
          lRetVal = RegQueryValueEx(hKeyResult, "TZI", 0&, ByVal 0&, _
                                      rTZI, Len(rTZI))
    
          If lRetVal = ERROR_SUCCESS Then
             TZ.Bias = rTZI.Bias
             TZ.StandardBias = rTZI.StandardBias
             TZ.DaylightBias = rTZI.DaylightBias
             TZ.StandardDate = rTZI.StandardDate
             TZ.DaylightDate = rTZI.DaylightDate
    
             cbStr = 32
             dwType = REG_SZ
    
             lRetVal = RegQueryValueEx(hKeyResult, "Std", _
                         0&, dwType, bytSTDName(0), cbStr)
    
             If lRetVal = ERROR_SUCCESS Then
                Call MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, _
                   bytSTDName(0), cbStr, TZ.StandardName(0), 32)
             Else
                RegCloseKey hKeyResult
                Exit Sub
             End If
    
             cbStr = 32
             dwType = REG_SZ
    
             lRetVal = RegQueryValueEx(hKeyResult, "Dlt", _
                         0&, dwType, bytDLTName(0), cbStr)
    
             If lRetVal = ERROR_SUCCESS Then
                Call MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, _
                   bytDLTName(0), cbStr, TZ.DaylightName(0), 32)
             Else
                RegCloseKey hKeyResult
                Exit Sub
             End If
    
             lRetVal = GetTimeZoneInformation(oldTZ)
    
             If lRetVal = TIME_ZONE_ID_INVALID Then
                MsgBox "Error getting original TimeZone Info"
                RegCloseKey hKeyResult
                Exit Sub
             Else
             If TZ.DaylightDate.wMonth <> 0 And TZ.DaylightBias <> 0 Then
                lRetVal = SetTimeZoneInformation(TZ)
             Else
               Call CopyMemory(TZ.DaylightName(0), TZ.StandardName(0), 64)
               TZ.DaylightBias = 0
               lRetVal = SetTimeZoneInformation(TZ)
             End If
                MsgBox "Time Zone Changed, Click OK to restore"
                lRetVal = SetTimeZoneInformation(oldTZ)
             End If
          End If
    
          RegCloseKey hKeyResult
       End If
    End Sub
    					
  4. Execute o programa e observe que a caixa de listagem exibe todos os fusos horários disponíveis.
  5. Selecione um fuso horário e, em seguida, clique duas vezes nele. Sua localidade de sistema é alterada para a zona selecionada. Você receber a seguinte mensagem:
    Alteração de fuso horário, clique em OK para restaurar
  6. No painel de controle, clique duas vezes no ícone Data/hora . Você também pode acessar esta caixa de diálogo clicando duas vezes no relógio na bandeja do sistema. Clique na guia fuso horário e observe que o fuso horário foi alterado.
  7. Fechar a caixa de diálogo Propriedades de data/hora e clique em OK na caixa de mensagem exibida pelo seu aplicativo. Repita a etapa anterior para confirmar que as informações de fuso horário foi restauradas.

A informação contida neste artigo aplica-se a:
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
Palavras-chave: 
kbmt kbapi kbhowto KB221542 KbMtpt
Tradução automáticaTradução automática
IMPORTANTE: Este artigo foi traduzido por um sistema de tradução automática (também designado por Machine Translation ou MT), não tendo sido portanto traduzido ou revisto por pessoas. A Microsoft possui artigos traduzidos por aplicações (MT) e artigos traduzidos por tradutores profissionais, com o objetivo de oferecer em português a totalidade dos artigos existentes na base de dados de suporte. No entanto, a tradução automática não é sempre perfeita, podendo conter erros de vocabulário, sintaxe ou gramática. A Microsoft não é responsável por incoerências, erros ou prejuízos ocorridos em decorrência da utilização dos artigos MT por parte dos nossos clientes. A Microsoft realiza atualizações freqüentes ao software de tradução automática (MT). Obrigado.
Clique aqui para ver a versão em Inglês deste artigo: 221542  (http://support.microsoft.com/kb/221542/en-us/ )