Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

How to Compare 2 TimeSpans

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Will - 18 Oct 2007 04:23 GMT
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
Marc Gravell - 18 Oct 2007 05:17 GMT
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
Marc Gravell - 18 Oct 2007 05:18 GMT
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
Chris Shepherd - 18 Oct 2007 13:48 GMT
> 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
[quoted text clipped - 18 lines]
>
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.