Results 1 to 2 of 2

Thread: Daylight Savings Time detection

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    74

    Daylight Savings Time detection

    I was working on an Excel file today in an attempt to automate most of it's requirements. One cell is required to have either PDT or PST depending upon what time of year it is. I could not find anything in VB 6 that tested the system clock to determine if the time of year was currently standard or daylight time so I wrote this:

    VB Code:
    1. Sub CheckPDT()
    2.  
    3.    'Holds the date for the start and end PDT
    4.     Dim PDTStart As String
    5.     Dim PDTEnd As String
    6.    
    7.     'These hold the values days, months, year, name of the day of the week, and the number of the day of the week
    8.     Dim MyDay As String
    9.     Dim MyMonth As String
    10.     Dim MyYear As String
    11.     Dim DayNum As String
    12.     Dim TestMonth As Integer
    13.     Dim TestDay As Integer
    14.    
    15.     'The date we are going to test
    16.     Dim MyDate As String
    17.    
    18.     'Hold the difference between 2 dates
    19.     Dim DiffApril As Integer
    20.     Dim DiffOct As Integer
    21.    
    22.     'Set the initial TestMonth and TestDay for April 1
    23.     TestMonth = 4
    24.     TestDay = 1
    25.    
    26.     'Get the first Sunday in April by cycling thru the days beinning from the 1st
    27.     Do Until MyDay = "Sunday"
    28.         MyDate = CStr(TestMonth) & "/" & CStr(TestDay) & "/" & Year(Date)
    29.         MyDate = DateValue(MyDate)
    30.         DayNum = Day(MyDate)
    31.         MyMonth = Month(MyDate)
    32.         MyMonth = MonthName(MyMonth, False)
    33.         MyDay = Weekday(MyDate)
    34.         MyDay = WeekdayName(MyDay, False, 1)
    35.         If MyDay = "Sunday" Then
    36.             PDTStart = MyMonth & " " & DayNum & ", " & Year(Date)
    37.             Exit Do
    38.         End If
    39.         TestDay = TestDay + 1
    40.     Loop
    41.    
    42.     'Reset the TestMonth, TestDay  to October 31 and MyDay to nothing
    43.     TestMonth = 10
    44.     TestDay = 31
    45.     MyDay = ""
    46.    
    47.     'Get the last Sunday in October by cycling backwards thru the days beginning with the 31st
    48.     Do Until MyDay = "Sunday"
    49.         MyDate = CStr(TestMonth) & "/" & CStr(TestDay) & "/" & Year(Date)
    50.         MyDate = DateValue(MyDate)
    51.         DayNum = Day(MyDate)
    52.         MyMonth = Month(MyDate)
    53.         MyMonth = MonthName(MyMonth, False)
    54.         MyDay = Weekday(MyDate)
    55.         MyDay = WeekdayName(MyDay, False, 1)
    56.         If MyDay = "Sunday" Then
    57.             PDTEnd = MyMonth & " " & DayNum & ", " & Year(Date)
    58.             Exit Do
    59.         End If
    60.         TestDay = TestDay - 1
    61.     Loop
    62.    
    63.     'Get the difference between today's date and the start and end of Daylight Savings Time
    64.     DiffApril = Val(DateDiff("d", PDTStart, Date))
    65.     DiffOct = Val(DateDiff("d", PDTEnd, Date))
    66.    
    67.     'Display the time
    68.     If (DiffApril > 0) And (DiffOct < 0) Then
    69.         MsgBox "The time is " & Time & " Pacific Daylight Time."
    70.     Else
    71.         MsgBox "The time is " & Time & " Pacific Standard Time."
    72.     End If
    73.  
    74. End Sub

  2. #2
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Daylight Savings Time detection

    Just found this trolling through the CodeBank and thought I'd post an API version of this in case anyone else needs it (much easier\faster).
    VB Code:
    1. Private Type TimeZoneInfo
    2.     Bias As Long
    3.     StandardName(63) As Byte
    4.     StandardDate(7) As Integer
    5.     StandardBias As Long
    6.     DaylightName(63) As Byte
    7.     DaylightDate(7) As Integer
    8.     DaylightBias As Long
    9. End Type
    10.  
    11. Private Const TIME_ZONE_ID_DAYLIGHT = 2
    12.  
    13. Private Declare Function GetTimeZoneInformation Lib "kernel32" (uTZ As TimeZoneInfo) As Long
    14.  
    15. Private Function IsDLSavings() As Boolean
    16.  
    17.     Dim uInfo As TimeZoneInfo, lReturn As Long
    18.    
    19.     lReturn = GetTimeZoneInformation(uInfo)
    20.  
    21.     If lReturn = TIME_ZONE_ID_DAYLIGHT Then
    22.         IsDLSavings = True
    23.     End If
    24.  
    25. End Function
    Last edited by Comintern; Nov 30th, 2006 at 10:43 PM. Reason: typo

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