Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

How to find Time Zone information on current system ?
[ All Languages » VB »  Windows]

Total Hit ( 3282)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


The following code will extract the timezone information from your system. It includes three functions. One returns the current date/time in UTC (or ZULU or GMT) time, the next returns the offset of your time zone from GMT in minutes (divide by 60 to get hours), and the last returns the name of the time zone. This last function uses some WideChar to Ascii conversion functions that are only available on NT or 9x systems with the Microsoft Layer for Unicode on Windows 95/98/Me Systems installed, so be careful with this one.

Click here to copy the following block
Option Explicit

Private Const CP_ACP = 0

Private Declare Function GetTimeZoneInformation Lib _
    "kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long
Private Declare Function lstrlenW Lib "kernel32" (lpString As Any) As Long
Private Declare Function WideCharToMultiByte Lib "kernel32" _
    (ByVal codepage As Long, ByVal dwFlags As Long, _
    lpWideCharStr As Any, ByVal cchWideChar As Long, _
    lpMultiByteStr As Any, ByVal cchMultiByte As Long, _
    ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long

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(32) As Integer
  StandardDate As SYSTEMTIME
  StandardBias As Long
  DaylightName(32) As Integer
  DaylightDate As SYSTEMTIME
  DaylightBias As Long
End Type

Private Sub Form_Load()
  MsgBox "UTC Time :" & GetUTCTime
  MsgBox "TimeZone Offset" & GetTimeZoneOffset
  MsgBox "TimeZone Name" & GetTimeZoneName
End Sub

Private Function GetUTCTime() As Date
  Dim tz As TIME_ZONE_INFORMATION
  Dim lRV As Long
  Dim dRV As Date

  lRV = GetTimeZoneInformation(tz)

  dRV = DateAdd("n", CDbl(tz.Bias), Now)

  GetUTCTime = dRV
End Function

Private Function GetTimeZoneOffset() As Integer
  Dim tz As TIME_ZONE_INFORMATION
  Dim lRV As Long
  Dim iRV As Integer

  lRV = GetTimeZoneInformation(tz)

  ' offset is indicated in minutes
  iRV = tz.Bias

  GetTimeZoneOffset = iRV
End Function

Private Function GetTimeZoneName() As String
  Dim tz As TIME_ZONE_INFORMATION
  Dim lRV As Long
  Dim sRV As String
  Dim lCounter As Long

  Dim wchar(1) As Byte

  lRV = GetTimeZoneInformation(tz)

  ' the TZ Name is stored in Wide Chars, we will extract by just grabbing first byte of each digit and mapping
  ' to ASCII. Should be safe for most of the time.

  sRV = GetStrFromPtrW(VarPtr(tz.StandardName(0)))

  GetTimeZoneName = sRV
End Function

' Returns the string up to the first Null Char
Public Function GetStrFromANSIBuffer(sBuf As String) As String
  If InStr(sBuf, &H0) Then
    GetStrFromANSIBuffer = Left(sBuf, InStr(sBuf, &H0) - 1)
  Else
    GetStrFromANSIBuffer = sBuf            ' no null, so don't worry about it
  End If
End Function

' Return ANSI string from a pointer to a Unicode string.
Public Function GetStrFromPtrW(lpszW As Long) As String
  Dim sRV As String

  sRV = String$(lstrlenW(ByVal lpszW) * 2, &H0)     ' 2 bytes/char

  ' copy from Unicode string into new buffer
  WideCharToMultiByte CP_ACP, &H0, ByVal lpszW, -1, ByVal sRV, Len(sRV), &H0, &H0
  GetStrFromPtrW = GetStrFromANSIBuffer(sRV)
End Function


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.