|
-
Jul 20th, 2005, 03:32 PM
#1
Thread Starter
Lively Member
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:
Sub CheckPDT()
'Holds the date for the start and end PDT
Dim PDTStart As String
Dim PDTEnd As String
'These hold the values days, months, year, name of the day of the week, and the number of the day of the week
Dim MyDay As String
Dim MyMonth As String
Dim MyYear As String
Dim DayNum As String
Dim TestMonth As Integer
Dim TestDay As Integer
'The date we are going to test
Dim MyDate As String
'Hold the difference between 2 dates
Dim DiffApril As Integer
Dim DiffOct As Integer
'Set the initial TestMonth and TestDay for April 1
TestMonth = 4
TestDay = 1
'Get the first Sunday in April by cycling thru the days beinning from the 1st
Do Until MyDay = "Sunday"
MyDate = CStr(TestMonth) & "/" & CStr(TestDay) & "/" & Year(Date)
MyDate = DateValue(MyDate)
DayNum = Day(MyDate)
MyMonth = Month(MyDate)
MyMonth = MonthName(MyMonth, False)
MyDay = Weekday(MyDate)
MyDay = WeekdayName(MyDay, False, 1)
If MyDay = "Sunday" Then
PDTStart = MyMonth & " " & DayNum & ", " & Year(Date)
Exit Do
End If
TestDay = TestDay + 1
Loop
'Reset the TestMonth, TestDay to October 31 and MyDay to nothing
TestMonth = 10
TestDay = 31
MyDay = ""
'Get the last Sunday in October by cycling backwards thru the days beginning with the 31st
Do Until MyDay = "Sunday"
MyDate = CStr(TestMonth) & "/" & CStr(TestDay) & "/" & Year(Date)
MyDate = DateValue(MyDate)
DayNum = Day(MyDate)
MyMonth = Month(MyDate)
MyMonth = MonthName(MyMonth, False)
MyDay = Weekday(MyDate)
MyDay = WeekdayName(MyDay, False, 1)
If MyDay = "Sunday" Then
PDTEnd = MyMonth & " " & DayNum & ", " & Year(Date)
Exit Do
End If
TestDay = TestDay - 1
Loop
'Get the difference between today's date and the start and end of Daylight Savings Time
DiffApril = Val(DateDiff("d", PDTStart, Date))
DiffOct = Val(DateDiff("d", PDTEnd, Date))
'Display the time
If (DiffApril > 0) And (DiffOct < 0) Then
MsgBox "The time is " & Time & " Pacific Daylight Time."
Else
MsgBox "The time is " & Time & " Pacific Standard Time."
End If
End Sub
-
Nov 30th, 2006, 10:42 PM
#2
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:
Private Type TimeZoneInfo
Bias As Long
StandardName(63) As Byte
StandardDate(7) As Integer
StandardBias As Long
DaylightName(63) As Byte
DaylightDate(7) As Integer
DaylightBias As Long
End Type
Private Const TIME_ZONE_ID_DAYLIGHT = 2
Private Declare Function GetTimeZoneInformation Lib "kernel32" (uTZ As TimeZoneInfo) As Long
Private Function IsDLSavings() As Boolean
Dim uInfo As TimeZoneInfo, lReturn As Long
lReturn = GetTimeZoneInformation(uInfo)
If lReturn = TIME_ZONE_ID_DAYLIGHT Then
IsDLSavings = True
End If
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|