forked from bytecode77/bytecode-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTimeExtensions.cs
More file actions
294 lines (288 loc) · 15.4 KB
/
DateTimeExtensions.cs
File metadata and controls
294 lines (288 loc) · 15.4 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
using System;
using System.Globalization;
namespace BytecodeApi.Extensions
{
/// <summary>
/// Provides a set of <see langword="static" /> methods for interaction with <see cref="DateTime" /> objects.
/// </summary>
public static class DateTimeExtensions
{
/// <summary>
/// Converts the value of this <see cref="DateTime" /> to its equivalent <see cref="string" /> representation using a specified format and the invariant culture.
/// </summary>
/// <param name="dateTime">The <see cref="DateTime" /> value to convert.</param>
/// <param name="format">A <see cref="string" /> value specifying the format that is used to convert this <see cref="DateTime" />.</param>
/// <returns>
/// The equivalent <see cref="string" /> representation of this <see cref="DateTime" />.
/// </returns>
public static string ToStringInvariant(this DateTime dateTime, string format)
{
return dateTime.ToString(format, CultureInfo.InvariantCulture);
}
/// <summary>
/// Returns <see langword="null" />, if this <see cref="DateTime" /> object is <see langword="default" />(<see cref="DateTime" />), otherwise its original value.
/// </summary>
/// <param name="dateTime">The <see cref="DateTime" /> value to convert.</param>
/// <returns>
/// <see langword="null" />, if this <see cref="DateTime" /> object is <see langword="default" />(<see cref="DateTime" />);
/// otherwise, its original value.
/// </returns>
public static DateTime? ToNullIfDefault(this DateTime dateTime)
{
return dateTime == default ? (DateTime?)null : dateTime;
}
/// <summary>
/// Returns a new <see cref="DateTime" /> that subtracts the specified number of years from this <see cref="DateTime" /> value.
/// </summary>
/// <param name="dateTime">The original <see cref="DateTime" /> value.</param>
/// <param name="value">A <see cref="int" /> value specifying the years to be subtracted from this <see cref="DateTime" /> object.</param>
/// <returns>
/// A new <see cref="DateTime" /> object whose value is the difference of this <see cref="DateTime" /> value and the specified years.
/// </returns>
public static DateTime SubtractYears(this DateTime dateTime, int value)
{
return dateTime.AddYears(-value);
}
/// <summary>
/// Returns a new <see cref="DateTime" /> that subtracts the specified number of months from this <see cref="DateTime" /> value.
/// </summary>
/// <param name="dateTime">The original <see cref="DateTime" /> value.</param>
/// <param name="value">A <see cref="int" /> value specifying the months to be subtracted from this <see cref="DateTime" /> object.</param>
/// <returns>
/// A new <see cref="DateTime" /> object whose value is the difference of this <see cref="DateTime" /> value and the specified months.
/// </returns>
public static DateTime SubtractMonths(this DateTime dateTime, int value)
{
return dateTime.AddMonths(-value);
}
/// <summary>
/// Returns a new <see cref="DateTime" /> that subtracts the specified number of days from this <see cref="DateTime" /> value.
/// </summary>
/// <param name="dateTime">The original <see cref="DateTime" /> value.</param>
/// <param name="value">A <see cref="int" /> value specifying the days to be subtracted from this <see cref="DateTime" /> object.</param>
/// <returns>
/// A new <see cref="DateTime" /> object whose value is the difference of this <see cref="DateTime" /> value and the specified days.
/// </returns>
public static DateTime SubtractDays(this DateTime dateTime, double value)
{
return dateTime.AddDays(-value);
}
/// <summary>
/// Returns a new <see cref="DateTime" /> that subtracts the specified number of hours from this <see cref="DateTime" /> value.
/// </summary>
/// <param name="dateTime">The original <see cref="DateTime" /> value.</param>
/// <param name="value">A <see cref="int" /> value specifying the hours to be subtracted from this <see cref="DateTime" /> object.</param>
/// <returns>
/// A new <see cref="DateTime" /> object whose value is the difference of this <see cref="DateTime" /> value and the specified hours.
/// </returns>
public static DateTime SubtractHours(this DateTime dateTime, double value)
{
return dateTime.AddHours(-value);
}
/// <summary>
/// Returns a new <see cref="DateTime" /> that subtracts the specified number of minutes from this <see cref="DateTime" /> value.
/// </summary>
/// <param name="dateTime">The original <see cref="DateTime" /> value.</param>
/// <param name="value">A <see cref="int" /> value specifying the minutes to be subtracted from this <see cref="DateTime" /> object.</param>
/// <returns>
/// A new <see cref="DateTime" /> object whose value is the difference of this <see cref="DateTime" /> value and the specified minutes.
/// </returns>
public static DateTime SubtractMinutes(this DateTime dateTime, double value)
{
return dateTime.AddMinutes(-value);
}
/// <summary>
/// Returns a new <see cref="DateTime" /> that subtracts the specified number of seconds from this <see cref="DateTime" /> value.
/// </summary>
/// <param name="dateTime">The original <see cref="DateTime" /> value.</param>
/// <param name="value">A <see cref="int" /> value specifying the seconds to be subtracted from this <see cref="DateTime" /> object.</param>
/// <returns>
/// A new <see cref="DateTime" /> object whose value is the difference of this <see cref="DateTime" /> value and the specified seconds.
/// </returns>
public static DateTime SubtractSeconds(this DateTime dateTime, double value)
{
return dateTime.AddSeconds(-value);
}
/// <summary>
/// Returns a new <see cref="DateTime" /> that subtracts the specified number of milliseconds from this <see cref="DateTime" /> value.
/// </summary>
/// <param name="dateTime">The original <see cref="DateTime" /> value.</param>
/// <param name="value">A <see cref="int" /> value specifying the milliseconds to be subtracted from this <see cref="DateTime" /> object.</param>
/// <returns>
/// A new <see cref="DateTime" /> object whose value is the difference of this <see cref="DateTime" /> value and the specified milliseconds.
/// </returns>
public static DateTime SubtractMilliseconds(this DateTime dateTime, double value)
{
return dateTime.AddMilliseconds(-value);
}
/// <summary>
/// Returns a new <see cref="DateTime" /> that subtracts the specified number of ticks from this <see cref="DateTime" /> value.
/// </summary>
/// <param name="dateTime">The original <see cref="DateTime" /> value.</param>
/// <param name="value">A <see cref="int" /> value specifying the ticks to be subtracted from this <see cref="DateTime" /> object.</param>
/// <returns>
/// A new <see cref="DateTime" /> object whose value is the difference of this <see cref="DateTime" /> value and the specified ticks.
/// </returns>
public static DateTime SubtractTicks(this DateTime dateTime, long value)
{
return dateTime.AddTicks(-value);
}
/// <summary>
/// Returns a new <see cref="DateTime" /> that adds the specified number of business days to this <see cref="DateTime" /> value. Business days exclude Saturday and Sunday. The calculation is iterative. If <paramref name="days" /> is positive, days are added, otherwise days are subtracted.
/// <para>Example 1: Friday + 2 business days = Tuesday</para>
/// <para>Example 2: Monday - 2 business days = Thursday</para>
/// </summary>
/// <param name="dateTime">The original <see cref="DateTime" /> value.</param>
/// <param name="days">A <see cref="int" /> value specifying the business days to be added to this <see cref="DateTime" /> object.</param>
/// <returns>
/// A new <see cref="DateTime" /> object whose value is the sum of this <see cref="DateTime" /> value and the specified business days.
/// </returns>
public static DateTime AddBusinessDays(this DateTime dateTime, int days)
{
if (days != 0)
{
int sign = Math.Sign(days);
days = Math.Abs(days);
for (int i = 0; i < days; i++)
{
if (sign > 0)
{
switch (dateTime.DayOfWeek)
{
case DayOfWeek.Friday:
dateTime = dateTime.AddDays(3);
break;
case DayOfWeek.Saturday:
dateTime = dateTime.AddDays(2);
break;
default:
dateTime = dateTime.AddDays(1);
break;
}
}
else
{
switch (dateTime.DayOfWeek)
{
case DayOfWeek.Sunday:
dateTime = dateTime.SubtractDays(2);
break;
case DayOfWeek.Monday:
dateTime = dateTime.SubtractDays(3);
break;
default:
dateTime = dateTime.SubtractDays(1);
break;
}
}
}
}
return dateTime;
}
/// <summary>
/// Returns a new <see cref="DateTime" /> that subtracts the specified number of business days from this <see cref="DateTime" /> value. Business days exclude Saturday and Sunday. The calculation is iterative. If <paramref name="days" /> is positive, days are subtracted, otherwise days are added.
/// <para>Example 1: Friday + 2 business days = Tuesday</para>
/// <para>Example 2: Monday - 2 business days = Thursday</para>
/// </summary>
/// <param name="dateTime">The original <see cref="DateTime" /> value.</param>
/// <param name="days">A <see cref="int" /> value specifying the business days to be subtracted from this <see cref="DateTime" /> object.</param>
/// <returns>
/// A new <see cref="DateTime" /> object whose value is the difference of this <see cref="DateTime" /> value and the specified business days.
/// </returns>
public static DateTime SubtractBusinessDays(this DateTime dateTime, int days)
{
return dateTime.AddBusinessDays(-days);
}
/// <summary>
/// Computes the total count of business days between two <see cref="DateTime" /> instances. Business days exclude Saturday and Sunday. The time fraction is ignored and the returned value is inclusive.
/// <para>Example 1: Friday through Tuesday = 3 business days</para>
/// <para>Example 2: Saturday through Sunday = 0 business days</para>
/// </summary>
/// <param name="dateTime">The <see cref="DateTime" /> value to compare to <paramref name="value" />.</param>
/// <param name="value">The <see cref="DateTime" /> value to compare to this <see cref="DateTime" />. <paramref name="value" /> can be either less or greater than this <see cref="DateTime" /> value.</param>
/// <returns>
/// A <see cref="int" /> value representing the total count of business days between two <see cref="DateTime" /> instances.
/// </returns>
public static int GetTotalBusinessDays(this DateTime dateTime, DateTime value)
{
if (dateTime > value) CSharp.Swap(ref dateTime, ref value);
int count = 0;
for (DateTime i = dateTime.Date; i <= value.Date; i = i.AddDays(1))
{
if (i.DayOfWeek != DayOfWeek.Saturday && i.DayOfWeek != DayOfWeek.Sunday) count++;
}
return count;
}
/// <summary>
/// Compares the value of this <see cref="DateTime" /> instance to a specified <see cref="DateTime" /> value and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified <see cref="DateTime" /> value. The <paramref name="part" /> parameter specifies which fraction is considered during comparison.
/// </summary>
/// <param name="dateTime">The <see cref="DateTime" /> value to be compared to <paramref name="other" />.</param>
/// <param name="other">A <see cref="DateTime" /> to compare with <paramref name="dateTime" />.</param>
/// <param name="part">The <see cref="DateTimePart" /> specifying, which fraction is considered during comparison.</param>
/// <returns>
/// A value that indicates the relative order of the objects being compared considering only the specified <see cref="DateTimePart" />.
/// </returns>
public static int CompareTo(this DateTime dateTime, DateTime other, DateTimePart part)
{
return dateTime.GetPart(part).CompareTo(other.GetPart(part));
}
/// <summary>
/// Returns a new <see cref="DateTime" /> that represents a fraction of this <see cref="DateTime" /> value specified by the <paramref name="part" /> parameter.
/// </summary>
/// <param name="dateTime">The <see cref="DateTime" /> value to be stripped.</param>
/// <param name="part">The <see cref="DateTimePart" /> specifying, which fraction of <paramref name="dateTime" /> is returned.</param>
/// <returns>
/// A new <see cref="DateTime" /> that represents a fraction of this <see cref="DateTime" /> value specified by the <paramref name="part" /> parameter.
/// </returns>
public static DateTime GetPart(this DateTime dateTime, DateTimePart part)
{
switch (part)
{
case DateTimePart.Full: return dateTime;
case DateTimePart.DateTimeWithSeconds: return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Kind);
case DateTimePart.DateTime: return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, 0, dateTime.Kind);
case DateTimePart.Date: return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, dateTime.Kind);
case DateTimePart.YearMonth: return new DateTime(dateTime.Year, dateTime.Month, 1, 0, 0, 0, dateTime.Kind);
case DateTimePart.Year: return new DateTime(dateTime.Year, 1, 1, 0, 0, 0, dateTime.Kind);
default: throw Throw.InvalidEnumArgument(nameof(part), part);
}
}
/// <summary>
/// Returns a new <see cref="DateTime" /> representing the first day of the week according to the current culture.
/// </summary>
/// <param name="dateTime">The original <see cref="DateTime" /> value.</param>
/// <returns>
/// A new <see cref="DateTime" /> object representing the first day of the week according to the current culture.
/// </returns>
public static DateTime GetFirstDayOfWeek(this DateTime dateTime)
{
return GetFirstDayOfWeek(dateTime, CultureInfo.CurrentCulture);
}
/// <summary>
/// Returns a new <see cref="DateTime" /> representing the first day of the week using specified culture-specific calendar rules.
/// </summary>
/// <param name="dateTime">The original <see cref="DateTime" /> value.</param>
/// <param name="culture">An object that supplies culture-specific calendar rules.</param>
/// <returns>
/// A new <see cref="DateTime" /> object representing the first day of the week according to <paramref name="culture" />.
/// </returns>
public static DateTime GetFirstDayOfWeek(this DateTime dateTime, CultureInfo culture)
{
Check.ArgumentNull(culture, nameof(culture));
return dateTime.GetFirstDayOfWeek(culture.DateTimeFormat.FirstDayOfWeek);
}
/// <summary>
/// Returns a new <see cref="DateTime" /> representing the first day of the week, according to the <paramref name="firstDayOfWeek" /> parameter.
/// </summary>
/// <param name="dateTime">The original <see cref="DateTime" /> value.</param>
/// <param name="firstDayOfWeek">The first day of week.</param>
/// <returns>
/// A new <see cref="DateTime" /> object representing the first day of the week, according to the <paramref name="firstDayOfWeek" /> parameter.
/// </returns>
public static DateTime GetFirstDayOfWeek(this DateTime dateTime, DayOfWeek firstDayOfWeek)
{
while (dateTime.DayOfWeek != firstDayOfWeek) dateTime = dateTime.SubtractDays(1);
return dateTime.Date;
}
}
}