-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDate.cs
More file actions
195 lines (160 loc) · 6.36 KB
/
Copy pathDate.cs
File metadata and controls
195 lines (160 loc) · 6.36 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
/* Date */
using System;
using System.Globalization;
namespace PayrollEngine.Client.Scripting;
/// <summary>Date specifications</summary>
public static class Date
{
/// <summary>First month in year</summary>
public static readonly int FirstMonthOfCalendarYear = 1;
/// <summary>First day in month</summary>
public static readonly int FirstDayOfMonth = 1;
/// <summary>Number of months in a year</summary>
public static readonly int MonthsInYear = 12;
/// <summary>Last month in year</summary>
public static readonly int LastMonthOfCalendarYear = MonthsInYear;
/// <summary>Number of days in a week</summary>
public static readonly int DaysInWeek = 7;
/// <summary>Represents the smallest possible value of a time instant</summary>
public static DateTime MinValue => DateTime.MinValue.ToUtc();
/// <summary>Represents the largest possible value of a time instant</summary>
public static DateTime MaxValue => DateTime.MaxValue.ToUtc();
/// <summary>Gets a time instant that is set to the current date and time</summary>
public static DateTime Now => DateTime.UtcNow;
/// <summary>Gets a time instant that is set to the current day</summary>
public static DateTime Today => Now.Date;
/// <summary>Gets a time instant that is set to the next day</summary>
/// <remarks>Computed property (not static readonly) so the value updates with Today.</remarks>
public static DateTime Tomorrow => Today.AddDays(1);
/// <summary>Gets a time instant that is set to the previous day</summary>
/// <remarks>Computed property (not static readonly) so the value updates with Today.</remarks>
public static DateTime Yesterday => Today.AddDays(-1);
/// <summary>Get the year start date in UTC</summary>
public static DateTime YearStart(int year) =>
new(year, 1, 1, 0, 0, 0, DateTimeKind.Utc);
/// <summary>Get the year-end date in UTC</summary>
public static DateTime YearEnd(int year) =>
YearStart(year).AddYears(1).AddTicks(-1);
/// <summary>Get the month start date in UTC</summary>
public static DateTime MonthStart(int year, int month) =>
new(year, month, 1, 0, 0, 0, DateTimeKind.Utc);
/// <summary>Get the month end date in UTC</summary>
public static DateTime MonthEnd(int year, int month) =>
MonthStart(year, month).AddMonths(1).AddTicks(-1);
/// <summary>Get the day start date and time in UTC</summary>
public static DateTime DayStart(int year, int month, int day) =>
new(year, month, day, 0, 0, 0, DateTimeKind.Utc);
/// <summary>Get the day end date and time in UTC</summary>
public static DateTime DayEnd(int year, int month, int day) =>
DayStart(year, month, day).AddDays(1).AddTicks(-1);
/// <summary>Get the minimum date</summary>
public static DateTime Min(DateTime left, DateTime right) =>
left < right ? left : right;
/// <summary>Get the maximum date</summary>
public static DateTime Max(DateTime left, DateTime right) =>
left > right ? left : right;
/// <summary>Get the minimum timespan</summary>
public static TimeSpan Min(TimeSpan left, TimeSpan right) =>
left < right ? left : right;
/// <summary>Get the maximum timespan</summary>
public static TimeSpan Max(TimeSpan left, TimeSpan right) =>
left > right ? left : right;
#region Convert
/// <summary>Parse date time string</summary>
/// <param name="dateValue">The date value</param>
/// <returns>Valid date or null</returns>
public static DateTime? Parse(string dateValue)
{
if (string.IsNullOrWhiteSpace(dateValue))
{
return null;
}
dateValue = dateValue.Trim('"');
// predefined constants
var dateTime = ParsePredefined(dateValue);
if (dateTime != null)
{
return dateTime;
}
// offset
dateTime = ParseOffset(dateValue);
if (dateTime != null)
{
return dateTime;
}
// date time parsing
if (DateTime.TryParse(dateValue, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out var parameter))
{
return parameter;
}
return null;
}
private static DateTime? ParseOffset(string dateValue)
{
// offset
if (!dateValue.StartsWith("offset:", StringComparison.InvariantCultureIgnoreCase))
{
return null;
}
var offset = dateValue.Substring("offset:".Length);
if (string.IsNullOrWhiteSpace(offset))
{
return null;
}
// count
var countText = offset.Substring(0, offset.Length - 1).TrimStart('+');
if (!int.TryParse(countText, out var count))
{
return null;
}
// offset with count
switch (offset[^1])
{
// days
case 'd':
return Today.AddDays(count);
// weeks
case 'w':
return Today.AddDays(DaysInWeek * count);
// months
case 'm':
return Today.AddMonths(count);
// years
case 'y':
return Today.AddYears(count);
}
return null;
}
private static DateTime? ParsePredefined(string dateValue)
{
switch (dateValue.ToLower())
{
// ReSharper disable StringLiteralTypo
case "yesterday":
return Yesterday;
case "today":
return Today;
case "tomorrow":
return Tomorrow;
case "previousmonth":
var previousMonth = Today.AddMonths(-1);
return new(previousMonth.Year, previousMonth.Month, 1);
case "month":
var month = Today;
return new(month.Year, month.Month, 1);
case "nextmonth":
var nextMonth = Today.AddMonths(1);
return new(nextMonth.Year, nextMonth.Month, 1);
case "previousyear":
return new(Today.AddYears(-1).Year, 1, 1);
case "year":
return new(Today.Year, 1, 1);
case "nextyear":
return new(Today.AddYears(1).Year, 1, 1);
default:
return null;
// ReSharper restore StringLiteralTypo
}
}
#endregion
}