473,386 Members | 1,830 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,386 software developers and data experts.

Testing Time Values

Hello,

Has anyone ran across an efficient algorithm for determining if a DateTime
or TimeSpan value falls within two other DateTime/TimeSpan values?

For example, if I have a start time of 09:00 and an end time of 21:00 and I
want to test if 16:00 is between them, this is fairly straightforward.
However, if my start time is 21:00 and my end time is 09:00 and I want to
test if 04:00 is between them, then it's not quite as straightforward.

I don't want to take the actual dates into account, only the times.

Any ideas?

--- Thanks, Jeff
Nov 16 '05 #1
8 1634
Hi Jeff,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to check if a certain DateTime
is in a certain range. If there is any misunderstanding, please feel free
to let me know.

There is no direct method for us to call in the .NET framework. Since it
has no date information in it, I think it's hard to write such a method.
For example, we need to check if a time is between 21:00 and 9:00. Now we
pass in 6:00. But how do we know it is the 6:00 of the first day or the
next day?

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #2
> For example, we need to check if a time is between 21:00 and 9:00. Now we
pass in 6:00. But how do we know it is the 6:00 of the first day or the
next day?


Kevin,

As far as my needs go, I'm not concerned about which day it is. In the
example you state above, if 06:00 falls "anywhere" between 21:00 and 09:00,
which it does, then I would want a result of true to come back. If I were
testing to see if 06:00 falls "anywhere" between 09:00 and 21:00 (the
reverse of the example above) then I would want false to come back since
06:00 does not fall anywhere between 9 in the morning and 9 in the evening.

Does this make more sense?

--- Thanks, Jeff
Nov 16 '05 #3
Hi Jeff

Try this. It works for me

// Check if a given TimeSpan DateTime is between two other given TimeSpans

private bool IsTimeBetween(TimeSpan tsToTest, TimeSpan tsStart, TimeSpan
tsEnd)
{
bool bRet = false;
if (tsStart > tsEnd)
{
TimeSpan ts0 = new TimeSpan(0); // 00:00:00
TimeSpan ts1 = new TimeSpan(TimeSpan.TicksPerDay-1); // 23:59:59.xxx
bRet = (IsTimeBetween(tsToTest, tsStart, ts1) ||
(IsTimeBetween(tsToTest, ts0, tsEnd)));
}
else
{
if ((tsToTest >= tsStart) && (tsToTest <= tsEnd))
{
bRet = true;
}
}
return bRet;
}

// Check if time part of a given DateTime is between two given TimeSpans
private bool IsTimeBetween(DateTime dtToTest, TimeSpan tsStart, TimeSpan
tsEnd)
{
return IsTimeBetween(dtToTest.TimeOfDay, tsStart, tsEnd);
}

Boaz Ben-Porat
Milstone Systems

"Jeff B." <js*@community.nospam> wrote in message
news:eP**************@tk2msftngp13.phx.gbl...
Hello,

Has anyone ran across an efficient algorithm for determining if a DateTime
or TimeSpan value falls within two other DateTime/TimeSpan values?

For example, if I have a start time of 09:00 and an end time of 21:00 and
I want to test if 16:00 is between them, this is fairly straightforward.
However, if my start time is 21:00 and my end time is 09:00 and I want to
test if 04:00 is between them, then it's not quite as straightforward.

I don't want to take the actual dates into account, only the times.

Any ideas?

--- Thanks, Jeff

Nov 16 '05 #4
Jeff B. <js*@community.nospam> wrote:
For example, we need to check if a time is between 21:00 and 9:00. Now we
pass in 6:00. But how do we know it is the 6:00 of the first day or the
next day?


As far as my needs go, I'm not concerned about which day it is. In the
example you state above, if 06:00 falls "anywhere" between 21:00 and 09:00,
which it does, then I would want a result of true to come back. If I were
testing to see if 06:00 falls "anywhere" between 09:00 and 21:00 (the
reverse of the example above) then I would want false to come back since
06:00 does not fall anywhere between 9 in the morning and 9 in the evening.

Does this make more sense?


Not really, because it falls between 9 in the morning and 9 on the
following evening. I think you need a more precise definition of
exactly what you mean. Once you've got a precise definition, turning it
into code should be simple.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
> Not really, because it falls between 9 in the morning and 9 on the
following evening. I think you need a more precise definition of
exactly what you mean. Once you've got a precise definition, turning it
into code should be simple.


Ok, to be more precise, if I were testing to see if 06:00 falls "anywhere"
between 09:00 and 21:00 I am talking about the _first_ occurrence of 21:00
following 09:00. In other words, if the timespan being tested falls between
the starting timespan and the first occurrence of the ending timespan
(whether it happens to fall on the first day or next day) then I want a
method that returns true; else I want it to return false.

