Results 1 to 10 of 10

Thread: Can't Get Correct Daylight Savings Time

  1. #1

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Can't Get Correct Daylight Savings Time

    Hello everybody,

    For a few years I have used the link below from the excellent site vbnet to calculate past, current and future daylight savings time. I am interested in only current and future daylight savings time. However when I run the code (for US/Canada daylight savings time) it calculates correctly the first Sunday of November. However it is supposed to calculate the 2nd Sunday of March but instead calculates the first Sunday of March. Anybody have any ideas? Thanks

    GetTimeZoneInformation: Past, Current and Future Daylight/Standard Dates

    Try running the attached project (copied from vbnet) and you can see for yourselves.

    GetTimeZoneInfo.zip

    Thanks.

  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,526

    Re: Can't Get Correct Daylight Savings Time

    That code is getting the time zone information from your computer system's time zone settings. However, if you have not updated those settings with the latest Microsoft updates for your region, it won't be correct. You did not say what time zone you are in, but I am betting that there is a Microsoft update that you have not installed to correct this. There is (or was) also a tool that you can use to update the settings yourself if no update is available.

  3. #3

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Can't Get Correct Daylight Savings Time

    Quote Originally Posted by jdc2000 View Post
    That code is getting the time zone information from your computer system's time zone settings. However, if you have not updated those settings with the latest Microsoft updates for your region, it won't be correct. You did not say what time zone you are in, but I am betting that there is a Microsoft update that you have not installed to correct this. There is (or was) also a tool that you can use to update the settings yourself if no update is available.
    Thanks for the comment. All updates are up to date on a Windows 10 machine. I am checking US/Canada timezone. Try running the sample code I attached and you should get the same results as this screenshot. In my case it is not due to a missing Microsoft update.

    Name:  Capture.PNG
Views: 546
Size:  4.4 KB

  4. #4
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,526

    Re: Can't Get Correct Daylight Savings Time

    You still did not specify which US/Canada time zone your system is set to, and, just because your Windows 10 computer shows "up-to-date" on the Microsoft updates, that does not necessarily mean that you have the correct time zone update info installed.

    Post the time zone ID number that your system is returning.

  5. #5

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Can't Get Correct Daylight Savings Time

    Quote Originally Posted by jdc2000 View Post
    You still did not specify which US/Canada time zone your system is set to, and, just because your Windows 10 computer shows "up-to-date" on the Microsoft updates, that does not necessarily mean that you have the correct time zone update info installed.

    Post the time zone ID number that your system is returning.
    I am using Eastern Standard Time. Here are two screenshots of what is returned in the IDE:

    Name:  Capture1.PNG
Views: 549
Size:  15.3 KB

    and

    Name:  Capture2.PNG
