473,394 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Standard Time calculation wrong in PreciseDateDiff .... fix

I have used the code provided in the PreciseDateDiff function at the
following Access Web link to calculate the time change date (from
Daylight to Standard time and vice versa):

http://www.mvps.org/access/api/api0024.htm

But looks like for this week only, there is a small error in the
calculation in the 'GetSundate' function included in the code. You
can see what I mean by adding the code below (copied directly from the
above link) to an Access module. Then in the debugger, enter:

?StandardTime(2003)

The supposed StandardTime change date will display as"
11/2/2003 2:00:00 AM

which is obviously not correct. It works correctly for the year 2000
and 2004, but the years between are wrong.

The problem stems from the following line:

varRet = DateAdd("ww", intSun - 1, varRet)

I believe the 'GetTimeZoneInformation' call to the operating system
returns a value of 5 for intSun in October, and the above blindly adds
4 weeks regardless of the date. Consequently, the date rolls into
November sometimes. I have added the following 'if' statement to
change the value to a 4 if this happens:

If intSun = 5 And Month(DateAdd("ww", intSun - 1, varRet)) = 11 Then
intSun = 4
End If
varRet = DateAdd("ww", intSun - 1, varRet)
Just thought others may be interested. I don't know if there is a
more elegant solution.

NOTE: For users who just use the PreciseDateDiff function as is (to
calculate date differences), it may not make a difference, since the 2
calulations will cancel each other, but I needed to have the date of
the change.

Hope this might help anyone else that is using it.

Terry.
'************************** Code Start ***********************
'This code was originally written by Terry Kreft & Michel Walsh
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code courtesy of
'Terry Kreft & Michel Walsh
'
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

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

