@@ -173,59 +173,87 @@ public static DateTime StartOfSecond(this DateTime date) {
173
173
}
174
174
175
175
public static DateTime EndOfSecond ( this DateTime date ) {
176
- return date . StartOfSecond ( ) . AddSeconds ( 1 ) . SubtractMilliseconds ( 1 ) ;
176
+ var value = date . StartOfSecond ( ) . SafeAdd ( TimeSpan . FromSeconds ( 1 ) ) ;
177
+ if ( value == DateTime . MaxValue )
178
+ return value ;
179
+
180
+ return value . SubtractMilliseconds ( 1 ) ;
177
181
}
178
182
179
183
public static DateTime StartOfMinute ( this DateTime date ) {
180
184
return date . Floor ( TimeSpan . FromMinutes ( 1 ) ) ;
181
185
}
182
186
183
187
public static DateTime EndOfMinute ( this DateTime date ) {
184
- return date . StartOfMinute ( ) . AddMinutes ( 1 ) . SubtractMilliseconds ( 1 ) ;
188
+ var value = date . StartOfMinute ( ) . SafeAdd ( TimeSpan . FromMinutes ( 1 ) ) ;
189
+ if ( value == DateTime . MaxValue )
190
+ return value ;
191
+
192
+ return value . SubtractMilliseconds ( 1 ) ;
185
193
}
186
194
187
195
public static DateTime StartOfHour ( this DateTime date ) {
188
196
return date . Floor ( TimeSpan . FromHours ( 1 ) ) ;
189
197
}
190
198
191
199
public static DateTime EndOfHour ( this DateTime date ) {
192
- return date . StartOfHour ( ) . AddHours ( 1 ) . SubtractMilliseconds ( 1 ) ;
200
+ var value = date . StartOfHour ( ) . SafeAdd ( TimeSpan . FromHours ( 1 ) ) ;
201
+ if ( value == DateTime . MaxValue )
202
+ return value ;
203
+
204
+ return value . SubtractMilliseconds ( 1 ) ;
193
205
}
194
206
195
207
public static DateTime StartOfDay ( this DateTime date ) {
196
208
return date . Date ;
197
209
}
198
210
199
211
public static DateTime EndOfDay ( this DateTime date ) {
200
- return date . Date . AddDays ( 1 ) . SubtractMilliseconds ( 1 ) ;
212
+ var value = date . Date . SafeAdd ( TimeSpan . FromDays ( 1 ) ) ;
213
+ if ( value == DateTime . MaxValue )
214
+ return value ;
215
+
216
+ return value . SubtractMilliseconds ( 1 ) ;
201
217
}
202
218
203
219
public static DateTime StartOfWeek ( this DateTime date , DayOfWeek startOfWeek = DayOfWeek . Sunday ) {
204
220
int diff = date . DayOfWeek - startOfWeek ;
205
221
if ( diff < 0 )
206
222
diff += 7 ;
207
223
208
- return date . Date . AddDays ( - 1 * diff ) ;
224
+ return date . Date . SafeSubtract ( TimeSpan . FromDays ( diff ) ) ;
209
225
}
210
226
211
227
public static DateTime EndOfWeek ( this DateTime date , DayOfWeek startOfWeek = DayOfWeek . Sunday ) {
212
- return date . StartOfWeek ( startOfWeek ) . AddWeeks ( 1 ) . SubtractMilliseconds ( 1 ) ;
228
+ var value = date . StartOfWeek ( startOfWeek ) . AddWeeks ( 1 ) ;
229
+ if ( value == DateTime . MaxValue )
230
+ return value ;
231
+
232
+ return value . SubtractMilliseconds ( 1 ) ;
213
233
}
214
234
215
235
public static DateTime StartOfMonth ( this DateTime date ) {
216
- return date . Date . AddDays ( 1 - date . Date . Day ) ;
236
+ return date . Date . SafeSubtract ( TimeSpan . FromDays ( date . Date . Day - 1 ) ) ;
217
237
}
218
238
219
239
public static DateTime EndOfMonth ( this DateTime date ) {
220
- return date . StartOfMonth ( ) . AddMonths ( 1 ) . SubtractMilliseconds ( 1 ) ;
240
+ var value = date . StartOfMonth ( ) . AddMonths ( 1 ) ;
241
+ if ( value == DateTime . MaxValue )
242
+ return value ;
243
+
244
+ return value . SubtractMilliseconds ( 1 ) ;
221
245
}
222
246
223
247
public static DateTime StartOfYear ( this DateTime date ) {
224
- return date . Date . AddDays ( 1 - date . Date . Day ) . AddMonths ( 1 - date . Date . Month ) ;
248
+ return date . StartOfMonth ( ) . SubtractMonths ( date . Date . Month - 1 ) ;
225
249
}
226
250
227
251
public static DateTime EndOfYear ( this DateTime date ) {
228
- return date . StartOfYear ( ) . AddYears ( 1 ) . SubtractMilliseconds ( 1 ) ;
252
+ var value = date . StartOfYear ( ) . AddYears ( 1 ) ;
253
+ if ( value == DateTime . MaxValue )
254
+ return value ;
255
+
256
+ return value . SubtractMilliseconds ( 1 ) ;
229
257
}
230
258
231
259
public static DateTime Floor ( this DateTime date , TimeSpan interval ) {
@@ -242,35 +270,35 @@ public static DateTime Round(this DateTime date, TimeSpan roundingInterval) {
242
270
}
243
271
244
272
public static DateTime NextSecond ( this DateTime date ) {
245
- return date . AddSeconds ( 1 ) ;
273
+ return date . SafeAdd ( TimeSpan . FromSeconds ( 1 ) ) ;
246
274
}
247
275
248
276
public static DateTime LastSecond ( this DateTime date ) {
249
- return date . SubtractSeconds ( 1 ) ;
277
+ return date . SafeSubtract ( TimeSpan . FromSeconds ( 1 ) ) ;
250
278
}
251
279
252
280
public static DateTime NextMinute ( this DateTime date ) {
253
- return date . AddMinutes ( 1 ) ;
281
+ return date . SafeAdd ( TimeSpan . FromMinutes ( 1 ) ) ;
254
282
}
255
283
256
284
public static DateTime LastMinute ( this DateTime date ) {
257
- return date . SubtractMinutes ( 1 ) ;
285
+ return date . SafeSubtract ( TimeSpan . FromMinutes ( 1 ) ) ;
258
286
}
259
287
260
288
public static DateTime NextHour ( this DateTime date ) {
261
- return date . AddHours ( 1 ) ;
289
+ return date . SafeAdd ( TimeSpan . FromHours ( 1 ) ) ;
262
290
}
263
291
264
292
public static DateTime LastHour ( this DateTime date ) {
265
- return date . SubtractHours ( 1 ) ;
293
+ return date . SafeSubtract ( TimeSpan . FromHours ( 1 ) ) ;
266
294
}
267
295
268
296
public static DateTime NextDay ( this DateTime date ) {
269
- return date . AddDays ( 1 ) ;
297
+ return date . SafeAdd ( TimeSpan . FromDays ( 1 ) ) ;
270
298
}
271
299
272
300
public static DateTime LastDay ( this DateTime date ) {
273
- return date . SubtractDays ( 1 ) ;
301
+ return date . SafeSubtract ( TimeSpan . FromDays ( 1 ) ) ;
274
302
}
275
303
276
304
public static DateTime NextWeek ( this DateTime date ) {
@@ -301,53 +329,53 @@ public static DateTime SubtractTicks(this DateTime date, long value) {
301
329
if ( value < 0 )
302
330
throw new ArgumentException ( "Value cannot be less than 0." , nameof ( value ) ) ;
303
331
304
- return date . AddTicks ( value * - 1 ) ;
332
+ return date . SafeSubtract ( TimeSpan . FromTicks ( value ) ) ;
305
333
}
306
334
307
335
public static DateTime SubtractMilliseconds ( this DateTime date , double value ) {
308
336
if ( value < 0 )
309
337
throw new ArgumentException ( "Value cannot be less than 0." , nameof ( value ) ) ;
310
338
311
- return date . AddMilliseconds ( value * - 1 ) ;
339
+ return date . SafeSubtract ( TimeSpan . FromMilliseconds ( value ) ) ;
312
340
}
313
341
314
342
public static DateTime SubtractSeconds ( this DateTime date , double value ) {
315
343
if ( value < 0 )
316
344
throw new ArgumentException ( "Value cannot be less than 0." , nameof ( value ) ) ;
317
345
318
- return date . AddSeconds ( value * - 1 ) ;
346
+ return date . SafeSubtract ( TimeSpan . FromSeconds ( value ) ) ;
319
347
}
320
348
321
349
public static DateTime SubtractMinutes ( this DateTime date , double value ) {
322
350
if ( value < 0 )
323
351
throw new ArgumentException ( "Value cannot be less than 0." , nameof ( value ) ) ;
324
352
325
- return date . AddMinutes ( value * - 1 ) ;
353
+ return date . SafeSubtract ( TimeSpan . FromMinutes ( value ) ) ;
326
354
}
327
355
328
356
public static DateTime SubtractHours ( this DateTime date , double value ) {
329
357
if ( value < 0 )
330
358
throw new ArgumentException ( "Value cannot be less than 0." , nameof ( value ) ) ;
331
359
332
- return date . AddHours ( value * - 1 ) ;
360
+ return date . SafeSubtract ( TimeSpan . FromHours ( value ) ) ;
333
361
}
334
362
335
363
public static DateTime SubtractDays ( this DateTime date , double value ) {
336
364
if ( value < 0 )
337
365
throw new ArgumentException ( "Value cannot be less than 0." , nameof ( value ) ) ;
338
366
339
- return date . AddDays ( value * - 1 ) ;
367
+ return date . SafeSubtract ( TimeSpan . FromDays ( value ) ) ;
340
368
}
341
369
342
370
public static DateTime AddWeeks ( this DateTime date , double value ) {
343
- return date . AddDays ( value * 7 ) ;
371
+ return date . SafeAdd ( TimeSpan . FromDays ( value * 7 ) ) ;
344
372
}
345
373
346
374
public static DateTime SubtractWeeks ( this DateTime date , double value ) {
347
375
if ( value < 0 )
348
376
throw new ArgumentException ( "Value cannot be less than 0." , nameof ( value ) ) ;
349
377
350
- return date . AddWeeks ( value * - 1 ) ;
378
+ return date . SafeSubtract ( TimeSpan . FromDays ( value * 7 ) ) ;
351
379
}
352
380
353
381
public static DateTime SubtractMonths ( this DateTime date , int months ) {
0 commit comments