|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Visual Basic Locale/Regionalization
Routines GetTimeZoneInformation: Determine when Daylight Saving Time Occurs |
||
Posted: | Tuesday March 27, 2001 | |
Updated: | Monday December 26, 2011 | |
Applies to: | VB4-32, VB5, VB6 | |
Developed with: | VB5, Windows 2000 | |
OS restrictions: | None | |
Author: | VBnet - Randy Birch, Chip Pearson, Bob Butler | |
Related: |
SetLocaleInfo: Change System Long and Short Date Formats WM_TIMECHANGE: Detect System Changes to the Date/Time RegQueryValueEx: Identify Time Zones by Time Zone Bias EnumDateFormats: Regional Locale Date Settings EnumTimeFormats: Regional Locale Time Settings GetLocaleInfo: Regional Locale Date Settings |
|
Prerequisites |
None. |
|
Windows
time-related functions return one of the five supported time formats (System, File, Local, MS-DOS and Windows), and include functions to
convert between time formats for comparison or display, or to convert formats based on the time zone setting. GetTimeZoneInformation
is the API used to retrieve the time-zone parameters for the system, and this API also controls the translation between Coordinated Universal
Time (UTC) and local machine time.
The return value from a call to GetTimeZoneInformation indicates whether the local system is running under Standard Time or Daylight Saving Time. Where the call can not determine the time zone type (for example if daylight saving time is not used in the current time zone, or because a call to SetTimeZoneInformation was made without specifying the transition dates), the call returns 0. This demo wraps GetTimeZoneInformation into two callable functions. One function can return the string representing the time zone type currently in force (daylight, standard or unknown), while the second call simply returns True if the system time is Daylight Saving Time (False if either Standard Time or unknown). This code is based on newsgroup postings by Bob Butler and Excel MVP Chip Pearson. |
BAS Module Code |
None. |
|
Form Code |
Add a label (Label1) for the current date, a command button (Command1) and two text boxes (Text1 - Text2) to a form. Other labels are optional. Add the following code: |
|
Option Explicit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved. ' Some pages may also contain other copyrights by the author. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Distribution: You can freely use this code in your own ' applications, but you may not reproduce ' or publish this code on any web site, ' online service, or distribute as source ' on any media without express permission. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Const TIME_ZONE_ID_UNKNOWN As Long = 1 Private Const TIME_ZONE_ID_STANDARD As Long = 1 Private Const TIME_ZONE_ID_DAYLIGHT As Long = 2 Private Const TIME_ZONE_ID_INVALID As Long = &HFFFFFFFF 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(0 To 63) As Byte 'Unicode StandardDate As SYSTEMTIME StandardBias As Long DaylightName(0 To 63) As Byte 'Unicode DaylightDate As SYSTEMTIME DaylightBias As Long End Type Private Declare Function GetTimeZoneInformation Lib "kernel32" _ (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long Private Sub Command1_Click() Label1.Caption = Format$(Now, "long date") Text1.Text = GetCurrentTimeZone() Text2.Text = Format$(IsDaylightSavingTime(), "Yes/No") End Sub Private Function IsDaylightSavingTime() As Boolean Dim tzi As TIME_ZONE_INFORMATION IsDaylightSavingTime = GetTimeZoneInformation(tzi) = TIME_ZONE_ID_DAYLIGHT End Function Private Function GetCurrentTimeZone() As String Dim tzi As TIME_ZONE_INFORMATION Dim tmp As String Select Case GetTimeZoneInformation(tzi) Case 0: tmp = "Cannot determine current time zone" Case 1: tmp = tzi.StandardName Case 2: tmp = tzi.DaylightName End Select GetCurrentTimeZone = TrimNull(tmp) End Function Private Function TrimNull(item As String) Dim pos As Integer 'double check that there is a chr$(0) in the string pos = InStr(item, Chr$(0)) If pos Then TrimNull = Left$(item, pos - 1) Else TrimNull = item End If End Function |
Comments |
|
|
|
|||||
|
|||||
|
|||||
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |