Discussion on:

Message 7 of 18
0 Votes
+ -
and Time comparisons? - a solution
Time comparisons are tricky because the usits we use are recurring decimals when converted from fractions of a day. To overcome this and to add the facility of specifying an accuracy limit, I wrote the following function:

Public Function fGintCompareTimes( _
ByVal vPdtmTime1 As Date, _
ByVal vPdtmTime2 As Date, _
Optional ByVal vPdtmAccuracy As Date = CDate(0.000005787)) As Integer
'==============================================================================
' updated by comment
'------------------------------------------------------------------------------
'24 Feb 06 JEF Updated with standard variable names etc.
'23 MAY 01 JEF First written
'==============================================================================
'Because time is stored as a fractional part of a day which usually results in
'an endlessly recurring decimal value, comparison of two 'equal' times may
'give a 'not equal' result due to rounding errors in cases where the times
'result from calculations. This function was written to eliminate this problem
'by incorporating a limit to the accuracy of 0.5 seconds as the default and an
'absolute minimum of 0.05 seconds.
'==============================================================================
'On Entry:
' vPdtmTime1 contains the time to be compared with ...
' vPdtmTime2 which contains the other time
' vPdtmAccuracy contains the maximum difference between the two times for
' which they will be considered equal (default = 0.5 secs)
'On Exit:
' fGintCompareTimes returns 0 if dtmTime1 = dtmTime2 (within dtmAccuracy)
' 1 if dtmTime1 > dtmTime2
' -1 if dtmTime1 dtmTime2
'==============================================================================
If vPdtmAccuracy CDate(0.0000005787) Then _
vPdtmAccuracy = CDate(0.0000005787)

'times are considered equal if within 'dtmAccuracy' of one another
If Abs(vPdtmTime1 - vPdtmTime2) vPdtmTime2, 1, -1)
End If

End Function
Posted by JohnOfStony
24th Feb 2006