Views: 534
Size:  5.2 KB

    Thanks.

  6. #6
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,526

    Re: Can't Get Correct Daylight Savings Time

    It looks like the data returned from the Windows system settings by the functions is as expected. The program code may need to be adjusted to account for changes made after the code was last updated.

    North America Canada, United States, Mexico, St. Johns, Bahamas, Turks and Caicos
    Start: First Sunday in April
    End: Last Sunday in October

  7. #7

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Can't Get Correct Daylight Savings Time

    Quote Originally Posted by jdc2000 View Post
    It looks like the data returned from the Windows system settings by the functions is as expected. The program code may need to be adjusted to account for changes made after the code was last updated.
    The correct formula is:

    What are the current rules for daylight saving time?
    The rules for DST changed in 2007 for the first time in more than 20 years. The new changes were enacted by the Energy Policy Act of 2005, which extended the length of DST in the interest of reducing energy consumption. The rules increased the duration of DST by about one month. DST is now in effect for 238 days, or about 65% of the year, although Congress retained the right to revert to the prior law should the change prove unpopular or if energy savings are not significant. At present, daylight saving time in the United States

    begins at 2:00 a.m. on the second Sunday of March and
    ends at 2:00 a.m. on the first Sunday of November

    https://www.nist.gov/pml/time-and-fr...aving-time-dst
    If we read the tzi object that is what is stated. How come the vbnet code then returns the incorrect value for March? Thanks.

  8. #8
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,526

    Re: Can't Get Correct Daylight Savings Time

    In your image in post #5 above, the wDay value under the DaylightDate section is the one you need. The '2' would indicate the change is on the 2nd Sunday. Find where that value is used in the code to see where adjustments are needed.

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Can't Get Correct Daylight Savings Time

    You should step through your code and see where you calculate the date.
    Code:
                tmp = DateSerial(tziYear, _
                                 .wMonth, _
                                (.wDayOfWeek - MonthFirstWeekday + .wDay * 7) Mod 7 + 1)
    Just looking at that line without even running it is obviously wrong.
    The last thing it does is a Mod 7 on whatever day is calculated so it doesn't matter if .wDay is 1 or 2 (or 4 or 1000).
    The answer will always be the first chosen day of the week of the month ( (any integer * 7) mod 7 will always equal 0).

    Since a month can start on any day of the week, and if you're interested in a day of the week that is less than that (i.e. you're interested in Sunday, but the first day of the week of the month starts on any day other than Sunday), you'll get a negative number from (.wDayOfWeek - MonthFirstWeekday). In other cases, (i.e. the first day of the month occurs on Tuesday and you're interested in Thursday), then the difference will be positive.

    So, to find the first day a particular day of the week occurs in a month you do the difference, then add 7 to make sure the value is not negative, and then do a Mod 7 to limit it to 0 to 6 representing the Sunday to Friday range. After you know what date the first particular day of the week occurs (i.e. the first Sunday, or the first Monday, etc.), then you can add the week offset (not before as the code you posted is doing). You would add an offset of 0 * 7 for the first occurance, 1 * 7 for the second occurance, etc...

    So, if you change the code to the following, it should work.
    Code:
                tmp = DateSerial(tziYear, _
                                 .wMonth, _
                                (.wDayOfWeek - MonthFirstWeekday + 7) Mod 7 + (.wDay - 1) * 7 + 1)
    Last edited by passel; May 8th, 2018 at 10:50 PM.

  10. #10

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Can't Get Correct Daylight Savings Time

    Thanks passel, I tried your code and it is working. In the meantime, yesterday I worked on this and found a cleaner way of doing this. I separate the functionality of getting the Nth Weekday of the Nth Month into a separate function which I then just send the tzi object to. I will have to test it for different locations around the world but it seems to be working correctly. Take a look at the code and let me know if you see anything that could go wrong. Thanks again.

    Code:
    Private Function GetTimezoneChangeDate(tziDate As SYSTEMTIME, ByVal tziYear As Long) As Date
    
    On Error Resume Next
    
    ' Have to add 1 as tziDate.wDayOfWeek + 1 vbSunday starts at 0 from the API but 1 from VB
    GetTimezoneChangeDate = GetNthWeekDayOfNthMonth(DateSerial(tziYear, tziDate.wMonth, 1), (tziDate.wDayOfWeek + 1), tziDate.wDay)
       
    End Function
    
    Private Function GetNthWeekDayOfNthMonth(dtDate As Date, intWeekDay As Integer, intNthWeekdayNumber As Integer) As Date
    
    ' Example usage: This will return the 2nd Sunday of March 2018
    ' Call GetNthWeekDayOfNthMonth(DateSerial(2018, 3, 1), vbSunday, 2)
    
    ' Example usage: This will return the 1st Sunday of November 2018
    ' Call GetNthWeekDayOfNthMonth(DateSerial(2018, 11, 1), vbSunday, 1)
    
    Dim newDate As Date
    
    On Error Resume Next
    
    'adjust the increment if it's more than 6
    intNthWeekdayNumber = IIf(intNthWeekdayNumber > 6, 6, intNthWeekdayNumber)
    
    'determine first day of given month
    dtDate = DateSerial(year(dtDate), month(dtDate), 1)
    
    'determine first specified day of given month, e.g. vbSunday
    newDate = dtDate - Weekday(dtDate) + intWeekDay + IIf(Weekday(dtDate) > intWeekDay, 7, 0)
    
    'determine the nth specified day of given month
    newDate = DateAdd("d", 7 * (intNthWeekdayNumber - 1), newDate)
    
    'if the resulting calculation is greater than the length of the
    'specified month, cycle backwards to the last specified day of
    'the month
    Do While month(newDate) <> month(dtDate)
       newDate = newDate - 7
    Loop
    
    GetNthWeekDayOfNthMonth = newDate
    
    End Function

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