-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateTimeExtensions.cs
45 lines (41 loc) · 1.83 KB
/
DateTimeExtensions.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
using System;
namespace CK.Core;
public static class DateTimeExtensions
{
/// <summary>
/// Adds a <see cref="WeakTimeSpan"/> to this DateTime.
/// <para>
/// If <see cref="WeakTimeSpan.IsValid"/> is false, this DateTime is returned unchanged.
/// </para>
/// </summary>
/// <param name="dateTime">This DateTime.</param>
/// <param name="span">The span to add.</param>
/// <returns>The offsetted DateTime.</returns>
public static DateTime Add( this DateTime dateTime, WeakTimeSpan span ) => DoAdd( dateTime, span.Unit, span.Count );
/// <summary>
/// Substract a <see cref="WeakTimeSpan"/> to this DateTime.
/// <para>
/// If <see cref="WeakTimeSpan.IsValid"/> is false, this DateTime is returned unchanged.
/// </para>
/// </summary>
/// <param name="dateTime">This DateTime.</param>
/// <param name="span">The span to add.</param>
/// <returns>The offsetted DateTime.</returns>
public static DateTime Substract( this DateTime dateTime, WeakTimeSpan span ) => DoAdd( dateTime, span.Unit, -span.Count );
internal static DateTime DoAdd( DateTime dateTime, TimeSpanUnit unit, long count )
{
return unit switch
{
TimeSpanUnit.Year => dateTime.AddYears( (int)count ),
TimeSpanUnit.Semester => dateTime.AddMonths( 6 * (int)count ),
TimeSpanUnit.Quarter => dateTime.AddMonths( 3 * (int)count ),
TimeSpanUnit.Month => dateTime.AddMonths( (int)count ),
TimeSpanUnit.Day => dateTime.AddDays( count ),
TimeSpanUnit.Hour => dateTime.AddHours( count ),
TimeSpanUnit.Minute => dateTime.AddMinutes( count ),
TimeSpanUnit.Second => dateTime.AddSeconds( count ),
TimeSpanUnit.Millisecond => dateTime.AddMilliseconds( count ),
_ => dateTime
};
}
}