How to Compare 2 TimeSpans

W

Will

This little puzzle has been bugging me for some time. I'm trying to
create a little service that will check a time frame and compare it to
another timeframe (start / stop time). A real-world example is as
follows:

Patsy would like to volunteer time at a booth this year at the fair.
Her optimal timeframe to volunteer is between 1:00pm and 5:00pm and she
inputs this via some form. Let's say that I want to compare this to a
Dictionary<DateTime, DateTime> that stores start-times and end-times.
So I could get back available timeframes such as:

12:00pm, 1:00pm
1:00pm, 2:30pm
2:30pm, 3:30pm
3:30pm, 4:00pm
4:30pm, 5:30pm

The first timeframe obviously won't work because it starts at 12:00pm.
The last timeframe won't work because while it starts at 4:30pm it's not
over until 5:30pm.

I know that TimeSpan is a span of time between 2 times. Here I actually
have 4 times. What is the quickest way (if any) to make sure the first
timeframe satisfies one or more of the other timeframes. There has got
to be a simple way to compare timeframes in the .NET Framework. I just
haven't stumbled upon it.

Any help is greatly appreciated.

Thanks in advance,

Will
 
M

Marc Gravell

I had a similar problem while working on some scheduling (timetable-
style) software a few years back. My solution was to create an
Interval struct that comprised 2 DateTime values, with methods to
check the Intersection between 2 Intervals, get the Duration, or check
containment (with bool parameter to indicate whether matching bound
were allowed, or if it was strict). I also (for interest) set them up
so that sort and equality were defined in terms of both dates in
order.

Let me know if this doesn't help you ;-p I've spent many an hour in
this exact area...

Marc
 
M

Marc Gravell

I had a similar problem while working on some scheduling (timetable-
style) software a few years back. My solution was to create an
Interval struct that comprised 2 DateTime values, with methods to
check the Intersection between 2 Intervals, get the Duration, or check
containment (with bool parameter to indicate whether matching bound
were allowed, or if it was strict). I also (for interest) set them up
so that sort and equality were defined in terms of both dates in
order.

Let me know if this doesn't help you ;-p I've spent many an hour in
this exact area...

Marc
 
C

Chris Shepherd

Will said:
This little puzzle has been bugging me for some time. I'm trying to
create a little service that will check a time frame and compare it to
another timeframe (start / stop time). A real-world example is as
follows:

Patsy would like to volunteer time at a booth this year at the fair.
Her optimal timeframe to volunteer is between 1:00pm and 5:00pm and she
inputs this via some form. Let's say that I want to compare this to a
Dictionary<DateTime, DateTime> that stores start-times and end-times.
So I could get back available timeframes such as:

12:00pm, 1:00pm
1:00pm, 2:30pm
2:30pm, 3:30pm
3:30pm, 4:00pm
4:30pm, 5:30pm

The first timeframe obviously won't work because it starts at 12:00pm.
The last timeframe won't work because while it starts at 4:30pm it's not
over until 5:30pm.

I know that TimeSpan is a span of time between 2 times. Here I actually

Semantics maybe, but TimeSpan is actually just a measurement of time. It
doesn't have to be between two particular points, it represents it
generically. IE: If I initialize a TimeSpan to 2 hours, 31 minutes, I'm
really just talking about it being 2 hours, 31 minutes long, not
specifically the 2 hours, 31 minutes that occur between 8:00 AM and
10:31 AM.
have 4 times. What is the quickest way (if any) to make sure the first
timeframe satisfies one or more of the other timeframes. There has got
to be a simple way to compare timeframes in the .NET Framework. I just
haven't stumbled upon it.

All you're really doing is comparing the DateTimes. That's going to be
fast even if you do it a lot.
- Does the current range start before our allowed start?
- Does the current range end before our allowed start?
- Does the current range start after our allowed end?
- Does the current range end after our allowed end?

Pretty textbook to me, what's the problem you're having?

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top