I hope this makes more sense.

--- Thanks, Jeff
Nov 16 '05 #6
Boaz,

This algorithm works great. It's along the same lines of what I was
attempting but a lot simpler and cleaner. Thanks for the help.

--- Jeff
Hi Jeff

Try this. It works for me

// Check if a given TimeSpan DateTime is between two other given TimeSpans

private bool IsTimeBetween(TimeSpan tsToTest, TimeSpan tsStart, TimeSpan
tsEnd)
{
bool bRet = false;
if (tsStart > tsEnd)
{
TimeSpan ts0 = new TimeSpan(0); // 00:00:00
TimeSpan ts1 = new TimeSpan(TimeSpan.TicksPerDay-1); //
23:59:59.xxx
bRet = (IsTimeBetween(tsToTest, tsStart, ts1) ||
(IsTimeBetween(tsToTest, ts0, tsEnd)));
}
else
{
if ((tsToTest >= tsStart) && (tsToTest <= tsEnd))
{
bRet = true;
}
}
return bRet;
}

// Check if time part of a given DateTime is between two given TimeSpans
private bool IsTimeBetween(DateTime dtToTest, TimeSpan tsStart, TimeSpan
tsEnd)
{
return IsTimeBetween(dtToTest.TimeOfDay, tsStart, tsEnd);
}

Boaz Ben-Porat
Milstone Systems

Nov 16 '05 #7
Jeff B. <js*@community.nospam> wrote:
Not really, because it falls between 9 in the morning and 9 on the
following evening. I think you need a more precise definition of
exactly what you mean. Once you've got a precise definition, turning it
into code should be simple.


Ok, to be more precise, if I were testing to see if 06:00 falls "anywhere"
between 09:00 and 21:00 I am talking about the _first_ occurrence of 21:00
following 09:00. In other words, if the timespan being tested falls between
the starting timespan and the first occurrence of the ending timespan
(whether it happens to fall on the first day or next day) then I want a
method that returns true; else I want it to return false.

I hope this makes more sense.


Right. In that case, I think you need to:

a) Take the start time and the end time, constructing DateTimes for
both of them on the same day
b) If the end time is currently before the start time, add 24 hours
c) See if the test time is between start time and end time

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Boaz Ben-Porat <bb*@milestone.dk> wrote:
Try this. It works for me


<snip>

I think that can be made a bit more readable, with the same interface:

static bool IsTimeBetween (TimeSpan tsToTest,
TimeSpan tsStart,
TimeSpan tsEnd)
{
DateTime day = DateTime.Today;

DateTime start = day+tsStart;
DateTime end = day+tsEnd;
DateTime test = day+tsTest;

if (start > end)
{
end = end.AddDays (1);
}
return (test >= start && test <= end);
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9

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

Similar topics

0
by: Jonathan Allen | last post by:
We have found that our method of testing does not match traditional text-book methodologies. I decided to write a short white paper on it so that I could get your opinions. Does anyone else use...
38
by: Christoph Zwerschke | last post by:
In August 2001, there was a thread about the "Art of Unit Testing": http://groups.google.com/group/comp.lang.python/browse_frm/thread/aa2bd17e7f995d05/71a29faf0a0485d5 Paul Moore asked the...
19
by: lihua | last post by:
Hi, Group! I got one question here: We all know that fclose() must be called after file operations to avoid unexpected errors.But there are really cases when you forget to do that!Just like...
72
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: ...
7
by: Diffident | last post by:
Hello All, Can anyone please suggest me a good unit testing tool. I have seen NUnit but not sure on how I can use it to test my methods which involve session variables, viewstate variables,...
24
by: David | last post by:
Hi list. What strategies do you use to ensure correctness of new code? Specifically, if you've just written 100 new lines of Python code, then: 1) How do you test the new code? 2) How do...
5
by: =?Utf-8?B?U2FsYW1FbGlhcw==?= | last post by:
Hi, I know that VS 2005 has a lot of testing features and already used them for doing web load testing. I am wondering if it is possible to load test a win forms application. I don't mean writing...
11
by: VK | last post by:
In the continuation of the discussion at "Making Site Opaque -- This Strategy Feasible?" and my comment at http://groups.google.com/group/comp.lang.javascript/msg/b515a4408680e8e2 I have...
0
by: Matthew Fitzgibbons | last post by:
I'm by no means a testing expert, but I'll take a crack at it. Casey McGinty wrote: I've never run into this. Rule of thumb: always separate software from hardware. Write mock classes or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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?
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
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...

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.