Results 1 to 7 of 7

Thread: Is DST Flag from the Windows OS available via API?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    130

    Is DST Flag from the Windows OS available via API?

    I know there are several examples of figuring DST out there but they all seem to be geographic centric. That is to say, they're hard coded for the US or EU or whatever.

    What I was hoping for is, that since the Win OS itself takes care of timezone determination and DST flagging, if there was an API to grab within VB6, that would be a super simple way to determine DST status.

    So, is there a way?

  2. #2
    Fanatic Member
    Join Date
    Feb 2019
    Posts
    924

    Re: Is DST Flag from the Windows OS available via API?

    See GetTimeZoneInformation.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    130

    Re: Is DST Flag from the Windows OS available via API?

    That only returns the selected Timezone, not the DST status for it, correct?

  4. #4
    Member dseaman's Avatar
    Join Date
    Oct 2004
    Location
    Natal, Brazil
    Posts
    38

    Re: Is DST Flag from the Windows OS available via API?

    Code:
    Option Explicit
    
    Private Type TIME_ZONE_INFORMATION
       Bias                 As Long
       StandardName(0 To 31) As Integer
       StandardDate(0 To 7) As Integer
       StandardBias         As Long
       DaylightName(0 To 31) As Integer
       DaylightDate(0 To 7) As Integer
       DaylightBias         As Long
    End Type
    
    Private Declare Function GetTimeZoneInformation Lib "kernel32" (ByRef lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long
    
    Function IsDayLight() As Boolean
       Const TIME_ZONE_ID_DAYLIGHT As Long = 2
       Dim tz As TIME_ZONE_INFORMATION
       IsDayLight = GetTimeZoneInformation(tz) = TIME_ZONE_ID_DAYLIGHT
    End Function
    
    Private Sub Form_Load()
      Debug.Print "IsDayLight", IsDayLight
    End Sub

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    130

    Re: Is DST Flag from the Windows OS available via API?

    Thanks, exactly what I was looking for

  6. #6
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,480

    Re: Is DST Flag from the Windows OS available via API?

    On any current version of Windows you could call GetTimeZoneInformationForYear(). If you are running on an unsupported OS earlier than Vista SP1 then use the obsolete GetTimeZoneInformation() but the results are less accurate because the transition date-times move from year to year these days.

    Code:
    Option Explicit
    
    Private Const WIN32_NULL As Long = 0
    
    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 As String * 32
        StandardDate As SystemTime
        StandardBias As Long
        DaylightName As String * 32
        DaylightDate As SystemTime
        DaylightBias As Long
    End Type
    
    Private Enum TZI_RESULT
        TIME_ZONE_ID_INVALID = -1
        TIME_ZONE_ID_UNKNOWN = 0
        TIME_ZONE_ID_STANDARD = 1
        TIME_ZONE_ID_DAYLIGHT = 2
    End Enum
    '
    'Private Declare Function GetTimeZoneInformation Lib "kernel32" ( _
    '    ByVal lpTIME_ZONE_INFORMATION As Long) As TZI_RESULT
    
    Private Declare Function GetTimeZoneInformationForYear Lib "kernel32" ( _
        ByVal wYear As Integer, _
        ByVal pdtzi As Long, _
        ByVal lpTIME_ZONE_INFORMATION As Long) As TZI_RESULT
    
    Private Declare Function SystemTimeToVariantTime Lib "oleaut32" ( _
        ByRef SystemTime As SystemTime, _
        ByRef VariantTime As Date) As Long
    
    Private Sub Log(ByVal Text As String)
        With Text1
            .SelStart = &H7FFF 'At end.
            .SelText = Text
        End With
    End Sub
    
    Private Sub LogLine(Optional ByVal Text As String)
        With Text1
            .SelStart = &H7FFF 'At end.
            .SelText = Text
            .SelText = vbNewLine
        End With
    End Sub
    
    Private Sub Form_Load()
        Dim TZI_RESULT As TZI_RESULT
        Dim TIME_ZONE_INFORMATION As TIME_ZONE_INFORMATION
        Dim NULPos As Long
        Dim DateValue As Date
    
        'Requires Windows Vista SP1 or later:
        TZI_RESULT = GetTimeZoneInformationForYear(Year(Date), _
                                                   WIN32_NULL, _
                                                   VarPtr(TIME_ZONE_INFORMATION))
        'Fallback for old OSs (e.g. Windows XP):
        'TZI_RESULT = GetTimeZoneInformation(VarPtr(TIME_ZONE_INFORMATION))
    
        With TIME_ZONE_INFORMATION
            Log Choose(TZI_RESULT + 2, _
                       "TIME_ZONE_ID_INVALID", _
                       "TIME_ZONE_ID_UNKNOWN", _
                       "TIME_ZONE_ID_STANDARD", _
                       "TIME_ZONE_ID_DAYLIGHT")
            If TZI_RESULT = TIME_ZONE_ID_INVALID Then
                Log " error "
                LogLine CStr(Err.LastDllError)
            Else
                LogLine
                LogLine
    
                Log "Bias = "
                LogLine CStr(.Bias)
    
                NULPos = InStr(.StandardName, vbNullChar)
                Log "Standard name = "
                If NULPos = 0 Then
                    LogLine .StandardName
                Else
                    LogLine Left$(.StandardName, NULPos - 1)
                End If
    
                .StandardDate.wYear = Year(Date)
                SystemTimeToVariantTime .StandardDate, DateValue
                Log "Standard date = "
                LogLine Format$(DateValue, "General Date")
    
                Log "Standard bias = "
                LogLine CStr(.StandardBias)
    
                NULPos = InStr(.DaylightName, vbNullChar)
                Log "Daylight name = "
                If NULPos = 0 Then
                    LogLine .DaylightName
                Else
                    LogLine Left$(.DaylightName, NULPos - 1)
                End If
    
                .DaylightDate.wYear = Year(Date)
                SystemTimeToVariantTime .DaylightDate, DateValue
                Log "Daylight date = "
                LogLine Format$(DateValue, "General Date")
    
                Log "Daylight bias = "
                LogLine CStr(.DaylightBias)
            End If
        End With
    End Sub
    
    Private Sub Form_Resize()
        If WindowState <> vbMinimized Then
            Text1.Move 0, 0, ScaleWidth, ScaleHeight
        End If
    End Sub
    Name:  sshot.png
Views: 264
Size:  2.8 KB

    Because many time zones move into and out of DST partway into a given date it may be sort of pointless. It really depends on what you are trying to do.

    If your goal is something like calculating GMT from a local date-time you are far better off making API calls that do that, such as TzSpecificLocalTimeToSystemTime() that takes everything into account.
    Last edited by dilettante; Aug 2nd, 2019 at 03:35 PM. Reason: edited code to handle cases where no NUL is encountered

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    130

    Re: Is DST Flag from the Windows OS available via API?

    This will be used on Win 7 and later.
    I want my code to use the user's timezone and date/time settings of their own computer to determine DST (or not DST)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width