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.
The .NET Framework already offers the extensive base classes
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 (
The implementation of these time periods is based on the interface
ITimePeriod and extended by the specializations
ITimeBlock and and
ITimePeriod offers information and operations for time periods without defining the ways in which the crucial properties are being calculated:
Durationof the time period
Starttime is defined
Endtime is defined
trueif neither the
Endtimes are defined
Endhold identical values
truefor immutable time periods (for its usage, see below)
The relation of two time periods is described by the enumeration
IntersectsWith are available for convenience to query for special, often used variants of such period relations.
TimeRange as an implementation of
ITimeRange defines the time period by its
End; the duration is calculated from these:
TimeRange can be created by specifying its
End. If required, the given
End will be sorted chronologically.
For the modification of such a time period, various operations are available (Orange = new instance):
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-NETNone found.