2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
4
5
- namespace Exceptionless . DateTimeExtensions
6
- {
5
+ namespace Exceptionless . DateTimeExtensions {
7
6
/// <summary>
8
7
/// A class representing a business week.
9
8
/// </summary>
10
- public class BusinessWeek
11
- {
9
+ public class BusinessWeek {
12
10
/// <summary>
13
11
/// Initializes a new instance of the <see cref="BusinessWeek"/> class.
14
12
/// </summary>
15
- public BusinessWeek ( )
16
- {
13
+ public BusinessWeek ( ) {
17
14
BusinessDays = new List < BusinessDay > ( ) ;
18
15
}
19
16
@@ -30,8 +27,7 @@ public BusinessWeek()
30
27
/// <returns>
31
28
/// <c>true</c> if the specified date falls on a business day; otherwise, <c>false</c>.
32
29
/// </returns>
33
- public bool IsBusinessDay ( DateTime date )
34
- {
30
+ public bool IsBusinessDay ( DateTime date ) {
35
31
return BusinessDays . Any ( day => day . IsBusinessDay ( date ) ) ;
36
32
}
37
33
@@ -47,20 +43,16 @@ public bool IsBusinessDay(DateTime date)
47
43
/// Business time is calculated by adding only the time that falls inside the business day range.
48
44
/// If all the time between the start and end date fall outside the business day, the time will be zero.
49
45
/// </remarks>
50
- public TimeSpan GetBusinessTime ( DateTime startDate , DateTime endDate )
51
- {
46
+ public TimeSpan GetBusinessTime ( DateTime startDate , DateTime endDate ) {
52
47
Validate ( true ) ;
53
48
54
49
var businessTime = TimeSpan . Zero ;
55
50
var workingDate = startDate ;
56
51
57
- while ( workingDate < endDate )
58
- {
59
- DateTime businessStart ;
60
- BusinessDay businessDay ;
52
+ while ( workingDate < endDate ) {
61
53
62
54
// get start date
63
- if ( ! NextBusinessDay ( workingDate , out businessStart , out businessDay ) )
55
+ if ( ! NextBusinessDay ( workingDate , out var businessStart , out var businessDay ) )
64
56
break ;
65
57
66
58
// business start after end date
@@ -70,11 +62,10 @@ public TimeSpan GetBusinessTime(DateTime startDate, DateTime endDate)
70
62
if ( businessDay == null )
71
63
break ;
72
64
73
- TimeSpan timeToEndOfDay = businessDay . EndTime . Subtract ( businessStart . TimeOfDay ) ;
74
- DateTime businessEnd = businessStart . SafeAdd ( timeToEndOfDay ) ;
65
+ var timeToEndOfDay = businessDay . EndTime . Subtract ( businessStart . TimeOfDay ) ;
66
+ var businessEnd = businessStart . SafeAdd ( timeToEndOfDay ) ;
75
67
76
- if ( endDate <= businessEnd )
77
- {
68
+ if ( endDate <= businessEnd ) {
78
69
timeToEndOfDay = endDate . TimeOfDay . Subtract ( businessStart . TimeOfDay ) ;
79
70
businessTime = businessTime . Add ( timeToEndOfDay ) ;
80
71
return businessTime ;
@@ -94,23 +85,19 @@ public TimeSpan GetBusinessTime(DateTime startDate, DateTime endDate)
94
85
/// <param name="startDate">The start date.</param>
95
86
/// <param name="businessTime">The business time.</param>
96
87
/// <returns></returns>
97
- public DateTime GetBusinessEndDate ( DateTime startDate , TimeSpan businessTime )
98
- {
88
+ public DateTime GetBusinessEndDate ( DateTime startDate , TimeSpan businessTime ) {
99
89
Validate ( true ) ;
100
90
101
91
var endDate = startDate ;
102
92
var remainingTime = businessTime ;
103
93
104
- while ( remainingTime > TimeSpan . Zero )
105
- {
106
- DateTime businessStart ;
107
- BusinessDay businessDay ;
94
+ while ( remainingTime > TimeSpan . Zero ) {
108
95
109
96
// get start date
110
- if ( ! NextBusinessDay ( endDate , out businessStart , out businessDay ) )
97
+ if ( ! NextBusinessDay ( endDate , out var businessStart , out var businessDay ) )
111
98
break ;
112
99
113
- TimeSpan timeForDay = businessDay . EndTime . Subtract ( businessStart . TimeOfDay ) ;
100
+ var timeForDay = businessDay . EndTime . Subtract ( businessStart . TimeOfDay ) ;
114
101
if ( remainingTime <= timeForDay )
115
102
return businessStart . SafeAdd ( remainingTime ) ;
116
103
@@ -127,8 +114,7 @@ public DateTime GetBusinessEndDate(DateTime startDate, TimeSpan businessTime)
127
114
/// </summary>
128
115
/// <param name="throwExcption">if set to <c>true</c> throw excption if invalid.</param>
129
116
/// <returns><c>true</c> if valid; otherwise <c>false</c>.</returns>
130
- protected virtual bool Validate ( bool throwExcption )
131
- {
117
+ protected virtual bool Validate ( bool throwExcption ) {
132
118
if ( BusinessDays . Any ( ) )
133
119
return true ;
134
120
@@ -138,38 +124,33 @@ protected virtual bool Validate(bool throwExcption)
138
124
return false ;
139
125
}
140
126
141
- internal bool NextBusinessDay ( DateTime startDate , out DateTime nextDate , out BusinessDay businessDay )
142
- {
127
+ internal bool NextBusinessDay ( DateTime startDate , out DateTime nextDate , out BusinessDay businessDay ) {
143
128
nextDate = startDate ;
144
129
businessDay = null ;
145
130
146
131
var tree = GetDayTree ( ) ;
147
132
148
133
// loop no more then 7 times
149
- for ( int x = 0 ; x < 7 ; x ++ )
150
- {
151
- DayOfWeek dayOfWeek = nextDate . DayOfWeek ;
134
+ for ( int x = 0 ; x < 7 ; x ++ ) {
135
+ var dayOfWeek = nextDate . DayOfWeek ;
152
136
153
- if ( ! tree . ContainsKey ( dayOfWeek ) )
154
- {
137
+ if ( ! tree . ContainsKey ( dayOfWeek ) ) {
155
138
// no business days on this day of the week
156
139
nextDate = nextDate . AddDays ( 1 ) . Date ;
157
140
continue ;
158
141
}
159
142
160
- IList < BusinessDay > businessDays = tree [ dayOfWeek ] ;
143
+ var businessDays = tree [ dayOfWeek ] ;
161
144
if ( businessDays == null )
162
145
continue ;
163
146
164
- foreach ( BusinessDay day in businessDays )
165
- {
147
+ foreach ( var day in businessDays ) {
166
148
if ( day == null )
167
149
continue ;
168
150
169
- TimeSpan timeOfDay = nextDate . TimeOfDay ;
151
+ var timeOfDay = nextDate . TimeOfDay ;
170
152
171
- if ( timeOfDay >= day . StartTime && timeOfDay < day . EndTime )
172
- {
153
+ if ( timeOfDay >= day . StartTime && timeOfDay < day . EndTime ) {
173
154
// working date in range
174
155
businessDay = day ;
175
156
return true ;
@@ -196,19 +177,14 @@ internal bool NextBusinessDay(DateTime startDate, out DateTime nextDate, out Bus
196
177
197
178
private Dictionary < DayOfWeek , IList < BusinessDay > > _dayTree ;
198
179
199
- private Dictionary < DayOfWeek , IList < BusinessDay > > GetDayTree ( )
200
- {
180
+ private Dictionary < DayOfWeek , IList < BusinessDay > > GetDayTree ( ) {
201
181
if ( _dayTree != null )
202
182
return _dayTree ;
203
183
204
184
_dayTree = new Dictionary < DayOfWeek , IList < BusinessDay > > ( ) ;
205
- var days = BusinessDays
206
- . OrderBy ( b => b . DayOfWeek )
207
- . ThenBy ( b => b . StartTime )
208
- . ToList ( ) ;
185
+ var days = BusinessDays . OrderBy ( b => b . DayOfWeek ) . ThenBy ( b => b . StartTime ) . ToList ( ) ;
209
186
210
- foreach ( var day in days )
211
- {
187
+ foreach ( var day in days ) {
212
188
if ( ! _dayTree . ContainsKey ( day . DayOfWeek ) )
213
189
_dayTree . Add ( day . DayOfWeek , new List < BusinessDay > ( ) ) ;
214
190
@@ -226,16 +202,14 @@ private Dictionary<DayOfWeek, IList<BusinessDay>> GetDayTree()
226
202
/// <summary>
227
203
/// Nested class to lazy-load singleton.
228
204
/// </summary>
229
- private class Nested
230
- {
205
+ private class Nested {
231
206
/// <summary>
232
207
/// Initializes the Nested class.
233
208
/// </summary>
234
209
/// <remarks>
235
210
/// Explicit static constructor to tell C# compiler not to mark type as beforefieldinit.
236
211
/// </remarks>
237
- static Nested ( )
238
- {
212
+ static Nested ( ) {
239
213
Current = new BusinessWeek ( ) ;
240
214
Current . BusinessDays . Add ( new BusinessDay ( DayOfWeek . Monday ) ) ;
241
215
Current . BusinessDays . Add ( new BusinessDay ( DayOfWeek . Tuesday ) ) ;
@@ -247,7 +221,7 @@ static Nested()
247
221
/// <summary>
248
222
/// Current singleton instance.
249
223
/// </summary>
250
- internal readonly static BusinessWeek Current ;
224
+ internal static readonly BusinessWeek Current ;
251
225
}
252
226
}
253
227
}
0 commit comments