/
DateMathTime.cs
362 lines (317 loc) · 12.5 KB
/
DateMathTime.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using OpenSearch.Net.Utf8Json;
namespace OpenSearch.Client
{
/// <summary>
/// A time representation for use within <see cref="DateMath" /> expressions.
/// </summary>
[JsonFormatter(typeof(DateMathTimeFormatter))]
public class DateMathTime : IComparable<DateMathTime>, IEquatable<DateMathTime>
{
private const double MillisecondsInADay = MillisecondsInAnHour * 24;
private const double MillisecondsInAMinute = MillisecondsInASecond * 60;
private const double MillisecondsInAMonthApproximate = MillisecondsInAYearApproximate / MonthsInAYear;
private const double MillisecondsInAnHour = MillisecondsInAMinute * 60;
private const double MillisecondsInASecond = 1000;
private const double MillisecondsInAWeek = MillisecondsInADay * 7;
private const double MillisecondsInAYearApproximate = MillisecondsInADay * 365;
private const int MonthsInAYear = 12;
private static readonly Regex ExpressionRegex =
new Regex(@"^
(?<factor>[+\-]? # open factor capture, allowing optional +- signs
(?:(?#numeric)(?:\d+(?:\.\d*)?)|(?:\.\d+)) #a numeric in the forms: (N, N., .N, N.N)
(?:(?#exponent)e[+\-]?\d+)? #an optional exponential scientific component, E also matches here (IgnoreCase)
) # numeric and exponent fall under the factor capture
\s{0,10} #optional spaces (sanity checked for max 10 repetitions)
(?<interval>(?:y|w|d|h|m|s)) #interval indicator
$",
RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
private double _approximateSeconds;
/// <summary>
/// Instantiates a new instance of <see cref="DateMathTime" /> from a TimeSpan.
/// Rounding can be specified to determine how fractional second values should be rounded.
/// </summary>
public DateMathTime(TimeSpan timeSpan, MidpointRounding rounding = MidpointRounding.AwayFromZero)
: this(timeSpan.TotalMilliseconds, rounding) { }
/// <summary>
/// Instantiates a new instance of <see cref="DateMathTime" /> from a milliseconds value.
/// Rounding can be specified to determine how fractional second values should be rounded.
/// </summary>
public DateMathTime(double milliseconds, MidpointRounding rounding = MidpointRounding.AwayFromZero) =>
SetWholeFactorIntervalAndSeconds(milliseconds, rounding);
/// <summary>
/// Instantiates a new instance of <see cref="DateMathTime" /> from a factor and interval.
/// </summary>
public DateMathTime(int factor, DateMathTimeUnit interval) =>
SetWholeFactorIntervalAndSeconds(factor, interval, MidpointRounding.AwayFromZero);
/// <summary>
/// Instantiates a new instance of <see cref="DateMathTime" /> from the timeUnit string expression.
/// Rounding can be specified to determine how fractional second values should be rounded.
/// </summary>
public DateMathTime(string timeUnit, MidpointRounding rounding = MidpointRounding.AwayFromZero)
{
if (timeUnit == null) throw new ArgumentNullException(nameof(timeUnit));
if (timeUnit.Length == 0) throw new ArgumentException("Expression string is empty", nameof(timeUnit));
var match = ExpressionRegex.Match(timeUnit);
if (!match.Success) throw new ArgumentException($"Expression '{timeUnit}' string is invalid", nameof(timeUnit));
var factor = match.Groups["factor"].Value;
if (!double.TryParse(factor, NumberStyles.Any, CultureInfo.InvariantCulture, out var fraction))
throw new ArgumentException($"Expression '{timeUnit}' contains invalid factor: {factor}", nameof(timeUnit));
var intervalValue = match.Groups["interval"].Value;
DateMathTimeUnit interval;
switch (intervalValue)
{
case "M":
interval = DateMathTimeUnit.Month;
break;
case "m":
interval = DateMathTimeUnit.Minute;
break;
default:
interval = intervalValue.ToEnum<DateMathTimeUnit>().GetValueOrDefault();
break;
}
SetWholeFactorIntervalAndSeconds(fraction, interval, rounding);
}
/// <summary>
/// The numeric time factor
/// </summary>
public int Factor { get; private set; }
/// <summary>
/// The time units
/// </summary>
public DateMathTimeUnit Interval { get; private set; }
public int CompareTo(DateMathTime other)
{
if (other == null) return 1;
if (Math.Abs(_approximateSeconds - other._approximateSeconds) < double.Epsilon) return 0;
if (_approximateSeconds < other._approximateSeconds) return -1;
return 1;
}
public bool Equals(DateMathTime other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Math.Abs(_approximateSeconds - other._approximateSeconds) < double.Epsilon;
}
public static implicit operator DateMathTime(TimeSpan span) => new DateMathTime(span);
public static implicit operator DateMathTime(double milliseconds) => new DateMathTime(milliseconds);
public static implicit operator DateMathTime(string expression) => new DateMathTime(expression);
private void SetWholeFactorIntervalAndSeconds(double factor, DateMathTimeUnit interval, MidpointRounding rounding)
{
var fraction = factor;
double milliseconds;
// if the factor is already a whole number then use it
if (TryGetIntegerGreaterThanZero(fraction, out var whole))
{
Factor = whole;
Interval = interval;
switch (interval)
{
case DateMathTimeUnit.Second:
_approximateSeconds = whole;
break;
case DateMathTimeUnit.Minute:
_approximateSeconds = whole * (MillisecondsInAMinute / MillisecondsInASecond);
break;
case DateMathTimeUnit.Hour:
_approximateSeconds = whole * (MillisecondsInAnHour / MillisecondsInASecond);
break;
case DateMathTimeUnit.Day:
_approximateSeconds = whole * (MillisecondsInADay / MillisecondsInASecond);
break;
case DateMathTimeUnit.Week:
_approximateSeconds = whole * (MillisecondsInAWeek / MillisecondsInASecond);
break;
case DateMathTimeUnit.Month:
_approximateSeconds = whole * (MillisecondsInAMonthApproximate / MillisecondsInASecond);
break;
case DateMathTimeUnit.Year:
_approximateSeconds = whole * (MillisecondsInAYearApproximate / MillisecondsInASecond);
break;
default:
throw new ArgumentOutOfRangeException(nameof(interval), interval, null);
}
return;
}
switch (interval)
{
case DateMathTimeUnit.Second:
milliseconds = factor * MillisecondsInASecond;
break;
case DateMathTimeUnit.Minute:
milliseconds = factor * MillisecondsInAMinute;
break;
case DateMathTimeUnit.Hour:
milliseconds = factor * MillisecondsInAnHour;
break;
case DateMathTimeUnit.Day:
milliseconds = factor * MillisecondsInADay;
break;
case DateMathTimeUnit.Week:
milliseconds = factor * MillisecondsInAWeek;
break;
case DateMathTimeUnit.Month:
if (TryGetIntegerGreaterThanZero(fraction, out whole))
{
Factor = whole;
Interval = interval;
_approximateSeconds = whole * (MillisecondsInAMonthApproximate / MillisecondsInASecond);
return;
}
milliseconds = factor * MillisecondsInAMonthApproximate;
break;
case DateMathTimeUnit.Year:
if (TryGetIntegerGreaterThanZero(fraction, out whole))
{
Factor = whole;
Interval = interval;
_approximateSeconds = whole * (MillisecondsInAYearApproximate / MillisecondsInASecond);
return;
}
fraction = fraction * MonthsInAYear;
if (TryGetIntegerGreaterThanZero(fraction, out whole))
{
Factor = whole;
Interval = DateMathTimeUnit.Month;
_approximateSeconds = whole * (MillisecondsInAMonthApproximate / MillisecondsInASecond);
return;
}
milliseconds = factor * MillisecondsInAYearApproximate;
break;
default:
throw new ArgumentOutOfRangeException(nameof(interval), interval, null);
}
SetWholeFactorIntervalAndSeconds(milliseconds, rounding);
}
private void SetWholeFactorIntervalAndSeconds(double milliseconds, MidpointRounding rounding)
{
double fraction;
int whole;
if (milliseconds >= MillisecondsInAWeek)
{
fraction = milliseconds / MillisecondsInAWeek;
if (TryGetIntegerGreaterThanZero(fraction, out whole))
{
Factor = whole;
Interval = DateMathTimeUnit.Week;
_approximateSeconds = Factor * (MillisecondsInAWeek / MillisecondsInASecond);
return;
}
}
if (milliseconds >= MillisecondsInADay)
{
fraction = milliseconds / MillisecondsInADay;
if (TryGetIntegerGreaterThanZero(fraction, out whole))
{
Factor = whole;
Interval = DateMathTimeUnit.Day;
_approximateSeconds = Factor * (MillisecondsInADay / MillisecondsInASecond);
return;
}
}
if (milliseconds >= MillisecondsInAnHour)
{
fraction = milliseconds / MillisecondsInAnHour;
if (TryGetIntegerGreaterThanZero(fraction, out whole))
{
Factor = whole;
Interval = DateMathTimeUnit.Hour;
_approximateSeconds = Factor * (MillisecondsInAnHour / MillisecondsInASecond);
return;
}
}
if (milliseconds >= MillisecondsInAMinute)
{
fraction = milliseconds / MillisecondsInAMinute;
if (TryGetIntegerGreaterThanZero(fraction, out whole))
{
Factor = whole;
Interval = DateMathTimeUnit.Minute;
_approximateSeconds = Factor * (MillisecondsInAMinute / MillisecondsInASecond);
return;
}
}
if (milliseconds >= MillisecondsInASecond)
{
fraction = milliseconds / MillisecondsInASecond;
if (TryGetIntegerGreaterThanZero(fraction, out whole))
{
Factor = whole;
Interval = DateMathTimeUnit.Second;
_approximateSeconds = Factor;
return;
}
}
// round to nearest second, using specified rounding
Factor = Convert.ToInt32(Math.Round(milliseconds / MillisecondsInASecond, rounding));
Interval = DateMathTimeUnit.Second;
_approximateSeconds = Factor;
}
private static bool TryGetIntegerGreaterThanZero(double d, out int value)
{
if (Math.Abs(d % 1) < double.Epsilon)
{
value = Convert.ToInt32(d);
return true;
}
value = 0;
return false;
}
public static bool operator <(DateMathTime left, DateMathTime right) => left.CompareTo(right) < 0;
public static bool operator <=(DateMathTime left, DateMathTime right) => left.CompareTo(right) < 0 || left.Equals(right);
public static bool operator >(DateMathTime left, DateMathTime right) => left.CompareTo(right) > 0;
public static bool operator >=(DateMathTime left, DateMathTime right) => left.CompareTo(right) > 0 || left.Equals(right);
public static bool operator ==(DateMathTime left, DateMathTime right) =>
left?.Equals(right) ?? ReferenceEquals(right, null);
public static bool operator !=(DateMathTime left, DateMathTime right) => !(left == right);
public override string ToString() => Factor + Interval.GetStringValue();
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((DateMathTime)obj);
}
// ReSharper disable once NonReadonlyMemberInGetHashCode
public override int GetHashCode() => _approximateSeconds.GetHashCode();
}
internal class DateMathTimeFormatter: IJsonFormatter<DateMathTime>
{
public void Serialize(ref JsonWriter writer, DateMathTime value, IJsonFormatterResolver formatterResolver)
{
if (value is null) writer.WriteNull();
else writer.WriteString(value.ToString());
}
public DateMathTime Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) => reader.ReadString();
}
}