473,770 Members | 5,091 Online
Bytes | Software Development & Data Engineering Community
+ 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 varCurrentMinut e >= 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(D aypartStart As String, DaypartEnd As String,
StartTime As String, EndTime As String) As Integer

Dim varStartTime, varEndTime As Variant
Dim varDaypartStart , varDaypartEnd As Variant
Dim varCurrentMinut e As Variant
Dim intDaypartCount As Integer

varStartTime = TimeValue(Start Time)
varEndTime = TimeValue(EndTi me)

varDaypartStart = TimeValue(Daypa rtStart)
varDaypartEnd = TimeValue(Daypa rtEnd)

intDaypartCount = 0
varCurrentMinut e = varStartTime

Do While varCurrentMinut e < varEndTime

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

varCurrentMinut e = varCurrentMinut e + #12:01:00 AM#
Loop

If intDaypartCount > 0 Then
DaypartLength = intDaypartCount - 1
Else
DaypartLength = 0
End If
End Function
Nov 13 '05 #1
1 2524
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 varCurrentMinut e = varDaypartStart Then
with
If Abs(DateDiff("s ", varCurrentMinut e, 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.

"gi75resear ch" <gi**********@c omcast.net> wrote in message
news:a0******** *************** **@posting.goog le.com...
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 varCurrentMinut e >= 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(D aypartStart As String, DaypartEnd As String,
StartTime As String, EndTime As String) As Integer

Dim varStartTime, varEndTime As Variant
Dim varDaypartStart , varDaypartEnd As Variant
Dim varCurrentMinut e As Variant
Dim intDaypartCount As Integer

varStartTime = TimeValue(Start Time)
varEndTime = TimeValue(EndTi me)

varDaypartStart = TimeValue(Daypa rtStart)
varDaypartEnd = TimeValue(Daypa rtEnd)

intDaypartCount = 0
varCurrentMinut e = varStartTime

Do While varCurrentMinut e < varEndTime

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

varCurrentMinut e = varCurrentMinut e + #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
1786
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 == foobar()) than this:
15
3325
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) { var range = document.selection.createRange(); if (range.parentElement() == element) range.text = '<' + tag + '>' + range.text + '<\/' + tag + '>'; }
8
5481
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 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
4
2110
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 problem I am having is that if no unique items exist, this will insert into the table fine. But if a unique item does exist it will not insert as it should. My problem is that I can not get access to return any kind of error. This is very...
5
2419
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 Birthday is a class and Birthday paradox is mainly the driver for it. I am trying to create an object of Birthday in BirthdayParadox and it is not working. I get the error: "'Birthday' undeclared (first use this function)"
4
1507
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 sporadically executed even though it is impossible for an
4
1486
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 to go with the obvious (but ugly, if you ask me) method below: string a = "test";
26
7143
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
1017
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: ********************************************************************** File "C:\Python25\Lib\idlelib\idle.pyw", line ?, in __main__.Test.test8 Failed example: Test.test8() Expected:
0
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10228
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3970
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 we have to send another system
2
3575
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.