forked from KSemenenko/TimePeriodLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateAdd.cs
More file actions
300 lines (260 loc) · 8.41 KB
/
DateAdd.cs
File metadata and controls
300 lines (260 loc) · 8.41 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// -- FILE ------------------------------------------------------------------
// name : DateAdd.cs
// project : Itenso Time Period
// created : Jani Giannoudis - 2011.03.19
// language : C# 4.0
// environment: .NET 2.0
// copyright : (c) 2011-2012 by Itenso GmbH, Switzerland
// --------------------------------------------------------------------------
using System;
using System.Collections.Generic;
namespace Itenso.TimePeriod
{
// ------------------------------------------------------------------------
public class DateAdd
{
// ----------------------------------------------------------------------
public ITimePeriodCollection IncludePeriods
{
get { return includePeriods; }
} // IncludePeriods
// ----------------------------------------------------------------------
public ITimePeriodCollection ExcludePeriods
{
get { return excludePeriods; }
} // ExcludePeriods
// ----------------------------------------------------------------------
public virtual DateTime? Subtract( DateTime start, TimeSpan offset, SeekBoundaryMode seekBoundaryMode = SeekBoundaryMode.Next )
{
if ( includePeriods.Count == 0 && excludePeriods.Count == 0 )
{
return start.Subtract( offset );
}
TimeSpan? remaining;
return offset < TimeSpan.Zero ?
CalculateEnd( start, offset.Negate(), SeekDirection.Forward, seekBoundaryMode, out remaining ) :
CalculateEnd( start, offset, SeekDirection.Backward, seekBoundaryMode, out remaining );
} // Subtract
// ----------------------------------------------------------------------
public virtual DateTime? Add( DateTime start, TimeSpan offset, SeekBoundaryMode seekBoundaryMode = SeekBoundaryMode.Next )
{
if ( includePeriods.Count == 0 && excludePeriods.Count == 0 )
{
return start.Add( offset );
}
TimeSpan? remaining;
return offset < TimeSpan.Zero ?
CalculateEnd( start, offset.Negate(), SeekDirection.Backward, seekBoundaryMode, out remaining ) :
CalculateEnd( start, offset, SeekDirection.Forward, seekBoundaryMode, out remaining );
} // Add
// ----------------------------------------------------------------------
protected DateTime? CalculateEnd( DateTime start, TimeSpan offset,
SeekDirection seekDirection, SeekBoundaryMode seekBoundaryMode, out TimeSpan? remaining )
{
if ( offset < TimeSpan.Zero )
{
throw new InvalidOperationException( "time span must be positive" );
}
remaining = offset;
// search periods
TimePeriodCollection searchPeriods = new TimePeriodCollection( includePeriods );
// no search periods specified: search anytime
if ( searchPeriods.Count == 0 )
{
searchPeriods.Add( TimeRange.Anytime );
}
// available periods
ITimePeriodCollection availablePeriods = new TimePeriodCollection();
// no exclude periods specified: use all search periods
if ( excludePeriods.Count == 0 )
{
availablePeriods.AddAll( searchPeriods );
}
else // remove exclude periods
{
TimeGapCalculator<TimeRange> gapCalculator = new TimeGapCalculator<TimeRange>();
foreach ( ITimePeriod searchPeriod in searchPeriods )
{
// no overlaps: use the entire search range
if ( !excludePeriods.HasOverlapPeriods( searchPeriod ) )
{
availablePeriods.Add( searchPeriod );
}
else // add gaps of search period using the exclude periods
{
availablePeriods.AddAll( gapCalculator.GetGaps( excludePeriods, searchPeriod ) );
}
}
}
// no periods available
if ( availablePeriods.Count == 0 )
{
return null;
}
// combine the available periods, ensure no overlapping
// used for FindNextPeriod/FindPreviousPeriod
if ( availablePeriods.Count > 1 )
{
TimePeriodCombiner<TimeRange> periodCombiner = new TimePeriodCombiner<TimeRange>();
availablePeriods = periodCombiner.CombinePeriods( availablePeriods );
}
// find the starting search period
ITimePeriod startPeriod = null;
DateTime seekMoment = start;
switch ( seekDirection )
{
case SeekDirection.Forward:
startPeriod = FindNextPeriod( start, availablePeriods, out seekMoment );
break;
case SeekDirection.Backward:
startPeriod = FindPreviousPeriod( start, availablePeriods, out seekMoment );
break;
}
// no starting period available
if ( startPeriod == null )
{
return null;
}
// no offset: use the search staring position
// maybe moved to the next available search period
if ( offset == TimeSpan.Zero )
{
return seekMoment;
}
// setup destination search
switch ( seekDirection )
{
case SeekDirection.Forward:
for ( int i = availablePeriods.IndexOf( startPeriod ); i < availablePeriods.Count; i++ )
{
ITimePeriod gap = availablePeriods[ i ];
TimeSpan gapRemining = gap.End - seekMoment;
bool isTargetPeriod = false;
switch ( seekBoundaryMode )
{
case SeekBoundaryMode.Fill:
isTargetPeriod = gapRemining >= remaining;
break;
case SeekBoundaryMode.Next:
isTargetPeriod = gapRemining > remaining;
break;
}
if ( isTargetPeriod )
{
DateTime end = seekMoment + remaining.Value;
remaining = null;
return end;
}
remaining = remaining - gapRemining;
if ( i == availablePeriods.Count - 1 )
{
return null;
}
seekMoment = availablePeriods[ i + 1 ].Start; // next period
}
break;
case SeekDirection.Backward:
for ( int i = availablePeriods.IndexOf( startPeriod ); i >= 0; i-- )
{
ITimePeriod gap = availablePeriods[ i ];
TimeSpan gapRemining = seekMoment - gap.Start;
bool isTargetPeriod = false;
switch ( seekBoundaryMode )
{
case SeekBoundaryMode.Fill:
isTargetPeriod = gapRemining >= remaining;
break;
case SeekBoundaryMode.Next:
isTargetPeriod = gapRemining > remaining;
break;
}
if ( isTargetPeriod )
{
DateTime end = seekMoment - remaining.Value;
remaining = null;
return end;
}
remaining = remaining - gapRemining;
if ( i == 0 )
{
return null;
}
seekMoment = availablePeriods[ i - 1 ].End; // previous period
}
break;
}
return null;
} // CalculateEnd
// ----------------------------------------------------------------------
// assumes no no overlapping periods in parameter periods
private static ITimePeriod FindNextPeriod( DateTime start, IEnumerable<ITimePeriod> periods, out DateTime moment )
{
ITimePeriod nearestPeriod = null;
TimeSpan difference = TimeSpan.MaxValue;
moment = start;
foreach ( ITimePeriod period in periods )
{
// inside period
if ( period.HasInside( start ) )
{
nearestPeriod = period;
moment = start;
break;
}
// period out of range
if ( period.End < start )
{
continue;
}
// not the nearest
TimeSpan periodToMoment = period.Start - start;
if ( periodToMoment >= difference )
{
continue;
}
difference = periodToMoment;
nearestPeriod = period;
moment = nearestPeriod.Start;
}
return nearestPeriod;
} // FindNextPeriod
// ----------------------------------------------------------------------
// assumes no no overlapping periods in parameter periods
private static ITimePeriod FindPreviousPeriod( DateTime start, IEnumerable<ITimePeriod> periods, out DateTime moment )
{
ITimePeriod nearestPeriod = null;
TimeSpan difference = TimeSpan.MaxValue;
moment = start;
foreach ( ITimePeriod period in periods )
{
// inside period
if ( period.HasInside( start ) )
{
nearestPeriod = period;
moment = start;
break;
}
// period out of range
if ( period.Start > start )
{
continue;
}
// not the nearest
TimeSpan periodToMoment = start - period.End;
if ( periodToMoment >= difference )
{
continue;
}
difference = periodToMoment;
nearestPeriod = period;
moment = nearestPeriod.End;
}
return nearestPeriod;
} // FindPreviousPeriod
// ----------------------------------------------------------------------
// members
private readonly TimePeriodCollection includePeriods = new TimePeriodCollection();
private readonly TimePeriodCollection excludePeriods = new TimePeriodCollection();
} // class DateAdd
} // namespace Itenso.TimePeriod
// -- EOF -------------------------------------------------------------------