In one of my recent .net projects (C#) I had the need to implement an object which represents a time range which is defined by a start time and an end time. In addition it should detect collision with other time ranges and check if given TimeSpans are within the time range itself. Last but not least it should support parsing time ranges from strings and format the range in a nice way. For this I’ve written a small class which could be useful for some of you out there.
Let’s have a look how this thing needs to be used. There are three constructors for the usage so the creation of instances can be done in various ways. Additionally an instance can be obtained by using the Parse method. Some examples of creation…
- TimeRange t = null;
- //instances for a range of 10:00 - 12:00
- t = new TimeRange(10, 0, 12, 0);
- t = new TimeRange(new TimeSpan(10, 0, 0), new TimeSpan(12, 0, 0));
- //time range 00:00-24:00
- t = new TimeRange();
- //parsing time ranges from a string
- t = TimeRange.Parse("10-12");
- t = TimeRange.Parse("10:00-12:00");
If we have to ore more time ranges we can check if they colide. In other words if they overlap or clash. Everything the same
- TimeRange t = new TimeRange(10, 0, 12, 0);
- bool clash = t.Clashes(new TimeRange(10, 0, 11, 0));
- //clash with bounds will clash 10:00-12:00 with 09:00-10:00
- t = new TimeRange(10, 0, 12, 0);
- clash = t.Clashes(new TimeRange(9, 0, 10, 0), true);
We can also check if a given TimeSpan is within the range or even another TimeRange.
- //we check if the timerange 11:10-12:00 lies completely in 10:30-12:45
- TimeRange t = new TimeRange(10, 30, 12, 45);
- bool inside = t.IsIn(new TimeRange(11, 10, 12, 0));
- //check if 18:00 is in the range
- inside = t.IsIn(new TimeSpan(18, 0, 0));
That should be it. Have fun with it and dont hasitate to drop me any feedback about this stuff or even extensions.
Overview of C# TimeRange:
- Parsing a timerange out of a string (supports various formats)
- Parses also TimeSpan from a string (ParseTimeSpan method)
- Collision detection of two timeranges (inclusive and exclusive)
- Detection of a given TimeSpan is within the Range
- Equality check of time ranges
- Formatting in a user friendly way (ToString overridden)
The TimeRange for C# tested with .net framework 2.0 can be downloaded here…
Thanks for the code, I found it usefull.
another example of use might be checking if the current time falls in a range:
static void Main(string[] args)
{
TimeRange timeRange = new TimeRange();
timeRange = TimeRange.Parse(“13:00-14:00″);
bool IsNowInTheRange = timeRange.IsIn(DateTime.Now.TimeOfDay);
Console.Write(IsNowInTheRange);
Console.ReadLine();
}
Cheers,
Stonie
Hi,
Thanks for the class, I’ve added the following code:
Visual Studio + .NET 3.5 was complaining that you overrode the == and != operators without overriding that method. Maybe you might need something more complex there, for me was enough.
Thanks!
What’s the license for it?
Unfortunately you’ve not determined if the range is from 17:00 - 02:00 the next morning.
var t = new TimeRange(opening, closing);
var b = t.IsIn(timespanNowValue);
“End must be after Start.” error