| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | |
| | | 8 | | namespace System.Text.Json |
| | | 9 | | { |
| | | 10 | | internal static partial class JsonHelpers |
| | | 11 | | { |
| | | 12 | | [StructLayout(LayoutKind.Auto)] |
| | | 13 | | private struct DateTimeParseData |
| | | 14 | | { |
| | | 15 | | public int Year; |
| | | 16 | | public int Month; |
| | | 17 | | public int Day; |
| | | 18 | | public bool IsCalendarDateOnly; |
| | | 19 | | public int Hour; |
| | | 20 | | public int Minute; |
| | | 21 | | public int Second; |
| | | 22 | | public int Fraction; // This value should never be greater than 9_999_999. |
| | | 23 | | public int OffsetHours; |
| | | 24 | | public int OffsetMinutes; |
| | 0 | 25 | | public bool OffsetNegative => OffsetToken == JsonConstants.Hyphen; |
| | | 26 | | public byte OffsetToken; |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 30 | | public static bool IsValidDateTimeOffsetParseLength(int length) |
| | 610 | 31 | | { |
| | 610 | 32 | | return IsInRangeInclusive(length, JsonConstants.MinimumDateTimeParseLength, JsonConstants.MaximumEscapedDate |
| | 610 | 33 | | } |
| | | 34 | | |
| | | 35 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 36 | | public static bool IsValidUnescapedDateTimeOffsetParseLength(int length) |
| | 0 | 37 | | { |
| | 0 | 38 | | return IsInRangeInclusive(length, JsonConstants.MinimumDateTimeParseLength, JsonConstants.MaximumDateTimeOff |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Parse the given UTF-8 <paramref name="source"/> as extended ISO 8601 format. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="source">UTF-8 source to parse.</param> |
| | | 45 | | /// <param name="value">The parsed <see cref="DateTime"/> if successful.</param> |
| | | 46 | | /// <returns>"true" if successfully parsed.</returns> |
| | | 47 | | public static bool TryParseAsISO(ReadOnlySpan<byte> source, out DateTime value) |
| | 40 | 48 | | { |
| | 40 | 49 | | if (!TryParseDateTimeOffset(source, out DateTimeParseData parseData)) |
| | 40 | 50 | | { |
| | 40 | 51 | | value = default; |
| | 40 | 52 | | return false; |
| | | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | if (parseData.OffsetToken == JsonConstants.UtcOffsetToken) |
| | 0 | 56 | | { |
| | 0 | 57 | | return TryCreateDateTime(parseData, DateTimeKind.Utc, out value); |
| | | 58 | | } |
| | 0 | 59 | | else if (parseData.OffsetToken == JsonConstants.Plus || parseData.OffsetToken == JsonConstants.Hyphen) |
| | 0 | 60 | | { |
| | 0 | 61 | | if (!TryCreateDateTimeOffset(ref parseData, out DateTimeOffset dateTimeOffset)) |
| | 0 | 62 | | { |
| | 0 | 63 | | value = default; |
| | 0 | 64 | | return false; |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | value = dateTimeOffset.LocalDateTime; |
| | 0 | 68 | | return true; |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | return TryCreateDateTime(parseData, DateTimeKind.Unspecified, out value); |
| | 40 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Parse the given UTF-8 <paramref name="source"/> as extended ISO 8601 format. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <param name="source">UTF-8 source to parse.</param> |
| | | 78 | | /// <param name="value">The parsed <see cref="DateTimeOffset"/> if successful.</param> |
| | | 79 | | /// <returns>"true" if successfully parsed.</returns> |
| | | 80 | | public static bool TryParseAsISO(ReadOnlySpan<byte> source, out DateTimeOffset value) |
| | 18 | 81 | | { |
| | 18 | 82 | | if (!TryParseDateTimeOffset(source, out DateTimeParseData parseData)) |
| | 18 | 83 | | { |
| | 18 | 84 | | value = default; |
| | 18 | 85 | | return false; |
| | | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | if (parseData.OffsetToken == JsonConstants.UtcOffsetToken || // Same as specifying an offset of "+00:00", ex |
| | 0 | 89 | | parseData.OffsetToken == JsonConstants.Plus || parseData.OffsetToken == JsonConstants.Hyphen) |
| | 0 | 90 | | { |
| | 0 | 91 | | return TryCreateDateTimeOffset(ref parseData, out value); |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | // No offset, attempt to read as local time. |
| | 0 | 95 | | return TryCreateDateTimeOffsetInterpretingDataAsLocalTime(parseData, out value); |
| | 18 | 96 | | } |
| | | 97 | | |
| | | 98 | | #if NET |
| | | 99 | | public static bool TryParseAsIso(ReadOnlySpan<byte> source, out DateOnly value) |
| | 12 | 100 | | { |
| | 12 | 101 | | if (TryParseDateTimeOffset(source, out DateTimeParseData parseData) && |
| | 12 | 102 | | parseData.IsCalendarDateOnly && |
| | 12 | 103 | | TryCreateDateTime(parseData, DateTimeKind.Unspecified, out DateTime dateTime)) |
| | 0 | 104 | | { |
| | 0 | 105 | | value = DateOnly.FromDateTime(dateTime); |
| | 0 | 106 | | return true; |
| | | 107 | | } |
| | | 108 | | |
| | 12 | 109 | | value = default; |
| | 12 | 110 | | return false; |
| | 12 | 111 | | } |
| | | 112 | | #endif |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// ISO 8601 date time parser (ISO 8601-1:2019). |
| | | 116 | | /// </summary> |
| | | 117 | | /// <param name="source">The date/time to parse in UTF-8 format.</param> |
| | | 118 | | /// <param name="parseData">The parsed <see cref="DateTimeParseData"/> for the given <paramref name="source"/>.< |
| | | 119 | | /// <remarks> |
| | | 120 | | /// Supports extended calendar date (5.2.2.1) and complete (5.4.2.1) calendar date/time of day |
| | | 121 | | /// representations with optional specification of seconds and fractional seconds. |
| | | 122 | | /// |
| | | 123 | | /// Times can be explicitly specified as UTC ("Z" - 5.3.3) or offsets from UTC ("+/-hh:mm" 5.3.4.2). |
| | | 124 | | /// If unspecified they are considered to be local per spec. |
| | | 125 | | /// |
| | | 126 | | /// Examples: (TZD is either "Z" or hh:mm offset from UTC) |
| | | 127 | | /// |
| | | 128 | | /// YYYY-MM-DD (eg 1997-07-16) |
| | | 129 | | /// YYYY-MM-DDThh:mm (eg 1997-07-16T19:20) |
| | | 130 | | /// YYYY-MM-DDThh:mm:ss (eg 1997-07-16T19:20:30) |
| | | 131 | | /// YYYY-MM-DDThh:mm:ss.s (eg 1997-07-16T19:20:30.45) |
| | | 132 | | /// YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00) |
| | | 133 | | /// YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:3001:00) |
| | | 134 | | /// YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45Z) |
| | | 135 | | /// |
| | | 136 | | /// Generally speaking we always require the "extended" option when one exists (3.1.3.5). |
| | | 137 | | /// The extended variants have separator characters between components ('-', ':', '.', etc.). |
| | | 138 | | /// Spaces are not permitted. |
| | | 139 | | /// </remarks> |
| | | 140 | | /// <returns>"true" if successfully parsed.</returns> |
| | | 141 | | private static bool TryParseDateTimeOffset(ReadOnlySpan<byte> source, out DateTimeParseData parseData) |
| | 70 | 142 | | { |
| | 70 | 143 | | parseData = default; |
| | | 144 | | |
| | | 145 | | // too short datetime |
| | 70 | 146 | | Debug.Assert(source.Length >= 10); |
| | | 147 | | |
| | | 148 | | // Parse the calendar date |
| | | 149 | | // ----------------------- |
| | | 150 | | // ISO 8601-1:2019 5.2.2.1b "Calendar date complete extended format" |
| | | 151 | | // [dateX] = [year]["-"][month]["-"][day] |
| | | 152 | | // [year] = [YYYY] [0000 - 9999] (4.3.2) |
| | | 153 | | // [month] = [MM] [01 - 12] (4.3.3) |
| | | 154 | | // [day] = [DD] [01 - 28, 29, 30, 31] (4.3.4) |
| | | 155 | | // |
| | | 156 | | // Note: 5.2.2.2 "Representations with reduced precision" allows for |
| | | 157 | | // just [year]["-"][month] (a) and just [year] (b), but we currently |
| | | 158 | | // don't permit it. |
| | | 159 | | |
| | 70 | 160 | | { |
| | 70 | 161 | | uint digit1 = source[0] - (uint)'0'; |
| | 70 | 162 | | uint digit2 = source[1] - (uint)'0'; |
| | 70 | 163 | | uint digit3 = source[2] - (uint)'0'; |
| | 70 | 164 | | uint digit4 = source[3] - (uint)'0'; |
| | | 165 | | |
| | 70 | 166 | | if (digit1 > 9 || digit2 > 9 || digit3 > 9 || digit4 > 9) |
| | 70 | 167 | | { |
| | 70 | 168 | | return false; |
| | | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | parseData.Year = (int)(digit1 * 1000 + digit2 * 100 + digit3 * 10 + digit4); |
| | 0 | 172 | | } |
| | | 173 | | |
| | 0 | 174 | | if (source[4] != JsonConstants.Hyphen |
| | 0 | 175 | | || !TryGetNextTwoDigits(source.Slice(start: 5, length: 2), ref parseData.Month) |
| | 0 | 176 | | || source[7] != JsonConstants.Hyphen |
| | 0 | 177 | | || !TryGetNextTwoDigits(source.Slice(start: 8, length: 2), ref parseData.Day)) |
| | 0 | 178 | | { |
| | 0 | 179 | | return false; |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | // We now have YYYY-MM-DD [dateX] |
| | 0 | 183 | | if (source.Length == 10) |
| | 0 | 184 | | { |
| | 0 | 185 | | parseData.IsCalendarDateOnly = true; |
| | 0 | 186 | | return true; |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | // Parse the time of day |
| | | 190 | | // --------------------- |
| | | 191 | | // |
| | | 192 | | // ISO 8601-1:2019 5.3.1.2b "Local time of day complete extended format" |
| | | 193 | | // [timeX] = ["T"][hour][":"][min][":"][sec] |
| | | 194 | | // [hour] = [hh] [00 - 23] (4.3.8a) |
| | | 195 | | // [minute] = [mm] [00 - 59] (4.3.9a) |
| | | 196 | | // [sec] = [ss] [00 - 59, 60 with a leap second] (4.3.10a) |
| | | 197 | | // |
| | | 198 | | // ISO 8601-1:2019 5.3.3 "UTC of day" |
| | | 199 | | // [timeX]["Z"] |
| | | 200 | | // |
| | | 201 | | // ISO 8601-1:2019 5.3.4.2 "Local time of day with the time shift between |
| | | 202 | | // local time scale and UTC" (Extended format) |
| | | 203 | | // |
| | | 204 | | // [shiftX] = ["+"|"-"][hour][":"][min] |
| | | 205 | | // |
| | | 206 | | // Notes: |
| | | 207 | | // |
| | | 208 | | // "T" is optional per spec, but _only_ when times are used alone. In our |
| | | 209 | | // case, we're reading out a complete date & time and as such require "T". |
| | | 210 | | // (5.4.2.1b). |
| | | 211 | | // |
| | | 212 | | // For [timeX] We allow seconds to be omitted per 5.3.1.3a "Representations |
| | | 213 | | // with reduced precision". 5.3.1.3b allows just specifying the hour, but |
| | | 214 | | // we currently don't permit this. |
| | | 215 | | // |
| | | 216 | | // Decimal fractions are allowed for hours, minutes and seconds (5.3.14). |
| | | 217 | | // We only allow fractions for seconds currently. Lower order components |
| | | 218 | | // can't follow, i.e. you can have T23.3, but not T23.3:04. There must be |
| | | 219 | | // one digit, but the max number of digits is implementation defined. We |
| | | 220 | | // currently allow up to 16 digits of fractional seconds only. While we |
| | | 221 | | // support 16 fractional digits we only parse the first seven, anything |
| | | 222 | | // past that is considered a zero. This is to stay compatible with the |
| | | 223 | | // DateTime implementation which is limited to this resolution. |
| | | 224 | | |
| | 0 | 225 | | if (source.Length < 16) |
| | 0 | 226 | | { |
| | | 227 | | // Source does not have enough characters for YYYY-MM-DDThh:mm |
| | 0 | 228 | | return false; |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | // Parse THH:MM (e.g. "T10:32") |
| | 0 | 232 | | if (source[10] != JsonConstants.TimePrefix || source[13] != JsonConstants.Colon |
| | 0 | 233 | | || !TryGetNextTwoDigits(source.Slice(start: 11, length: 2), ref parseData.Hour) |
| | 0 | 234 | | || !TryGetNextTwoDigits(source.Slice(start: 14, length: 2), ref parseData.Minute)) |
| | 0 | 235 | | { |
| | 0 | 236 | | return false; |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | // We now have YYYY-MM-DDThh:mm |
| | 0 | 240 | | Debug.Assert(source.Length >= 16); |
| | 0 | 241 | | if (source.Length == 16) |
| | 0 | 242 | | { |
| | 0 | 243 | | return true; |
| | | 244 | | } |
| | | 245 | | |
| | 0 | 246 | | byte curByte = source[16]; |
| | 0 | 247 | | int sourceIndex = 17; |
| | | 248 | | |
| | | 249 | | // Either a TZD ['Z'|'+'|'-'] or a seconds separator [':'] is valid at this point |
| | 0 | 250 | | switch (curByte) |
| | | 251 | | { |
| | | 252 | | case JsonConstants.UtcOffsetToken: |
| | 0 | 253 | | parseData.OffsetToken = JsonConstants.UtcOffsetToken; |
| | 0 | 254 | | return sourceIndex == source.Length; |
| | | 255 | | case JsonConstants.Plus: |
| | | 256 | | case JsonConstants.Hyphen: |
| | 0 | 257 | | parseData.OffsetToken = curByte; |
| | 0 | 258 | | return ParseOffset(ref parseData, source.Slice(sourceIndex)); |
| | | 259 | | case JsonConstants.Colon: |
| | 0 | 260 | | break; |
| | | 261 | | default: |
| | 0 | 262 | | return false; |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | // Try reading the seconds |
| | 0 | 266 | | if (source.Length < 19 |
| | 0 | 267 | | || !TryGetNextTwoDigits(source.Slice(start: 17, length: 2), ref parseData.Second)) |
| | 0 | 268 | | { |
| | 0 | 269 | | return false; |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | // We now have YYYY-MM-DDThh:mm:ss |
| | 0 | 273 | | Debug.Assert(source.Length >= 19); |
| | 0 | 274 | | if (source.Length == 19) |
| | 0 | 275 | | { |
| | 0 | 276 | | return true; |
| | | 277 | | } |
| | | 278 | | |
| | 0 | 279 | | curByte = source[19]; |
| | 0 | 280 | | sourceIndex = 20; |
| | | 281 | | |
| | | 282 | | // Either a TZD ['Z'|'+'|'-'] or a seconds decimal fraction separator ['.'] is valid at this point |
| | 0 | 283 | | switch (curByte) |
| | | 284 | | { |
| | | 285 | | case JsonConstants.UtcOffsetToken: |
| | 0 | 286 | | parseData.OffsetToken = JsonConstants.UtcOffsetToken; |
| | 0 | 287 | | return sourceIndex == source.Length; |
| | | 288 | | case JsonConstants.Plus: |
| | | 289 | | case JsonConstants.Hyphen: |
| | 0 | 290 | | parseData.OffsetToken = curByte; |
| | 0 | 291 | | return ParseOffset(ref parseData, source.Slice(sourceIndex)); |
| | | 292 | | case JsonConstants.Period: |
| | 0 | 293 | | break; |
| | | 294 | | default: |
| | 0 | 295 | | return false; |
| | | 296 | | } |
| | | 297 | | |
| | | 298 | | // Source does not have enough characters for second fractions (i.e. ".s") |
| | | 299 | | // YYYY-MM-DDThh:mm:ss.s |
| | 0 | 300 | | if (source.Length < 21) |
| | 0 | 301 | | { |
| | 0 | 302 | | return false; |
| | | 303 | | } |
| | | 304 | | |
| | | 305 | | // Parse fraction. This value should never be greater than 9_999_999 |
| | 0 | 306 | | { |
| | 0 | 307 | | int numDigitsRead = 0; |
| | 0 | 308 | | int fractionEnd = Math.Min(sourceIndex + JsonConstants.DateTimeParseNumFractionDigits, source.Length); |
| | | 309 | | |
| | 0 | 310 | | while (sourceIndex < fractionEnd && IsDigit(curByte = source[sourceIndex])) |
| | 0 | 311 | | { |
| | 0 | 312 | | if (numDigitsRead < JsonConstants.DateTimeNumFractionDigits) |
| | 0 | 313 | | { |
| | 0 | 314 | | parseData.Fraction = (parseData.Fraction * 10) + (int)(curByte - (uint)'0'); |
| | 0 | 315 | | numDigitsRead++; |
| | 0 | 316 | | } |
| | | 317 | | |
| | 0 | 318 | | sourceIndex++; |
| | 0 | 319 | | } |
| | | 320 | | |
| | 0 | 321 | | if (parseData.Fraction != 0) |
| | 0 | 322 | | { |
| | 0 | 323 | | while (numDigitsRead < JsonConstants.DateTimeNumFractionDigits) |
| | 0 | 324 | | { |
| | 0 | 325 | | parseData.Fraction *= 10; |
| | 0 | 326 | | numDigitsRead++; |
| | 0 | 327 | | } |
| | 0 | 328 | | } |
| | 0 | 329 | | } |
| | | 330 | | |
| | | 331 | | // We now have YYYY-MM-DDThh:mm:ss.s |
| | 0 | 332 | | Debug.Assert(sourceIndex <= source.Length); |
| | 0 | 333 | | if (sourceIndex == source.Length) |
| | 0 | 334 | | { |
| | 0 | 335 | | return true; |
| | | 336 | | } |
| | | 337 | | |
| | 0 | 338 | | curByte = source[sourceIndex++]; |
| | | 339 | | |
| | | 340 | | // TZD ['Z'|'+'|'-'] is valid at this point |
| | 0 | 341 | | switch (curByte) |
| | | 342 | | { |
| | | 343 | | case JsonConstants.UtcOffsetToken: |
| | 0 | 344 | | parseData.OffsetToken = JsonConstants.UtcOffsetToken; |
| | 0 | 345 | | return sourceIndex == source.Length; |
| | | 346 | | case JsonConstants.Plus: |
| | | 347 | | case JsonConstants.Hyphen: |
| | 0 | 348 | | parseData.OffsetToken = curByte; |
| | 0 | 349 | | return ParseOffset(ref parseData, source.Slice(sourceIndex)); |
| | | 350 | | default: |
| | 0 | 351 | | return false; |
| | | 352 | | } |
| | | 353 | | |
| | | 354 | | static bool ParseOffset(ref DateTimeParseData parseData, ReadOnlySpan<byte> offsetData) |
| | 0 | 355 | | { |
| | | 356 | | // Parse the hours for the offset |
| | 0 | 357 | | if (offsetData.Length < 2 |
| | 0 | 358 | | || !TryGetNextTwoDigits(offsetData.Slice(0, 2), ref parseData.OffsetHours)) |
| | 0 | 359 | | { |
| | 0 | 360 | | return false; |
| | | 361 | | } |
| | | 362 | | |
| | | 363 | | // We now have YYYY-MM-DDThh:mm:ss.s+|-hh |
| | | 364 | | |
| | 0 | 365 | | if (offsetData.Length == 2) |
| | 0 | 366 | | { |
| | | 367 | | // Just hours offset specified |
| | 0 | 368 | | return true; |
| | | 369 | | } |
| | | 370 | | |
| | | 371 | | // Ensure we have enough for ":mm" |
| | 0 | 372 | | if (offsetData.Length != 5 |
| | 0 | 373 | | || offsetData[2] != JsonConstants.Colon |
| | 0 | 374 | | || !TryGetNextTwoDigits(offsetData.Slice(3), ref parseData.OffsetMinutes)) |
| | 0 | 375 | | { |
| | 0 | 376 | | return false; |
| | | 377 | | } |
| | | 378 | | |
| | 0 | 379 | | return true; |
| | 0 | 380 | | } |
| | 70 | 381 | | } |
| | | 382 | | |
| | | 383 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 384 | | private static bool TryGetNextTwoDigits(ReadOnlySpan<byte> source, ref int value) |
| | 0 | 385 | | { |
| | 0 | 386 | | Debug.Assert(source.Length == 2); |
| | | 387 | | |
| | 0 | 388 | | uint digit1 = source[0] - (uint)'0'; |
| | 0 | 389 | | uint digit2 = source[1] - (uint)'0'; |
| | | 390 | | |
| | 0 | 391 | | if (digit1 > 9 || digit2 > 9) |
| | 0 | 392 | | { |
| | 0 | 393 | | value = default; |
| | 0 | 394 | | return false; |
| | | 395 | | } |
| | | 396 | | |
| | 0 | 397 | | value = (int)(digit1 * 10 + digit2); |
| | 0 | 398 | | return true; |
| | 0 | 399 | | } |
| | | 400 | | |
| | | 401 | | // The following methods are borrowed verbatim from src/Common/src/CoreLib/System/Buffers/Text/Utf8Parser/Utf8Pa |
| | | 402 | | |
| | | 403 | | /// <summary> |
| | | 404 | | /// Overflow-safe DateTimeOffset factory. |
| | | 405 | | /// </summary> |
| | | 406 | | private static bool TryCreateDateTimeOffset(DateTime dateTime, ref DateTimeParseData parseData, out DateTimeOffs |
| | 0 | 407 | | { |
| | 0 | 408 | | if (((uint)parseData.OffsetHours) > JsonConstants.MaxDateTimeUtcOffsetHours) |
| | 0 | 409 | | { |
| | 0 | 410 | | value = default; |
| | 0 | 411 | | return false; |
| | | 412 | | } |
| | | 413 | | |
| | 0 | 414 | | if (((uint)parseData.OffsetMinutes) > 59) |
| | 0 | 415 | | { |
| | 0 | 416 | | value = default; |
| | 0 | 417 | | return false; |
| | | 418 | | } |
| | | 419 | | |
| | 0 | 420 | | if (parseData.OffsetHours == JsonConstants.MaxDateTimeUtcOffsetHours && parseData.OffsetMinutes != 0) |
| | 0 | 421 | | { |
| | 0 | 422 | | value = default; |
| | 0 | 423 | | return false; |
| | | 424 | | } |
| | | 425 | | |
| | 0 | 426 | | long offsetTicks = (((long)parseData.OffsetHours) * 3600 + ((long)parseData.OffsetMinutes) * 60) * TimeSpan. |
| | 0 | 427 | | if (parseData.OffsetNegative) |
| | 0 | 428 | | { |
| | 0 | 429 | | offsetTicks = -offsetTicks; |
| | 0 | 430 | | } |
| | | 431 | | |
| | | 432 | | try |
| | 0 | 433 | | { |
| | 0 | 434 | | value = new DateTimeOffset(ticks: dateTime.Ticks, offset: new TimeSpan(ticks: offsetTicks)); |
| | 0 | 435 | | } |
| | 0 | 436 | | catch (ArgumentOutOfRangeException) |
| | 0 | 437 | | { |
| | | 438 | | // If we got here, the combination of the DateTime + UTC offset strayed outside the 1..9999 year range. |
| | | 439 | | // that it's better to catch the exception rather than replicate DateTime's range checking (which it's g |
| | 0 | 440 | | value = default; |
| | 0 | 441 | | return false; |
| | | 442 | | } |
| | | 443 | | |
| | 0 | 444 | | return true; |
| | 0 | 445 | | } |
| | | 446 | | |
| | | 447 | | /// <summary> |
| | | 448 | | /// Overflow-safe DateTimeOffset factory. |
| | | 449 | | /// </summary> |
| | | 450 | | private static bool TryCreateDateTimeOffset(ref DateTimeParseData parseData, out DateTimeOffset value) |
| | 0 | 451 | | { |
| | 0 | 452 | | if (!TryCreateDateTime(parseData, kind: DateTimeKind.Unspecified, out DateTime dateTime)) |
| | 0 | 453 | | { |
| | 0 | 454 | | value = default; |
| | 0 | 455 | | return false; |
| | | 456 | | } |
| | | 457 | | |
| | 0 | 458 | | if (!TryCreateDateTimeOffset(dateTime: dateTime, ref parseData, out value)) |
| | 0 | 459 | | { |
| | 0 | 460 | | value = default; |
| | 0 | 461 | | return false; |
| | | 462 | | } |
| | | 463 | | |
| | 0 | 464 | | return true; |
| | 0 | 465 | | } |
| | | 466 | | |
| | | 467 | | /// <summary> |
| | | 468 | | /// Overflow-safe DateTimeOffset/Local time conversion factory. |
| | | 469 | | /// </summary> |
| | | 470 | | private static bool TryCreateDateTimeOffsetInterpretingDataAsLocalTime(DateTimeParseData parseData, out DateTime |
| | 0 | 471 | | { |
| | 0 | 472 | | if (!TryCreateDateTime(parseData, DateTimeKind.Local, out DateTime dateTime)) |
| | 0 | 473 | | { |
| | 0 | 474 | | value = default; |
| | 0 | 475 | | return false; |
| | | 476 | | } |
| | | 477 | | |
| | | 478 | | try |
| | 0 | 479 | | { |
| | 0 | 480 | | value = new DateTimeOffset(dateTime); |
| | 0 | 481 | | } |
| | 0 | 482 | | catch (ArgumentOutOfRangeException) |
| | 0 | 483 | | { |
| | | 484 | | // If we got here, the combination of the DateTime + UTC offset strayed outside the 1..9999 year range. |
| | | 485 | | // that it's better to catch the exception rather than replicate DateTime's range checking (which it's g |
| | 0 | 486 | | value = default; |
| | 0 | 487 | | return false; |
| | | 488 | | } |
| | | 489 | | |
| | 0 | 490 | | return true; |
| | 0 | 491 | | } |
| | | 492 | | |
| | | 493 | | /// <summary> |
| | | 494 | | /// Overflow-safe DateTime factory. |
| | | 495 | | /// </summary> |
| | | 496 | | private static bool TryCreateDateTime(DateTimeParseData parseData, DateTimeKind kind, out DateTime value) |
| | 0 | 497 | | { |
| | 0 | 498 | | if (parseData.Year == 0) |
| | 0 | 499 | | { |
| | 0 | 500 | | value = default; |
| | 0 | 501 | | return false; |
| | | 502 | | } |
| | | 503 | | |
| | 0 | 504 | | Debug.Assert(parseData.Year <= 9999); // All of our callers to date parse the year from fixed 4-digit fields |
| | | 505 | | |
| | 0 | 506 | | if ((((uint)parseData.Month) - 1) >= 12) |
| | 0 | 507 | | { |
| | 0 | 508 | | value = default; |
| | 0 | 509 | | return false; |
| | | 510 | | } |
| | | 511 | | |
| | 0 | 512 | | uint dayMinusOne = ((uint)parseData.Day) - 1; |
| | 0 | 513 | | if (dayMinusOne >= 28 && dayMinusOne >= DateTime.DaysInMonth(parseData.Year, parseData.Month)) |
| | 0 | 514 | | { |
| | 0 | 515 | | value = default; |
| | 0 | 516 | | return false; |
| | | 517 | | } |
| | | 518 | | |
| | | 519 | | // Per ISO 8601-1:2019, 24:00:00 represents end of a calendar day |
| | | 520 | | // (same instant as next day's 00:00:00), but only when minute, second, and fraction are all zero. |
| | | 521 | | // We treat it as hour=0 and add one day at the end. |
| | 0 | 522 | | bool isEndOfDay = false; |
| | 0 | 523 | | if (parseData.Hour == 24) |
| | 0 | 524 | | { |
| | 0 | 525 | | if (parseData.Minute != 0 || parseData.Second != 0 || parseData.Fraction != 0) |
| | 0 | 526 | | { |
| | 0 | 527 | | value = default; |
| | 0 | 528 | | return false; |
| | | 529 | | } |
| | | 530 | | |
| | 0 | 531 | | parseData.Hour = 0; |
| | 0 | 532 | | isEndOfDay = true; |
| | 0 | 533 | | } |
| | | 534 | | |
| | 0 | 535 | | if (((uint)parseData.Hour) > 23) |
| | 0 | 536 | | { |
| | 0 | 537 | | value = default; |
| | 0 | 538 | | return false; |
| | | 539 | | } |
| | | 540 | | |
| | 0 | 541 | | if (((uint)parseData.Minute) > 59) |
| | 0 | 542 | | { |
| | 0 | 543 | | value = default; |
| | 0 | 544 | | return false; |
| | | 545 | | } |
| | | 546 | | |
| | | 547 | | // This needs to allow leap seconds when appropriate. |
| | | 548 | | // See https://github.com/dotnet/runtime/issues/30135. |
| | 0 | 549 | | if (((uint)parseData.Second) > 59) |
| | 0 | 550 | | { |
| | 0 | 551 | | value = default; |
| | 0 | 552 | | return false; |
| | | 553 | | } |
| | | 554 | | |
| | 0 | 555 | | Debug.Assert(parseData.Fraction >= 0 && parseData.Fraction <= JsonConstants.MaxDateTimeFraction); // All of |
| | | 556 | | |
| | 0 | 557 | | ReadOnlySpan<int> days = DateTime.IsLeapYear(parseData.Year) ? DaysToMonth366 : DaysToMonth365; |
| | 0 | 558 | | int yearMinusOne = parseData.Year - 1; |
| | 0 | 559 | | int totalDays = (yearMinusOne * 365) + (yearMinusOne / 4) - (yearMinusOne / 100) + (yearMinusOne / 400) + da |
| | 0 | 560 | | long ticks = totalDays * TimeSpan.TicksPerDay; |
| | 0 | 561 | | int totalSeconds = (parseData.Hour * 3600) + (parseData.Minute * 60) + parseData.Second; |
| | 0 | 562 | | ticks += totalSeconds * TimeSpan.TicksPerSecond; |
| | 0 | 563 | | ticks += parseData.Fraction; |
| | | 564 | | |
| | | 565 | | // If hour was originally 24 (end of day per ISO 8601), add one day to advance to next day's 00:00:00 |
| | 0 | 566 | | if (isEndOfDay) |
| | 0 | 567 | | { |
| | 0 | 568 | | ticks += TimeSpan.TicksPerDay; |
| | 0 | 569 | | if ((ulong)ticks > (ulong)DateTime.MaxValue.Ticks) |
| | 0 | 570 | | { |
| | 0 | 571 | | value = default; |
| | 0 | 572 | | return false; |
| | | 573 | | } |
| | 0 | 574 | | } |
| | | 575 | | |
| | 0 | 576 | | value = new DateTime(ticks: ticks, kind: kind); |
| | 0 | 577 | | return true; |
| | 0 | 578 | | } |
| | | 579 | | |
| | 0 | 580 | | private static ReadOnlySpan<int> DaysToMonth365 => [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]; |
| | 0 | 581 | | private static ReadOnlySpan<int> DaysToMonth366 => [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366]; |
| | | 582 | | } |
| | | 583 | | } |