forked from Giannoudis/TimePeriodLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDurationCalculator.cs
200 lines (171 loc) · 6.3 KB
/
DurationCalculator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// -- FILE ------------------------------------------------------------------
// name : DurationCalculator.cs
// project : Itenso Time Period
// created : Jani Giannoudis - 2013.10.27
// language : C# 4.0
// environment: .NET 2.0
// copyright : (c) 2011-2013 by Itenso GmbH, Switzerland
// --------------------------------------------------------------------------
using System;
namespace Itenso.TimePeriod
{
// ------------------------------------------------------------------------
public class DurationCalculator
{
// ----------------------------------------------------------------------
public DurationCalculator() :
this( new DurationProvider() )
{
} // DurationCalculator
// ----------------------------------------------------------------------
public DurationCalculator( IDurationProvider durationProvider )
{
if ( durationProvider == null )
{
throw new ArgumentNullException( "durationProvider" );
}
this.durationProvider = durationProvider;
} // DurationCalculator
// ----------------------------------------------------------------------
public IDurationProvider DurationProvider
{
get { return durationProvider; }
} // DurationProvider
// ----------------------------------------------------------------------
public ITimePeriodCollection IncludePeriods
{
get { return includePeriods; }
} // IncludePeriods
// ----------------------------------------------------------------------
public ITimePeriodCollection ExcludePeriods
{
get { return excludePeriods; }
} // ExcludePeriods
// ----------------------------------------------------------------------
public void Hours( Time start, Time end )
{
Hours( new HourRange( start, end ) );
} // Hours
// ----------------------------------------------------------------------
public void Hours( params HourRange[] hours )
{
filter.CollectingHours.Clear();
foreach ( HourRange hour in hours )
{
filter.CollectingHours.Add( hour );
}
} // Hours
// ----------------------------------------------------------------------
public void DayHours( DayOfWeek dayOfWeek, Time start, Time end )
{
DayHours( new DayHourRange( dayOfWeek, start, end ) );
} // DayHours
// ----------------------------------------------------------------------
public void DayHours( params DayHourRange[] dayHours )
{
filter.CollectingDayHours.Clear();
foreach ( DayHourRange dayHour in dayHours )
{
filter.CollectingDayHours.Add( dayHour );
}
} // DayHours
// ----------------------------------------------------------------------
public void WeekDays( params DayOfWeek[] weekDays )
{
filter.WeekDays.Clear();
foreach ( DayOfWeek weekDay in weekDays )
{
filter.WeekDays.Add( weekDay );
}
} // WeekDays
// ----------------------------------------------------------------------
public void WorkingWeekDays()
{
filter.WeekDays.Clear();
filter.AddWorkingWeekDays();
} // WorkingWeekDays
// ----------------------------------------------------------------------
public void WeekendWeekDays()
{
filter.WeekDays.Clear();
filter.AddWeekendWeekDays();
} // WeekendWeekDays
// ----------------------------------------------------------------------
public TimeSpan CalcDuration( ITimeRange period )
{
if ( period == null )
{
throw new ArgumentNullException( "period" );
}
return DoCalcDuration( period.Start, period.End );
} // CalcDuration
// ----------------------------------------------------------------------
public TimeSpan CalcDuration( DateTime start, DateTime end )
{
return DoCalcDuration( start, end );
} // CalcDuration
// ----------------------------------------------------------------------
public TimeSpan CalcDayllightDuration( ITimeRange period, TimeZoneInfo timeZone )
{
if ( period == null )
{
throw new ArgumentNullException( "period" );
}
if ( timeZone == null )
{
timeZone = TimeZoneInfo.Local;
}
return DoCalcDuration( period.Start, period.End, timeZone );
} // CalcDayllightDuration
// ----------------------------------------------------------------------
public TimeSpan CalcDayllightDuration( DateTime start, DateTime end, TimeZoneInfo timeZone )
{
if ( timeZone == null )
{
timeZone = TimeZoneInfo.Local;
}
return DoCalcDuration( start, end, timeZone );
} // CalcDayllightDuration
// ----------------------------------------------------------------------
protected virtual TimeSpan DoCalcDuration( DateTime start, DateTime end, TimeZoneInfo timeZone = null )
{
if ( start.Equals( end ) )
{
return TimeSpan.Zero;
}
// test range
TimeRange testRange = new TimeRange( start, end );
// search range
DateTime searchStart = new Day( testRange.Start ).Start;
DateTime serachEnd = new Day( testRange.End ).GetNextDay().Start;
TimeRange searchPeriod = new TimeRange( searchStart, serachEnd );
// exclude periods
filter.ExcludePeriods.Clear();
filter.ExcludePeriods.AddAll( excludePeriods );
// collect hours
TimeCalendar calendar = new TimeCalendar( new TimeCalendarConfig { EndOffset = TimeSpan.Zero } );
CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, searchPeriod, SeekDirection.Forward, calendar );
collector.CollectHours();
TimeSpan duration = TimeSpan.Zero;
collector.Periods.AddAll( includePeriods );
foreach ( ICalendarTimeRange period in collector.Periods )
{
// get the intersection of the test-range and the day hours
ITimePeriod intersection = testRange.GetIntersection( period );
if ( intersection == null )
{
continue;
}
duration = duration.Add( durationProvider.GetDuration( intersection.Start, intersection.End ) );
}
return start < end ? duration : duration.Negate();
} // DoCalcDuration
// ----------------------------------------------------------------------
// members
private readonly CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter();
private readonly TimePeriodCollection includePeriods = new TimePeriodCollection();
private readonly TimePeriodCollection excludePeriods = new TimePeriodCollection();
private readonly IDurationProvider durationProvider;
} // class DurationCalculator
} // namespace Itenso.TimePeriod
// -- EOF -------------------------------------------------------------------