Social Networks

When implementing some software for another project, I came across several requirements involving calculations with time periods. These calculations were an important part of the solution and had high demands in respect to the correctness and accuracy of the results.

The required functionality covered the following areas:

  • Support for individual time periods
  • Working with calendar periods within calendar years
  • Working with calendar periods deviating from the calendar year (fiscal or school periods)

The time calculations should be made available to both server components (Web Services and tasks) as well as for a rich client (Silverlight).

Analyzing the situation brought me to the conclusion that neither the components of the .NET Framework (which I didn’t expect) nor any other available tools would cover all the requirements. Because I already encountered similar needs in earlier projects, I decided to develop a generic library for this purpose.

From several development cycles resulted the following library Time Period, which is now available for the following .NET runtime environments:

  • .NET Framework from Version 2
  • .NET Framework for Silverlight from Version 4
  • .NET Framework for Windows Phone from Version 7

To visualize some of the library functionality, I have put online the Silverlight application Calendar Period Collectorunder http://www.cpc.itenso.com/. It demonstrates the search for calendar periods.

Time Periods

The .NET Framework already offers the extensive base classes DateTime and TimeSpan for basic time related calculations. The library Time Period extends the .NET Framework by several classes for handling periods of time. Such periods are basically characterized by a start, a duration, and an end:

 

Per definition, the start always occurs before the end. The start is considered undefined if it holds the minimal possible value (DateTime.MinValue). Likewise, the end is undefined if it holds the maximal possible value (DateTime.MaxValue).TimePeriod

The implementation of these time periods is based on the interface ITimePeriod and extended by the specializations ITimeRangeITimeBlock and and ITimeInterval:

TimePeriodInterfaces_small

The interface ITimePeriod offers information and operations for time periods without defining the ways in which the crucial properties are being calculated:

  • StartEnd, and Duration of the time period
  • HasStart is true if the Start time is defined
  • HasEnd is true if the End time is defined
  • IsAnytime is true if neither the Start nor the End times are defined
  • IsMoment is true if Start and End hold identical values
  • IsReadOnly is true for immutable time periods (for its usage, see below)

The relation of two time periods is described by the enumeration PeriodRelation:

Methods like IsSamePeriodHasInsideOverlapsWith, or IntersectsWith are available for convenience to query for special, often used variants of such period relations.

Time Range

TimeRange as an implementation of ITimeRange defines the time period by its Start and End; the duration is calculated from these:

Time Range

TimeRange can be created by specifying its Start/EndStart/Duration, or Duration/End. If required, the given Start and End will be sorted chronologically.

For the modification of such a time period, various operations are available (Orange = new instance):

Time Range Operations

This article is copied from the code-project, for download demo and view complete article please go to – http://www.codeproject.com/Articles/168662/Time-Period-Library-for-NET

None found.

Leave a Reply

Your email address will not be published. Required fields are marked *

Allowed Tags:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>