TimeRange object for C#

By | August 24

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…

  1. TimeRange t = null;
  2. //instances for a range of 10:00 - 12:00
  3. t = new TimeRange(10, 0, 12, 0);
  4. t = new TimeRange(new TimeSpan(10, 0, 0), new TimeSpan(12, 0, 0));
  5.  
  6. //time range 00:00-24:00
  7. t = new TimeRange();
  8.  
  9. //parsing time ranges from a string
  10. t = TimeRange.Parse("10-12");
  11. 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 :)

  1. TimeRange t = new TimeRange(10, 0, 12, 0);
  2. bool clash = t.Clashes(new TimeRange(10, 0, 11, 0));
  3.  
  4. //clash with bounds will clash 10:00-12:00 with 09:00-10:00
  5. t = new TimeRange(10, 0, 12, 0);
  6. 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.

  1. //we check if the timerange 11:10-12:00 lies completely in 10:30-12:45
  2. TimeRange t = new TimeRange(10, 30, 12, 45);
  3. bool inside = t.IsIn(new TimeRange(11, 10, 12, 0));
  4.  
  5. //check if 18:00 is in the range
  6. 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…

TimeRange class for C#

Category: c#

4 comments on “TimeRange object for C#

  1. 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

  2. Hi,

    Thanks for the class, I’ve added the following code:

    1. public override int GetHashCode()
    2.         {
    3.             return base.GetHashCode();
    4.         }

    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!

  3. What’s the license for it?

  4. 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