I am fully aware of the inability of DateDiff () to cope with day saving issues. Since I often use it to compare the number of hours or days between two dates between several months, I need to write a solution to handle DST. This is what I came up with, a function that first subtracts 60 minutes from the datetime value if it falls within the date ranges given in the local table (LU_DST). Thus, the use will be:
datediff("n",Conv_DST_to_Local([date1]),Conv_DST_to_Local([date2]))
My question is: is there a better way to handle this? I am going to make a wild assumption that I am not the first person with this question. This is similar to what should have been added to one of the main link libraries. Is there a way to access my system clock to ask him if the DST was valid on specific dates and times?
Function Conv_DST_to_Local(X As Date) As Date
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("LU_DST")
Conv_DST_to_Local = X
While rst.EOF = False
If X > rst.Fields(0) And X < rst.Fields(1) Then Conv_DST_to_Local = DateAdd("n", -60, X)
rst.MoveNext
Wend
End Function
Notes
- I visited and imported the BAS file http://www.cpearson.com/excel/TimeZoneAndDaylightTime.aspx . I spent at least an hour reading it, and although he can do his job well, I cannot figure out how to change it to my needs. But if you have an answer using your data structures, I will take a look.
- Time zones are not a problem, as it is all the time.
Edit
Thanks for the solutions, HansUp and Fenton, but I see an error in WbemScripting:
Debug.Print Conv_date_to_Utc(
11/7/2009 6:15:00 PM
Debug.Print Conv_date_to_Utc(
11/8/2009 7:15:00 PM
( GMT-05: 00). , , , DST 2009 1-, 7-. ?