473,472 Members | 2,184 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

cannot test time values for equality...why?

What should be a very simple function is going terribly wrong, and I
don't know why. StartTime and EndTime are table values (formatted like
"01:00A" or "02:00P"); DaypartStart and DaypartEnd are string values I
specify when calling the function (initially tried passing time values
& the same problem occurs); In the If statement, I can compare the
exact same time values, and the varCurrentMinute >= varDaypartStart
equality will not work; Two time values will not test to equality,
even though they show as the same time? How do I fix this?
Function DaypartLength(DaypartStart As String, DaypartEnd As String,
StartTime As String, EndTime As String) As Integer

Dim varStartTime, varEndTime As Variant
Dim varDaypartStart, varDaypartEnd As Variant
Dim varCurrentMinute As Variant
Dim intDaypartCount As Integer

varStartTime = TimeValue(StartTime)
varEndTime = TimeValue(EndTime)

varDaypartStart = TimeValue(DaypartStart)
varDaypartEnd = TimeValue(DaypartEnd)

intDaypartCount = 0
varCurrentMinute = varStartTime

Do While varCurrentMinute < varEndTime

If varCurrentMinute >= varDaypartStart _
And varCurrentMinute < varDaypartEnd Then
intDaypartCount = intDaypartCount + 1
End If

varCurrentMinute = varCurrentMinute + #12:01:00 AM#
Loop

If intDaypartCount > 0 Then
DaypartLength = intDaypartCount - 1
Else
DaypartLength = 0
End If
End Function
Nov 13 '05 #1
1 2502
The problem occurs because Access stores date/time values as floating point
numbers, so comparisons are subject to the rounding problems inherent in
floating point numbers.

Internally, the date/time type is stored as a number where the interger part
represents the date, and the time part is a fraction of a day, e.g. .5 =
noon, .25 = 6am. Just as numbers like 1/3 cannot be represented accurately
as decimal numbers, these time values are imprecise, so even two times that
look identical in the way they are printed may not be exactly the same when
printed to 17 decimal places of precision.

A workaround is to ask not if they are equal, but if there is 1 second
difference between them, i.e. replace:
If varCurrentMinute = varDaypartStart Then
with
If Abs(DateDiff("s", varCurrentMinute, varDaypartStart )) > 0 Then

When you use the time value (or any floating point number) as the controller
for the loop, the disparity grows each time through the loop. To avoid this,
you really need to control with loop with a Long Integer that increments in
a discrete number of minutes or seconds.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"gi75research" <gi**********@comcast.net> wrote in message
news:a0*************************@posting.google.co m...
What should be a very simple function is going terribly wrong, and I
don't know why. StartTime and EndTime are table values (formatted like
"01:00A" or "02:00P"); DaypartStart and DaypartEnd are string values I
specify when calling the function (initially tried passing time values
& the same problem occurs); In the If statement, I can compare the
exact same time values, and the varCurrentMinute >= varDaypartStart
equality will not work; Two time values will not test to equality,
even though they show as the same time? How do I fix this?
Function DaypartLength(DaypartStart As String, DaypartEnd As String,
StartTime As String, EndTime As String) As Integer

Dim varStartTime, varEndTime As Variant
Dim varDaypartStart, varDaypartEnd As Variant
Dim varCurrentMinute As Variant
Dim intDaypartCount As Integer

varStartTime = TimeValue(StartTime)
varEndTime = TimeValue(EndTime)

varDaypartStart = TimeValue(DaypartStart)
varDaypartEnd = TimeValue(DaypartEnd)

intDaypartCount = 0
varCurrentMinute = varStartTime

Do While varCurrentMinute < varEndTime

If varCurrentMinute >= varDaypartStart _
And varCurrentMinute < varDaypartEnd Then
intDaypartCount = intDaypartCount + 1
End If

varCurrentMinute = varCurrentMinute + #12:01:00 AM#
Loop

If intDaypartCount > 0 Then
DaypartLength = intDaypartCount - 1
Else
DaypartLength = 0
End If
End Function

Nov 13 '05 #2

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

Similar topics

26
by: Ney André de Mello Zunino | last post by:
Hello. I have noticed, in a lot of C and C++ code, that many programmers seem to prefer putting the test values first in conditional expressions. I.e., they would rather write this: if (-1 ==...
15
by: lawrence | last post by:
Is this the correct way to test for a method before I use it? createRange() is, I believe, an IE only method. function wrapSelectionInTag(selection, tag) { if (document.selection.createRange)...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
4
by: frank | last post by:
I posted a question before (to too many groups) and this time I am sending to this group only. I have a quick script as seen below, the file_list table has a unique field called file_name. The...
5
by: Willson | last post by:
Ok, first off I am learning C++ for the first time. I know Java very well, but a new class of mine requires C++. So bare with me please. I have 2 classes, Birthday.cpp and BirthdayParadox.cpp ...
4
by: Stephan Rose | last post by:
In the following section of code: if(addr == address) { this.cache.Add(entry); return entry.Name; } Looks pretty easy and simple. Problem though is, the return entry.Name line is only...
4
by: dwohlfahrt | last post by:
Hi All, I've spent the last few hours looking for a simple yet clean method via C# that will allow me to test whether three or more strings are equal (by value, of course). I've been forced...
26
by: neha_chhatre | last post by:
can anybody tell me how to compare two float values say for example t and check are two variables declared float how to compare t and check please help me as soon as possible
1
by: J-Burns | last post by:
Hello. Im using doctests to check the functions that ive made. Wat i dnt understand is that it gives me a fialure even though the expected and got values are the same. Check this out: ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.