Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Daylights Savings determination - 1st sunday of a month?

7 views
Skip to first unread message

FN

unread,
Nov 19, 2002, 8:54:43 PM11/19/02
to
I need to write code that will look at a given client, see a given date, and
know if daylight savings time was being observed. The formula is pretty
basic but I don't know how to best tackle it in VB. Basically, as seen at
this link, a country generally (forget exceptions for this post) moves ahead
on the 1st sunday of a month before summer, and back on the last sunday of a
month after summer. My code, though, has to do that for any year going
back in time or forward. I'd pass a date and location into my function and
it should return if daylight savings was in effect.

http://webexhibits.org/daylightsaving/g.html

How can my VB determine the 1st sunday, or the last without being big/ugly?
Perhaps someone has a nice elegant way?


Matthew Curland

unread,
Nov 19, 2002, 10:52:12 PM11/19/02
to
VB has a number of date functions (look under the DateTime section in the
object browser).
Public Function FirstDayInMonth(ByVal Year As Long, ByVal Month As Long,
ByVal Day As VbDayOfWeek) As Date
Dim FirstDay As Date
FirstDay = DateSerial(Year, Month, 1)
FirstDayInMonth = DateAdd("d", 7 - Weekday(FirstDay, (Day Mod 7) + 1),
FirstDay)
End Function
Public Function LastDayInMonth(ByVal Year As Long, ByVal Month As Long,
ByVal Day As VbDayOfWeek) As Date
If Month = 12 Then
Month = 0
Year = Year + 1
End If
LastDayInMonth = DateAdd("d", -7, FirstDayInMonth(Year, Month + 1, Day))
End Function

You can use the Year function to get the year from a given date. Also, you
can use +/- instead of DateAdd in both functions if you only care about
dates after 1900. So, for a given date, DST in the USA starts on
FirstDayInMonth(Year(testDate), 4, vbSunday) and ends on
LastDayInMonth(Year(testDate), 10, vbSunday)

-Matt

"FN" <newsgroupYESDE...@DELETECAPSyahoo.com> wrote in message
news:DXBC9.18463$%k2.54...@twister.socal.rr.com...

Michael Dunn

unread,
Nov 19, 2002, 11:48:56 PM11/19/02
to

"FN" <newsgroupYESDE...@DELETECAPSyahoo.com> wrote in message news:DXBC9.18463$%k2.54...@twister.socal.rr.com...
: I need to write code that will look at a given client, see a given date, and
:
You could try something like this (assumes DST is 1st Sun in April to last Sunday in October)
(not fully tested - quittin' time:)

Dim checkDate As Date
checkDate = DateValue(Text1.Text)

If checkDate >= DateSerial(Year(checkDate), 4, 1 _
- Weekday(DateSerial(Year(checkDate), 4, 1), vbMonday) + 7) And _
checkDate <= DateSerial(Year(checkDate), 11, 1 _
- Weekday(DateSerial(Year(checkDate), 11, 1), vbMonday)) Then
MsgBox "daylight savings time"
Else: MsgBox "normal time"
End If

Kaj G Backas

unread,
Nov 20, 2002, 3:16:41 AM11/20/02
to
In article <upJGz$EkCHA.2832@tkmsftngp11>, m_o...@yahoo.com says...

Please note that daylight saving time varies in different countries.
E.g. European Union has its own, as well as United Kingdom (England).
Both differ from the one used in USA.
br kgb

Adam D. Barratt

unread,
Nov 20, 2002, 3:54:15 AM11/20/02
to
Kaj G Backas <k...@systecon.fi> wrote:
> In article <upJGz$EkCHA.2832@tkmsftngp11>, m_o...@yahoo.com says...
>
> Please note that daylight saving time varies in different countries.
> E.g. European Union has its own, as well as United Kingdom (England).
> Both differ from the one used in USA.

Oi! UK <> England.

The relevant timezone isn't just the UK either, as it includes Eire
(Ireland, if you prefer), and we don't call it DST.

Other than that, spot on. ;-)

Adam
--
Work expands to fill the time available.
-- Cyril Northcote Parkinson, "The Economist", 1955

Michael Dunn

unread,
Nov 20, 2002, 6:43:53 AM11/20/02
to

"FN" <newsgroupYESDE...@DELETECAPSyahoo.com> wrote in message news:DXBC9.18463$%k2.54...@twister.socal.rr.com...
: I need to write code that will look at a given client, see a given date, and


This is still not exactly what you're after, but it might give you some ideas.

The last day of DST I've called "normal time"

