-
-
Notifications
You must be signed in to change notification settings - Fork 723
/
Iso8601Duration.cs
402 lines (343 loc) · 10.2 KB
/
Iso8601Duration.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
using System;
namespace HotChocolate.Types;
/// <summary>
/// ISO 8601 Duration based on XsdDuration which include additional "Weeks" part
/// https://github.com/dotnet/runtime/blob/master/src/libraries/System.Private.Xml/src/System/Xml/Schema/XsdDuration.cs
/// </summary>
internal struct Iso8601Duration
{
[Flags]
private enum Parts
{
HasNone = 0,
HasYears = 1,
HasMonths = 2,
HasWeeks = 4,
HasDays = 8,
HasHours = 16,
HasMinutes = 32,
HasSeconds = 64,
}
/// <summary>
/// Internal helper method that converts to a TimeSpan value. This code uses the estimate
/// that there are 365 days in the year, 52 weeks in a year and 30 days in a month.
/// </summary>
internal static bool TryToTimeSpan(
int years,
int months,
int weeks,
int days,
int hours,
int minutes,
int seconds,
uint nanoseconds,
bool isNegative,
out TimeSpan? result)
{
ulong ticks = 0;
// Throw error if result cannot fit into a long
try
{
checked
{
if (years != 0 && months != 0)
{
ticks += ((ulong)years + (ulong)months / 12) * 365;
}
if (months != 0)
{
ticks += ((ulong)months % 12) * 30;
}
if (weeks != 0)
{
ticks += ((ulong)weeks % 52) * 7;
}
if (days != 0 || hours != 0 || minutes != 0 || seconds != 0 || nanoseconds != 0)
{
ticks += (ulong)days;
ticks *= 24;
ticks += (ulong)hours;
ticks *= 60;
ticks += (ulong)minutes;
ticks *= 60;
ticks += (ulong)seconds;
// Tick count interval is in 100 nanosecond intervals (7 digits)
ticks *= TimeSpan.TicksPerSecond;
ticks += (ulong)nanoseconds / 100;
}
else
{
// Multiply YearMonth duration by number of ticks per day
ticks *= TimeSpan.TicksPerDay;
}
if (isNegative)
{
// Handle special case of Int64.MaxValue + 1 before negation,
// since it would otherwise overflow
result = ticks >= (ulong)long.MaxValue + 1
? new TimeSpan(long.MinValue)
: new TimeSpan(-((long)ticks));
}
else
{
result = new TimeSpan((long)ticks);
}
return true;
}
}
catch (OverflowException)
{
result = TimeSpan.MinValue;
return false;
}
}
internal static bool TryParse(string s, out TimeSpan? result)
{
int years = default;
int months = default;
int weeks = default;
int days = default;
int hours = default;
int minutes = default;
int seconds = default;
uint nanoseconds = default;
bool isNegative = false;
Parts parts = Parts.HasNone;
result = default;
s = s.Trim();
var length = s.Length;
var pos = 0;
if (pos >= length)
{
return false;
}
if (s[pos] == '-')
{
pos++;
isNegative = true;
}
else
{
nanoseconds = 0;
}
if (pos >= length)
{
return false;
}
if (s[pos++] != 'P')
{
return false;
}
if (!TryParseDigits(s, ref pos, false, out var value, out var numDigits))
{
return false;
}
if (pos >= length)
{
return false;
}
if (s[pos] == 'Y')
{
if (numDigits == 0)
{
return false;
}
parts |= Parts.HasYears;
years = value;
if (++pos <= length)
{
if (!TryParseDigits(s, ref pos, false, out value, out numDigits))
{
return false;
}
}
}
if (pos < length && s[pos] == 'M')
{
if (numDigits == 0)
{
return false;
}
parts |= Parts.HasMonths;
months = value;
if (++pos <= length)
{
if (!TryParseDigits(s, ref pos, false, out value, out numDigits))
{
return false;
}
}
}
if (pos < length && s[pos] == 'W')
{
if (numDigits == 0)
{
return false;
}
parts |= Parts.HasWeeks;
weeks = value;
if (++pos <= length)
{
if (!TryParseDigits(s, ref pos, false, out value, out numDigits))
{
return false;
}
}
}
if (pos < length && s[pos] == 'D')
{
if (numDigits == 0)
{
return false;
}
parts |= Parts.HasDays;
days = value;
if (++pos <= length)
{
if (!TryParseDigits(s, ref pos, false, out value, out numDigits))
{
return false;
}
}
}
if (pos < length && s[pos] == 'T')
{
if (numDigits != 0)
{
return false;
}
pos++;
if (!TryParseDigits(s, ref pos, false, out value, out numDigits))
{
return false;
}
if (pos >= length)
{
return false;
}
if (s[pos] == 'H')
{
if (numDigits == 0)
{
return false;
}
parts |= Parts.HasHours;
hours = value;
if (++pos <= length)
{
if (!TryParseDigits(s, ref pos, false, out value, out numDigits))
{
return false;
}
}
}
if (pos < length && s[pos] == 'M')
{
if (numDigits == 0)
{
return false;
}
parts |= Parts.HasMinutes;
minutes = value;
if (++pos <= length && !TryParseDigits(s, ref pos, false, out value, out numDigits))
{
return false;
}
}
if (pos < length && s[pos] == '.')
{
pos++;
parts |= Parts.HasSeconds;
seconds = value;
if (!TryParseDigits(s, ref pos, true, out value, out numDigits))
{
return false;
}
if (numDigits == 0)
{ //If there are no digits after the decimal point, assume 0
value = 0;
}
// Normalize to nanosecond intervals
for (; numDigits > 9; numDigits--)
value /= 10;
for (; numDigits < 9; numDigits++)
value *= 10;
nanoseconds |= (uint)value;
if (pos >= length)
{
return false;
}
if (s[pos] != 'S')
{
return false;
}
pos++;
}
else if (pos < length && s[pos] == 'S')
{
if (numDigits == 0)
{
return false;
}
parts |= Parts.HasSeconds;
seconds = value;
pos++;
}
}
if (pos == length)
{
// At least one part must be defined
if (parts == Parts.HasNone)
{
return false;
}
return TryToTimeSpan(years,
months,
weeks,
days,
hours,
minutes,
seconds,
nanoseconds,
isNegative,
out result);
}
return false;
}
/// Helper method that constructs an integer from leading digits starting at s[offset].
/// "offset" is updated to contain an offset just beyond the last digit.
/// The number of digits consumed is returned in cntDigits.
/// The integer is returned (0 if no digits). If the digits cannot fit into an Int32:
/// 1. If eatDigits is true, then additional digits will be silently discarded
/// (don't count towards numDigits)
/// 2. If eatDigits is false, an overflow exception is thrown
private static bool TryParseDigits(string s, ref int offset, bool eatDigits, out int result, out int numDigits)
{
var offsetStart = offset;
var offsetEnd = s.Length;
result = 0;
numDigits = 0;
while (offset < offsetEnd && s[offset] >= '0' && s[offset] <= '9')
{
var digit = s[offset] - '0';
if (result > (int.MaxValue - digit) / 10)
{
if (!eatDigits)
{
return false;
}
// Skip past any remaining digits
numDigits = offset - offsetStart;
while (offset < offsetEnd && s[offset] >= '0' && s[offset] <= '9')
{
offset++;
}
return true;
}
result = result * 10 + digit;
offset++;
}
numDigits = offset - offsetStart;
return true;
}
}