Declare Function GetTimeZoneInformation Lib "kernel32" _
(lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long

Public Function StandardTime(Optional intYear As Long = -1) As Date
' Originally submitted by Terry Kreft
' modified to accept an optinal year

If -1 = intYear Then intYear = Year(Date)
' Get this year, by defaut, not -1

Dim lngRet As Long
Dim TZI As TIME_ZONE_INFORMATION
lngRet = GetTimeZoneInformation(TZI)
With TZI.StandardDate
StandardTime = CVDate(GetSundate(.wMonth, .wDay, _
intYear) + (.wHour / 24))
End With
End Function

Private Function GetSundate(intMonth As Integer, _
intSun As Integer, _
Optional intYear As Long = -1) _
As Date
' Originally submitted by Terry Kreft
' Modified to set any Year

If intYear = -1 Then intYear = Year(Date)
' if not supplied, get this Year

Dim varRet As Variant
Dim intDayOfWeek As Integer

varRet = DateSerial(intYear, intMonth, 1)
' avoid regional setting problem

intDayOfWeek = Weekday(varRet)
If intDayOfWeek <> 1 Then
varRet = DateAdd("d", 8 - intDayOfWeek, varRet)
End If
varRet = DateAdd("ww", intSun - 1, varRet)
GetSundate = varRet
End Function




Nov 12 '05 #1
2 2510
The code doesn't blindly add 5 what it's doing is using the week offset
(which happens to be 5 in this case for the UK at least) to calculate when
the last day is.

Looking at the documentation again, if a value of 5 is returned from the API
call this actually means the last week of the month rather than an offset
from the start of the month.

So you could do something like

Private Function GetSundate(intMonth As Integer, _
intSun As Integer, _
Optional intYear As Long = -1) _
As Date
' Originally submitted by Terry Kreft
' Modified to set any Year

If intYear = -1 Then intYear = Year(Date)
' if not supplied, get this Year

Dim varRet As Variant
Dim intDayOfWeek As Integer
Select Case intSun
Case 5 ' last week of month
varRet = DateSerial(intYear, intMonth + 1, 1)
' avoid regional setting problem

intDayOfWeek = WeekDay(varRet)
If intDayOfWeek <> 1 Then
varRet = DateAdd("d", 8 - intDayOfWeek, varRet)
End If
varRet = DateAdd("ww", -1, varRet)

Case Else
varRet = DateSerial(intYear, intMonth, 1)
' avoid regional setting problem

intDayOfWeek = WeekDay(varRet)
If intDayOfWeek <> 1 Then
varRet = DateAdd("d", 8 - intDayOfWeek, varRet)
End If
varRet = DateAdd("ww", intSun - 1, varRet)
End Select
GetSundate = varRet
End Function

Terry Kreft
"Terry" <sa********@shaw.ca> wrote in message
news:o3********************************@4ax.com...
I have used the code provided in the PreciseDateDiff function at the
following Access Web link to calculate the time change date (from
Daylight to Standard time and vice versa):

http://www.mvps.org/access/api/api0024.htm

But looks like for this week only, there is a small error in the
calculation in the 'GetSundate' function included in the code. You
can see what I mean by adding the code below (copied directly from the
above link) to an Access module. Then in the debugger, enter:

?StandardTime(2003)

The supposed StandardTime change date will display as"
11/2/2003 2:00:00 AM

which is obviously not correct. It works correctly for the year 2000
and 2004, but the years between are wrong.

The problem stems from the following line:

varRet = DateAdd("ww", intSun - 1, varRet)

I believe the 'GetTimeZoneInformation' call to the operating system
returns a value of 5 for intSun in October, and the above blindly adds
4 weeks regardless of the date. Consequently, the date rolls into
November sometimes. I have added the following 'if' statement to
change the value to a 4 if this happens:

If intSun = 5 And Month(DateAdd("ww", intSun - 1, varRet)) = 11 Then
intSun = 4
End If
varRet = DateAdd("ww", intSun - 1, varRet)
Just thought others may be interested. I don't know if there is a
more elegant solution.

NOTE: For users who just use the PreciseDateDiff function as is (to
calculate date differences), it may not make a difference, since the 2
calulations will cancel each other, but I needed to have the date of
the change.

Hope this might help anyone else that is using it.

Terry.
'************************** Code Start ***********************
'This code was originally written by Terry Kreft & Michel Walsh
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code courtesy of
'Terry Kreft & Michel Walsh
'
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

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

Declare Function GetTimeZoneInformation Lib "kernel32" _
(lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long

Public Function StandardTime(Optional intYear As Long = -1) As Date
' Originally submitted by Terry Kreft
' modified to accept an optinal year

If -1 = intYear Then intYear = Year(Date)
' Get this year, by defaut, not -1

Dim lngRet As Long
Dim TZI As TIME_ZONE_INFORMATION
lngRet = GetTimeZoneInformation(TZI)
With TZI.StandardDate
StandardTime = CVDate(GetSundate(.wMonth, .wDay, _
intYear) + (.wHour / 24))
End With
End Function

Private Function GetSundate(intMonth As Integer, _
intSun As Integer, _
Optional intYear As Long = -1) _
As Date
' Originally submitted by Terry Kreft
' Modified to set any Year

If intYear = -1 Then intYear = Year(Date)
' if not supplied, get this Year

Dim varRet As Variant
Dim intDayOfWeek As Integer

varRet = DateSerial(intYear, intMonth, 1)
' avoid regional setting problem

intDayOfWeek = Weekday(varRet)
If intDayOfWeek <> 1 Then
varRet = DateAdd("d", 8 - intDayOfWeek, varRet)
End If
varRet = DateAdd("ww", intSun - 1, varRet)
GetSundate = varRet
End Function



Nov 12 '05 #2
Thanks for reviewing it Terry,

That works too (even in Canada!)..

Cheers.

Terry H.
On Thu, 30 Oct 2003 16:45:02 -0000, "Terry Kreft"
<te*********@mps.co.uk> wrote:
The code doesn't blindly add 5 what it's doing is using the week offset
(which happens to be 5 in this case for the UK at least) to calculate when
the last day is.

Looking at the documentation again, if a value of 5 is returned from the API
call this actually means the last week of the month rather than an offset
from the start of the month.

So you could do something like

Private Function GetSundate(intMonth As Integer, _
intSun As Integer, _
Optional intYear As Long = -1) _
As Date
' Originally submitted by Terry Kreft
' Modified to set any Year

If intYear = -1 Then intYear = Year(Date)
' if not supplied, get this Year

Dim varRet As Variant
Dim intDayOfWeek As Integer
Select Case intSun
Case 5 ' last week of month
varRet = DateSerial(intYear, intMonth + 1, 1)
' avoid regional setting problem

intDayOfWeek = WeekDay(varRet)
If intDayOfWeek <> 1 Then
varRet = DateAdd("d", 8 - intDayOfWeek, varRet)
End If
varRet = DateAdd("ww", -1, varRet)

Case Else
varRet = DateSerial(intYear, intMonth, 1)
' avoid regional setting problem

intDayOfWeek = WeekDay(varRet)
If intDayOfWeek <> 1 Then
varRet = DateAdd("d", 8 - intDayOfWeek, varRet)
End If
varRet = DateAdd("ww", intSun - 1, varRet)
End Select
GetSundate = varRet
End Function

Terry Kreft
"Terry" <sa********@shaw.ca> wrote in message
news:o3********************************@4ax.com.. .
I have used the code provided in the PreciseDateDiff function at the
following Access Web link to calculate the time change date (from
Daylight to Standard time and vice versa):

http://www.mvps.org/access/api/api0024.htm

But looks like for this week only, there is a small error in the
calculation in the 'GetSundate' function included in the code. You
can see what I mean by adding the code below (copied directly from the
above link) to an Access module. Then in the debugger, enter:

?StandardTime(2003)

The supposed StandardTime change date will display as"
11/2/2003 2:00:00 AM

which is obviously not correct. It works correctly for the year 2000
and 2004, but the years between are wrong.

The problem stems from the following line:

varRet = DateAdd("ww", intSun - 1, varRet)

I believe the 'GetTimeZoneInformation' call to the operating system
returns a value of 5 for intSun in October, and the above blindly adds
4 weeks regardless of the date. Consequently, the date rolls into
November sometimes. I have added the following 'if' statement to
change the value to a 4 if this happens:

If intSun = 5 And Month(DateAdd("ww", intSun - 1, varRet)) = 11 Then
intSun = 4
End If
varRet = DateAdd("ww", intSun - 1, varRet)
Just thought others may be interested. I don't know if there is a
more elegant solution.

NOTE: For users who just use the PreciseDateDiff function as is (to
calculate date differences), it may not make a difference, since the 2
calulations will cancel each other, but I needed to have the date of
the change.

Hope this might help anyone else that is using it.

Terry.
'************************** Code Start ***********************
'This code was originally written by Terry Kreft & Michel Walsh
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code courtesy of
'Terry Kreft & Michel Walsh
'
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

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

Declare Function GetTimeZoneInformation Lib "kernel32" _
(lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long

Public Function StandardTime(Optional intYear As Long = -1) As Date
' Originally submitted by Terry Kreft
' modified to accept an optinal year

If -1 = intYear Then intYear = Year(Date)
' Get this year, by defaut, not -1

Dim lngRet As Long
Dim TZI As TIME_ZONE_INFORMATION
lngRet = GetTimeZoneInformation(TZI)
With TZI.StandardDate
StandardTime = CVDate(GetSundate(.wMonth, .wDay, _
intYear) + (.wHour / 24))
End With
End Function

Private Function GetSundate(intMonth As Integer, _
intSun As Integer, _
Optional intYear As Long = -1) _
As Date
' Originally submitted by Terry Kreft
' Modified to set any Year

If intYear = -1 Then intYear = Year(Date)
' if not supplied, get this Year

Dim varRet As Variant
Dim intDayOfWeek As Integer

varRet = DateSerial(intYear, intMonth, 1)
' avoid regional setting problem

intDayOfWeek = Weekday(varRet)
If intDayOfWeek <> 1 Then
varRet = DateAdd("d", 8 - intDayOfWeek, varRet)
End If
varRet = DateAdd("ww", intSun - 1, varRet)
GetSundate = varRet
End Function




Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: John Bentley | last post by:
John Bentley writes at this level: If we think about our savings accounts then division never comes in (as far as I can see). We deposit and withdraw exact amounts most of the time. Occasionaly...
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
5
by: Tom | last post by:
A field in a data set I want to import into Access is in Unix time (seconds from a certain time on a certain date). Does anyone know the precise date and the precise time on that date that Unix is...
10
by: Marc Pelletier | last post by:
Hello, I am writing an application that does some simple astronomical calculations. One of the variables I need is the number of hours passed in this year. I've written the following function ...
9
by: falcon | last post by:
Is there a way I can do time series calculation, such as a moving average in list comprehension syntax? I'm new to python but it looks like list comprehension's 'head' can only work at a value at...
33
by: ram.ragu | last post by:
hi i have problem to calculate idle time of cpu and if idle time is more then i have to shut down the system. can anyone tell me the idea to so that please
3
by: Petrakid | last post by:
Ok i understand your frustration - but I too am quite frustrated. This assignment was due yesterday, and i am still not getting the right calculations. I said "pigs and chickens" because they are...
6
by: Lara1 | last post by:
I'm trying to get certain cells to show a hovering alert message when I click on them. (I don't want an error-message style box to pop up, because I'll eventually want it to show for lots of cells...
4
by: Timothy Madden | last post by:
Hello I see there is now why to truncate a file (in C or C++) and that I have to use platform-specific functions for truncating files. Anyone knows why ? I mean C/C++ evolved over many years...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.