In theory, if you click on a country, enter a date, hit command1, you'll get a message whether it's DST or normal time
(it's extremely raw - so may require a tweak or two)

What this needs is
list1 - sorted (to display countries)
text1 (to enter a date to check)
command1 (go button)
text2 (country name to add to list1)
combo1 to 6 (style 2 - dropdown)
(group in 3's, 1-3 for DST start, 4-6 for DST end)
command2 (add new details)


Option Explicit
Private Data(1000, 7) As String
'----------------------------------
Private Sub Form_Load()
Dim i As Integer
Combo1.AddItem "1st": Combo1.AddItem "2nd"
Combo1.AddItem "3rd": Combo1.AddItem "4th"
Combo1.AddItem "Last": Combo4.AddItem "1st"
Combo4.AddItem "2nd": Combo4.AddItem "3rd"
Combo4.AddItem "4th": Combo4.AddItem "Last"

For i = 1 To 12
If i < 8 Then
Combo2.AddItem Format(DateSerial(6, 1, i), "dddd")
Combo5.AddItem Format(DateSerial(6, 1, i), "dddd")
End If
Combo3.AddItem Format(DateSerial(1, i, 1), "mmmm")
Combo6.AddItem Format(DateSerial(1, i, 1), "mmmm")
Next
LoadListBox
End Sub
'--------------------------------
Private Sub Command1_Click()

If List1.ListIndex = -1 Then MsgBox "no country": Exit Sub

Dim checkDate As Date
Dim num As Integer
Dim firstDOWfrom As Integer
Dim firstDOWto As Integer
Dim mthFrom As Integer
Dim mthTo As Integer
Dim yrFrom As Integer
Dim yrTo As Integer

checkDate = DateValue(Text1.Text)
num = List1.ItemData(List1.ListIndex)
firstDOWfrom = (Val(Data(num, 2)) + 1) Mod 8
If firstDOWfrom = 0 Then firstDOWfrom = 1
firstDOWto = (Val(Data(num, 5)) + 1) Mod 8
If firstDOWto = 0 Then firstDOWfrom = 1
mthFrom = Val(Data(num, 3))
yrFrom = Year(checkDate)
If Month(checkDate) < mthFrom Then yrFrom = yrFrom - 1
If Data(num, 1) = 5 Then mthFrom = mthFrom + 1
mthTo = Val(Data(num, 6))
If Data(num, 4) = 5 Then mthTo = mthTo + 1
yrTo = Year(checkDate)
If mthFrom > mthTo Then yrTo = yrFrom + 1

If checkDate >= DateSerial(yrFrom, mthFrom, 1 _
- Weekday(DateSerial(yrFrom, mthFrom, 1), firstDOWfrom) _
+ (Data(num, 1) Mod 5) * 7) And _
checkDate <= DateSerial(yrTo, mthTo, 1 _
- Weekday(DateSerial(yrTo, mthTo, 1), firstDOWto) _
+ (Data(num, 4) Mod 5) * 7 - 1) Then


MsgBox "daylight savings time"
Else: MsgBox "normal time"
End If

End Sub
'---------------------------------------
Private Sub Command2_Click()
Dim ff As Long: ff = FreeFile()
Open App.Path & "\DST.txt" For Append As #ff
Write #ff, Text2.Text, Val(Combo1.ListIndex) + 1, _
Val(Combo2.ListIndex) + 1, Val(Combo3.ListIndex) + 1, _
Val(Combo4.ListIndex) + 1, Val(Combo5.ListIndex) + 1, _
Val(Combo6.ListIndex) + 1
Close #ff
LoadListBox
End Sub
'---------------------------
Private Sub LoadListBox()
Dim ff As Long: ff = FreeFile()
Dim i As Integer
List1.Clear
If Len(Dir(App.Path & "\DST.txt")) > 0 Then
Open App.Path & "\DST.txt" For Input As #ff
Do While Not EOF(ff)
Input #ff, Data(i, 0), Data(i, 1), Data(i, 2), _
Data(i, 3), Data(i, 4), Data(i, 5), Data(i, 6)
List1.AddItem Data(i, 0)
List1.ItemData(List1.NewIndex) = i
i = i + 1
Loop
Close #ff
End If
End Sub

Michael Dunn

unread,
Nov 20, 2002, 6:48:20 AM11/20/02
to

"Kaj G Backas" <k...@systecon.fi> wrote in message news:MPG.184571676...@msnews.microsoft.com...
: In article <upJGz$EkCHA.2832@tkmsftngp11>, m_o...@yahoo.com says...

:
: Please note that daylight saving time varies in different countries.
: E.g. European Union has its own, as well as United Kingdom (England).
: Both differ from the one used in USA.
: br kgb

It's just an example

Rick Rothstein

unread,
Nov 20, 2002, 11:04:25 AM11/20/02
to
"FN" <newsgroupYESDE...@DELETECAPSyahoo.com> wrote in
message news:DXBC9.18463$%k2.54...@twister.socal.rr.com...

Paste the following into your project. It will tell you if Daylight
Savings Time is currently engaged on the computer. Simply call the
function like this

MsgBox IsDST

There is the possibility for GetTimeZoneInformation to return an UNKNOWN
condition; I treat this as a False for the purposes of this function.

Rick - MVP


Private Declare Function GetTimeZoneInformation _
Lib "kernel32" _
(lpTimeZoneInformation As _
TIME_ZONE_INFORMATION) 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(31) As Integer
StandardDate As SYSTEMTIME
StandardBias As Long
DaylightName(31) As Integer
DaylightDate As SYSTEMTIME
DaylightBias As Long
End Type

Public Function IsDST() As Boolean
Dim TZInfo As TIME_ZONE_INFORMATION
Const TIME_ZONE_ID_DAYLIGHT As Long = 2
If GetTimeZoneInformation(TZInfo) = _
TIME_ZONE_ID_DAYLIGHT Then IsDST = True
End Function

FN

unread,
Nov 20, 2002, 2:26:38 PM11/20/02
to
Thank you to all that replied. The info was a big help.


Jim Mack

unread,
Nov 25, 2002, 10:05:30 AM11/25/02
to
If you have to move backward to any date, say in the last century, and
for any locale, then your task is hopeless. And going forward in time?
How could that ever work, given that the rules change from time to time?

For example, the US was on "War Time" during WW2, essentially year-round
DST. On exactly what dates did this switch in and out?

Also, the present US scheme of first Sunday in April / last Sunday in
October is only about 30 years old -- used to be the last Sunday in
April. Before that, DST was not universally observed, and even now
there are whole states, and some individual counties, that never observe
DST. At least one town straddles such a border, and they must keep two
sets of clocks to know the time on opposite sides of the street.

--

Jim Mack
MicroDexterity Inc
www.microdexterity.com

"FN" wrote...

0 new messages