| | 1 | | namespace Songhay; |
| | 2 | |
|
| | 3 | | public static partial class ProgramTypeUtility |
| | 4 | | { |
| | 5 | | /// <summary> |
| | 6 | | /// Tries to convert the specified value |
| | 7 | | /// to the <see cref="Nullable"></see> return type. |
| | 8 | | /// </summary> |
| | 9 | | /// <param name="value"> |
| | 10 | | /// The specified <see cref="Object"></see> box. |
| | 11 | | /// </param> |
| | 12 | | /// <returns> |
| | 13 | | /// Always returns a <see cref="Nullable"></see> |
| | 14 | | /// as parse failure means <c>HasValue</c> |
| | 15 | | /// is false. |
| | 16 | | /// </returns> |
| 0 | 17 | | public static bool? ParseBoolean(object? value) => ParseBoolean(value, supportBitStrings: false); |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Tries to convert the specified value |
| | 21 | | /// to the <see cref="Nullable"/> return type. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="value"> |
| | 24 | | /// The specified <see cref="Object"/> box. |
| | 25 | | /// </param> |
| | 26 | | /// <param name="supportBitStrings"> |
| | 27 | | /// When <c>true</c>, support "1" and "0" |
| | 28 | | /// as Boolean strings. |
| | 29 | | /// </param> |
| | 30 | | /// <returns> |
| | 31 | | /// Always returns a <see cref="Nullable"/> |
| | 32 | | /// as parse failure means <c>HasValue</c> |
| | 33 | | /// is false. |
| | 34 | | /// </returns> |
| | 35 | | public static bool? ParseBoolean(object? value, bool supportBitStrings) |
| 0 | 36 | | { |
| 0 | 37 | | string? s = (value != null) ? value.ToString() : string.Empty; |
| | 38 | |
|
| 0 | 39 | | if (supportBitStrings) |
| 0 | 40 | | { |
| 0 | 41 | | switch (s?.Trim()) |
| | 42 | | { |
| | 43 | | case "0": |
| 0 | 44 | | return false; |
| | 45 | | case "1": |
| 0 | 46 | | return true; |
| | 47 | | } |
| 0 | 48 | | } |
| | 49 | |
|
| 0 | 50 | | return bool.TryParse(s, out var bln) ? bln : default(bool?); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Tries to convert the specified value |
| | 55 | | /// to the <see cref="Nullable"/> return type. |
| | 56 | | /// </summary> |
| | 57 | | /// <param name="value"> |
| | 58 | | /// The specified <see cref="Object"/> box. |
| | 59 | | /// </param> |
| | 60 | | /// <param name="supportBitStrings"> |
| | 61 | | /// When <c>true</c>, support "1" and "0" |
| | 62 | | /// as Boolean strings. |
| | 63 | | /// </param> |
| | 64 | | /// <param name="defaultValue"> |
| | 65 | | /// The default value to return when <c>Nullable.HasValue == false</c>. |
| | 66 | | /// </param> |
| | 67 | | /// <returns> |
| | 68 | | /// Always returns a <see cref="Nullable"/> |
| | 69 | | /// as parse failure means <c>HasValue</c> |
| | 70 | | /// is false. |
| | 71 | | /// </returns> |
| | 72 | | public static bool? ParseBoolean(object? value, bool supportBitStrings, bool defaultValue) |
| 0 | 73 | | { |
| 0 | 74 | | bool? bln = ParseBoolean(value, supportBitStrings); |
| | 75 | |
|
| 0 | 76 | | return bln ?? new bool?(defaultValue); |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Tries to convert the specified value |
| | 81 | | /// to the <see cref="Nullable"/> return type. |
| | 82 | | /// </summary> |
| | 83 | | /// <param name="value"> |
| | 84 | | /// The specified <see cref="Object"/> box. |
| | 85 | | /// </param> |
| | 86 | | /// <returns> |
| | 87 | | /// Always returns a <see cref="Nullable"/> |
| | 88 | | /// as parse failure means <c>HasValue</c> |
| | 89 | | /// is false. |
| | 90 | | /// </returns> |
| | 91 | | public static byte? ParseByte(object? value) |
| 0 | 92 | | { |
| 0 | 93 | | string? s = value != null ? value.ToString() : string.Empty; |
| | 94 | |
|
| 0 | 95 | | return byte.TryParse(s, out var b) ? b : default(byte?); |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | /// <summary> |
| | 99 | | /// Tries to convert the specified value |
| | 100 | | /// to the <see cref="Nullable"/> return type. |
| | 101 | | /// </summary> |
| | 102 | | /// <param name="value"> |
| | 103 | | /// The specified <see cref="Object"/> box. |
| | 104 | | /// </param> |
| | 105 | | /// <param name="defaultValue"> |
| | 106 | | /// The default value to return when <c>Nullable.HasValue == false</c>. |
| | 107 | | /// </param> |
| | 108 | | /// <returns> |
| | 109 | | /// Always returns a <see cref="Nullable"/> |
| | 110 | | /// as parse failure means <c>HasValue</c> |
| | 111 | | /// is false. |
| | 112 | | /// </returns> |
| | 113 | | public static byte? ParseByte(object? value, byte defaultValue) |
| 0 | 114 | | { |
| 0 | 115 | | byte? b = ParseByte(value); |
| | 116 | |
|
| 0 | 117 | | return b ?? new byte?(defaultValue); |
| 0 | 118 | | } |
| | 119 | |
|
| | 120 | | /// <summary> |
| | 121 | | /// Tries to convert the specified value |
| | 122 | | /// to the <see cref="Nullable"/> return type. |
| | 123 | | /// </summary> |
| | 124 | | /// <param name="value"> |
| | 125 | | /// The specified <see cref="Object"/> box. |
| | 126 | | /// </param> |
| | 127 | | /// <returns> |
| | 128 | | /// Always returns a <see cref="Nullable"/> |
| | 129 | | /// as parse failure means <c>HasValue</c> |
| | 130 | | /// is false. |
| | 131 | | /// </returns> |
| | 132 | | public static DateTime? ParseDateTime(object? value) |
| 0 | 133 | | { |
| 0 | 134 | | string? s = value != null ? value.ToString() : string.Empty; |
| | 135 | |
|
| 0 | 136 | | return DateTime.TryParse(s, out var d) ? d : default(DateTime?); |
| 0 | 137 | | } |
| | 138 | |
|
| | 139 | | /// <summary> |
| | 140 | | /// Tries to convert the specified value |
| | 141 | | /// to the <see cref="Nullable"/> return type. |
| | 142 | | /// </summary> |
| | 143 | | /// <param name="value"> |
| | 144 | | /// The specified <see cref="Object"/> box. |
| | 145 | | /// </param> |
| | 146 | | /// <param name="defaultValue"> |
| | 147 | | /// The default value to return when <c>Nullable.HasValue == false</c>. |
| | 148 | | /// </param> |
| | 149 | | /// <returns> |
| | 150 | | /// Always returns a <see cref="Nullable"/> |
| | 151 | | /// as parse failure means <c>HasValue</c> |
| | 152 | | /// is false. |
| | 153 | | /// </returns> |
| | 154 | | public static DateTime? ParseDateTime(object? value, DateTime defaultValue) |
| 0 | 155 | | { |
| 0 | 156 | | DateTime? d = ParseDateTime(value); |
| | 157 | |
|
| 0 | 158 | | return d ?? new DateTime?(defaultValue); |
| 0 | 159 | | } |
| | 160 | |
|
| | 161 | | /// <summary> |
| | 162 | | /// Tries to convert the specified value |
| | 163 | | /// to the <see cref="Nullable"/> return type. |
| | 164 | | /// </summary> |
| | 165 | | /// <param name="value"> |
| | 166 | | /// The specified <see cref="Object"/> box. |
| | 167 | | /// </param> |
| | 168 | | /// <param name="formatExpression"> |
| | 169 | | /// The string format expression. |
| | 170 | | /// </param> |
| | 171 | | /// <returns> |
| | 172 | | /// Always returns a <see cref="Nullable"/> |
| | 173 | | /// as parse failure means <c>HasValue</c> |
| | 174 | | /// is false. |
| | 175 | | /// </returns> |
| | 176 | | public static string? ParseDateTimeWithFormat(object? value, string formatExpression) |
| 0 | 177 | | { |
| 0 | 178 | | string? s = (value != null) ? value.ToString() : string.Empty; |
| | 179 | |
|
| 0 | 180 | | return DateTime.TryParse(s, out var d) ? d.ToString(formatExpression, CultureInfo.CurrentCulture) : null; |
| 0 | 181 | | } |
| | 182 | |
|
| | 183 | | /// <summary> |
| | 184 | | /// Tries to convert the specified value |
| | 185 | | /// to the <see cref="Nullable"/> return type. |
| | 186 | | /// </summary> |
| | 187 | | /// <param name="value"> |
| | 188 | | /// The specified <see cref="Object"/> box. |
| | 189 | | /// </param> |
| | 190 | | /// <param name="formatExpression"> |
| | 191 | | /// The string format expression. |
| | 192 | | /// </param> |
| | 193 | | /// <param name="defaultValue"> |
| | 194 | | /// The default value to return when <c>Nullable.HasValue == false</c>. |
| | 195 | | /// </param> |
| | 196 | | /// <returns> |
| | 197 | | /// Always returns a <see cref="Nullable"/> |
| | 198 | | /// as parse failure means <c>HasValue</c> |
| | 199 | | /// is false. |
| | 200 | | /// </returns> |
| | 201 | | public static string ParseDateTimeWithFormat(object? value, string formatExpression, string defaultValue) |
| 0 | 202 | | { |
| 0 | 203 | | string? d = ParseDateTimeWithFormat(value, formatExpression); |
| | 204 | |
|
| 0 | 205 | | return string.IsNullOrWhiteSpace(d) ? defaultValue : d; |
| 0 | 206 | | } |
| | 207 | |
|
| | 208 | | /// <summary> |
| | 209 | | /// Tries to convert the specified value |
| | 210 | | /// to the <see cref="Nullable"/> return type. |
| | 211 | | /// </summary> |
| | 212 | | /// <param name="value"> |
| | 213 | | /// The specified <see cref="Object"/> box. |
| | 214 | | /// </param> |
| | 215 | | /// <returns> |
| | 216 | | /// Always returns a <see cref="Nullable"/> |
| | 217 | | /// as parse failure means <c>HasValue</c> |
| | 218 | | /// is false. |
| | 219 | | /// </returns> |
| | 220 | | public static decimal? ParseDecimal(object? value) |
| 0 | 221 | | { |
| 0 | 222 | | string? s = (value != null) ? value.ToString() : string.Empty; |
| | 223 | |
|
| 0 | 224 | | return decimal.TryParse(s, out var d) ? d : default(decimal?); |
| 0 | 225 | | } |
| | 226 | |
|
| | 227 | | /// <summary> |
| | 228 | | /// Tries to convert the specified value |
| | 229 | | /// to the <see cref="Nullable"/> return type. |
| | 230 | | /// </summary> |
| | 231 | | /// <param name="value"> |
| | 232 | | /// The specified <see cref="Object"/> box. |
| | 233 | | /// </param> |
| | 234 | | /// <param name="defaultValue"> |
| | 235 | | /// The default value to return when <c>Nullable.HasValue == false</c>. |
| | 236 | | /// </param> |
| | 237 | | /// <returns> |
| | 238 | | /// Always returns a <see cref="Nullable"/> |
| | 239 | | /// as parse failure means <c>HasValue</c> |
| | 240 | | /// is false. |
| | 241 | | /// </returns> |
| | 242 | | public static decimal? ParseDecimal(object? value, decimal defaultValue) |
| 0 | 243 | | { |
| 0 | 244 | | decimal? d = ParseDecimal(value); |
| | 245 | |
|
| 0 | 246 | | return d ?? new decimal?(defaultValue); |
| 0 | 247 | | } |
| | 248 | |
|
| | 249 | | /// <summary> |
| | 250 | | /// Tries to convert the specified value |
| | 251 | | /// to the <see cref="Nullable"/> return type. |
| | 252 | | /// </summary> |
| | 253 | | /// <param name="value"> |
| | 254 | | /// The specified <see cref="Object"/> box. |
| | 255 | | /// </param> |
| | 256 | | /// <returns> |
| | 257 | | /// Always returns a <see cref="Nullable"/> |
| | 258 | | /// as parse failure means <c>HasValue</c> |
| | 259 | | /// is false. |
| | 260 | | /// </returns> |
| | 261 | | public static double? ParseDouble(object? value) |
| 0 | 262 | | { |
| 0 | 263 | | string? s = value != null ? value.ToString() : string.Empty; |
| | 264 | |
|
| 0 | 265 | | return double.TryParse(s, out var d) ? d : default(double?); |
| 0 | 266 | | } |
| | 267 | |
|
| | 268 | | /// <summary> |
| | 269 | | /// Tries to convert the specified value |
| | 270 | | /// to the <see cref="Nullable"/> return type. |
| | 271 | | /// </summary> |
| | 272 | | /// <param name="value"> |
| | 273 | | /// The specified <see cref="Object"/> box. |
| | 274 | | /// </param> |
| | 275 | | /// <param name="defaultValue"> |
| | 276 | | /// The default value to return when <c>Nullable.HasValue == false</c>. |
| | 277 | | /// </param> |
| | 278 | | /// <returns> |
| | 279 | | /// Always returns a <see cref="Nullable"/> |
| | 280 | | /// as parse failure means <c>HasValue</c> |
| | 281 | | /// is false. |
| | 282 | | /// </returns> |
| | 283 | | public static double? ParseDouble(object? value, double defaultValue) |
| 0 | 284 | | { |
| 0 | 285 | | double? d = ParseDouble(value); |
| | 286 | |
|
| 0 | 287 | | return d ?? new double?(defaultValue); |
| 0 | 288 | | } |
| | 289 | |
|
| | 290 | | /// <summary> |
| | 291 | | /// Tries to convert the specified value |
| | 292 | | /// to the <see cref="Enum"/> return type. |
| | 293 | | /// </summary> |
| | 294 | | /// <typeparam name="TEnum">The type of the enum.</typeparam> |
| | 295 | | /// <param name="value">The value.</param> |
| | 296 | | /// <param name="defaultValue">The default value.</param> |
| | 297 | | /// <remarks> |
| | 298 | | /// For background, see http://stackoverflow.com/a/15017429/22944 |
| | 299 | | /// </remarks> |
| | 300 | | public static TEnum ParseEnum<TEnum>(string value, TEnum defaultValue) where TEnum : struct |
| 2 | 301 | | { |
| 2 | 302 | | var isDefined = !string.IsNullOrWhiteSpace(value) && Enum.IsDefined(typeof(TEnum), value); |
| | 303 | |
|
| 2 | 304 | | return isDefined ? (TEnum) Enum.Parse(typeof(TEnum), value) : defaultValue; |
| 2 | 305 | | } |
| | 306 | |
|
| | 307 | | /// <summary> |
| | 308 | | /// Tries to convert the specified value |
| | 309 | | /// to the <see cref="Nullable"/> return type. |
| | 310 | | /// </summary> |
| | 311 | | /// <param name="value"> |
| | 312 | | /// The specified <see cref="Object"/> box. |
| | 313 | | /// </param> |
| | 314 | | /// <returns> |
| | 315 | | /// Always returns a <see cref="Nullable"/> |
| | 316 | | /// as parse failure means <c>HasValue</c> |
| | 317 | | /// is false. |
| | 318 | | /// </returns> |
| | 319 | | public static Int16? ParseInt16(object? value) |
| 0 | 320 | | { |
| 0 | 321 | | string? s = (value != null) ? value.ToString() : string.Empty; |
| | 322 | |
|
| 0 | 323 | | return Int16.TryParse(s, out var i) ? i : default(Int16?); |
| 0 | 324 | | } |
| | 325 | |
|
| | 326 | | /// <summary> |
| | 327 | | /// Tries to convert the specified value |
| | 328 | | /// to the <see cref="Nullable"/> return type. |
| | 329 | | /// </summary> |
| | 330 | | /// <param name="value"> |
| | 331 | | /// The specified <see cref="Object"/> box. |
| | 332 | | /// </param> |
| | 333 | | /// <param name="defaultValue"> |
| | 334 | | /// The default value to return when <c>Nullable.HasValue == false</c>. |
| | 335 | | /// </param> |
| | 336 | | /// <returns> |
| | 337 | | /// Always returns a <see cref="Nullable"/> |
| | 338 | | /// as parse failure means <c>HasValue</c> |
| | 339 | | /// is false. |
| | 340 | | /// </returns> |
| | 341 | | public static Int16? ParseInt16(object? value, Int16 defaultValue) |
| 0 | 342 | | { |
| 0 | 343 | | Int16? i = ParseInt16(value); |
| | 344 | |
|
| 0 | 345 | | return i ?? new Int16?(defaultValue); |
| 0 | 346 | | } |
| | 347 | |
|
| | 348 | | /// <summary> |
| | 349 | | /// Tries to convert the specified value |
| | 350 | | /// to the <see cref="Nullable"/> return type. |
| | 351 | | /// </summary> |
| | 352 | | /// <param name="value"> |
| | 353 | | /// The specified <see cref="Object"/> box. |
| | 354 | | /// </param> |
| | 355 | | /// <returns> |
| | 356 | | /// Always returns a <see cref="Nullable"/> |
| | 357 | | /// as parse failure means <c>HasValue</c> |
| | 358 | | /// is false. |
| | 359 | | /// </returns> |
| | 360 | | public static Int32? ParseInt32(object? value) |
| 0 | 361 | | { |
| 0 | 362 | | string? s = value != null ? value.ToString() : string.Empty; |
| | 363 | |
|
| 0 | 364 | | return Int32.TryParse(s, out var i) ? i : default(Int32?); |
| 0 | 365 | | } |
| | 366 | |
|
| | 367 | | /// <summary> |
| | 368 | | /// Tries to convert the specified value |
| | 369 | | /// to the <see cref="Nullable"/> return type. |
| | 370 | | /// </summary> |
| | 371 | | /// <param name="value"> |
| | 372 | | /// The specified <see cref="Object"/> box. |
| | 373 | | /// </param> |
| | 374 | | /// <param name="defaultValue"> |
| | 375 | | /// The default value to return when <c>Nullable.HasValue == false</c>. |
| | 376 | | /// </param> |
| | 377 | | /// <returns> |
| | 378 | | /// Always returns a <see cref="Nullable"/> |
| | 379 | | /// as parse failure means <c>HasValue</c> |
| | 380 | | /// is false. |
| | 381 | | /// </returns> |
| | 382 | | public static Int32? ParseInt32(object? value, Int32 defaultValue) |
| 0 | 383 | | { |
| 0 | 384 | | Int32? i = ParseInt32(value); |
| | 385 | |
|
| 0 | 386 | | return i ?? new Int32?(defaultValue); |
| 0 | 387 | | } |
| | 388 | |
|
| | 389 | | /// <summary> |
| | 390 | | /// Tries to convert the specified value |
| | 391 | | /// to the <see cref="Nullable"/> return type. |
| | 392 | | /// </summary> |
| | 393 | | /// <param name="value"> |
| | 394 | | /// The specified <see cref="Object"/> box. |
| | 395 | | /// </param> |
| | 396 | | /// <returns> |
| | 397 | | /// Always returns a <see cref="Nullable"/> |
| | 398 | | /// as parse failure means <c>HasValue</c> |
| | 399 | | /// is false. |
| | 400 | | /// </returns> |
| | 401 | | public static Int64? ParseInt64(object? value) |
| 0 | 402 | | { |
| 0 | 403 | | string? s = value != null ? value.ToString() : string.Empty; |
| | 404 | |
|
| 0 | 405 | | return Int64.TryParse(s, out var i) ? i : default(Int64?); |
| 0 | 406 | | } |
| | 407 | |
|
| | 408 | | /// <summary> |
| | 409 | | /// Tries to convert the specified value |
| | 410 | | /// to the <see cref="Nullable"/> return type. |
| | 411 | | /// </summary> |
| | 412 | | /// <param name="value"> |
| | 413 | | /// The specified <see cref="Object"/> box. |
| | 414 | | /// </param> |
| | 415 | | /// <param name="defaultValue"> |
| | 416 | | /// The default value to return when <c>Nullable.HasValue == false</c>. |
| | 417 | | /// </param> |
| | 418 | | /// <returns> |
| | 419 | | /// Always returns a <see cref="Nullable"/> |
| | 420 | | /// as parse failure means <c>HasValue</c> |
| | 421 | | /// is false. |
| | 422 | | /// </returns> |
| | 423 | | public static Int64? ParseInt64(object? value, Int64 defaultValue) |
| 0 | 424 | | { |
| 0 | 425 | | Int64? i = ParseInt64(value); |
| | 426 | |
|
| 0 | 427 | | return i ?? new Int64?(defaultValue); |
| 0 | 428 | | } |
| | 429 | |
|
| | 430 | | /// <summary> |
| | 431 | | /// Parses the RFC3339 date and time. |
| | 432 | | /// </summary> |
| | 433 | | /// <param name="value">The value.</param> |
| | 434 | | /// <remarks> |
| | 435 | | /// This member is based on patterns in the Argotic Syndication Framework (http://www.codeplex.com/Argotic). |
| | 436 | | /// </remarks> |
| | 437 | | public static DateTime ParseRfc3339DateTime(string? value) |
| 0 | 438 | | { |
| 0 | 439 | | value.ThrowWhenNullOrWhiteSpace(); |
| | 440 | |
|
| 0 | 441 | | if (!TryParseRfc3339DateTime(value, out var minValue)) |
| 0 | 442 | | { |
| 0 | 443 | | throw new FormatException(string.Format(CultureInfo.CurrentCulture, |
| 0 | 444 | | "'{0}' is not a valid RFC-3339 formatted date-time value.", new object[] {value})); |
| | 445 | | } |
| | 446 | |
|
| 0 | 447 | | return minValue; |
| 0 | 448 | | } |
| | 449 | |
|
| | 450 | | /// <summary> |
| | 451 | | /// Parses the RFC822 date and time. |
| | 452 | | /// </summary> |
| | 453 | | /// <param name="value">The value.</param> |
| | 454 | | /// <remarks> |
| | 455 | | /// This member is based on patterns in the Argotic Syndication Framework (http://www.codeplex.com/Argotic). |
| | 456 | | /// </remarks> |
| | 457 | | public static DateTime ParseRfc822DateTime(string value) |
| 10 | 458 | | { |
| 10 | 459 | | value.ThrowWhenNullOrWhiteSpace(); |
| | 460 | |
|
| 10 | 461 | | if (!TryParseRfc822DateTime(value, out var minValue)) |
| 0 | 462 | | { |
| 0 | 463 | | throw new FormatException(string.Format(CultureInfo.CurrentCulture, |
| 0 | 464 | | "'{0}' is not a valid RFC-822 formatted date-time value.", new object[] {value})); |
| | 465 | | } |
| | 466 | |
|
| 10 | 467 | | return minValue; |
| 10 | 468 | | } |
| | 469 | |
|
| | 470 | | /// <summary> |
| | 471 | | /// Tries to convert the specified value |
| | 472 | | /// to a <see cref="String"/>. |
| | 473 | | /// </summary> |
| | 474 | | /// <param name="value"> |
| | 475 | | /// The specified <see cref="Object"/> box. |
| | 476 | | /// </param> |
| 0 | 477 | | public static string? ParseString(object? value) => value?.ToString(); |
| | 478 | |
|
| | 479 | | /// <summary> |
| | 480 | | /// Tries to convert the specified value |
| | 481 | | /// to a <see cref="String"/>. |
| | 482 | | /// </summary> |
| | 483 | | /// <param name="value">The value.</param> |
| | 484 | | /// <param name="defaultValue">The default value.</param> |
| | 485 | | public static string? ParseString(object? value, string? defaultValue) => |
| 0 | 486 | | value != null ? value.ToString() : defaultValue; |
| | 487 | |
|
| | 488 | | /// <summary> |
| | 489 | | /// Tries the parse RFC3339 date and time. |
| | 490 | | /// </summary> |
| | 491 | | /// <param name="value">The value.</param> |
| | 492 | | /// <param name="result">The result.</param> |
| | 493 | | /// <remarks> |
| | 494 | | /// This member is based on patterns in the Argotic Syndication Framework (http://www.codeplex.com/Argotic). |
| | 495 | | /// </remarks> |
| | 496 | | public static bool TryParseRfc3339DateTime(string value, out DateTime result) |
| 0 | 497 | | { |
| 0 | 498 | | DateTimeFormatInfo dateTimeFormat = CultureInfo.InvariantCulture.DateTimeFormat; |
| 0 | 499 | | string[] formats = |
| 0 | 500 | | { |
| 0 | 501 | | dateTimeFormat.SortableDateTimePattern, dateTimeFormat.UniversalSortableDateTimePattern, |
| 0 | 502 | | "yyyy'-'MM'-'dd'T'HH:mm:ss'Z'", "yyyy'-'MM'-'dd'T'HH:mm:ss.f'Z'", "yyyy'-'MM'-'dd'T'HH:mm:ss.ff'Z'", |
| 0 | 503 | | "yyyy'-'MM'-'dd'T'HH:mm:ss.fff'Z'", "yyyy'-'MM'-'dd'T'HH:mm:ss.ffff'Z'", |
| 0 | 504 | | "yyyy'-'MM'-'dd'T'HH:mm:ss.fffff'Z'", "yyyy'-'MM'-'dd'T'HH:mm:ss.ffffff'Z'", "yyyy'-'MM'-'dd'T'HH:mm:sszzz", |
| 0 | 505 | | "yyyy'-'MM'-'dd'T'HH:mm:ss.ffzzz", "yyyy'-'MM'-'dd'T'HH:mm:ss.fffzzz", "yyyy'-'MM'-'dd'T'HH:mm:ss.ffffzzz", |
| 0 | 506 | | "yyyy'-'MM'-'dd'T'HH:mm:ss.fffffzzz", "yyyy'-'MM'-'dd'T'HH:mm:ss.ffffffzzz" |
| 0 | 507 | | }; |
| | 508 | |
|
| 0 | 509 | | if (string.IsNullOrWhiteSpace(value)) |
| 0 | 510 | | { |
| 0 | 511 | | result = DateTime.MinValue; |
| 0 | 512 | | return false; |
| | 513 | | } |
| | 514 | |
|
| 0 | 515 | | return DateTime.TryParseExact(value, formats, dateTimeFormat, DateTimeStyles.AssumeUniversal, out result); |
| 0 | 516 | | } |
| | 517 | |
|
| | 518 | | /// <summary> |
| | 519 | | /// Tries the parse RFC822 date and time. |
| | 520 | | /// </summary> |
| | 521 | | /// <param name="value">The value.</param> |
| | 522 | | /// <param name="result">The result.</param> |
| | 523 | | /// <remarks> |
| | 524 | | /// This member is based on patterns in the Argotic Syndication Framework (http://www.codeplex.com/Argotic). |
| | 525 | | /// </remarks> |
| | 526 | | public static bool TryParseRfc822DateTime(string value, out DateTime result) |
| 10 | 527 | | { |
| 10 | 528 | | DateTimeFormatInfo dateTimeFormat = CultureInfo.InvariantCulture.DateTimeFormat; |
| 10 | 529 | | string[] formats = |
| 10 | 530 | | {dateTimeFormat.RFC1123Pattern, "ddd',' d MMM yyyy HH:mm:ss zzz", "ddd',' dd MMM yyyy HH:mm:ss zzz"}; |
| | 531 | |
|
| 10 | 532 | | if (string.IsNullOrWhiteSpace(value)) |
| 0 | 533 | | { |
| 0 | 534 | | result = DateTime.MinValue; |
| 0 | 535 | | return false; |
| | 536 | | } |
| | 537 | |
|
| 10 | 538 | | return DateTime.TryParseExact(ReplaceRfc822TimeZoneWithOffset(value), formats, dateTimeFormat, |
| 10 | 539 | | DateTimeStyles.None, out result); |
| 10 | 540 | | } |
| | 541 | |
|
| | 542 | | static string ReplaceRfc822TimeZoneWithOffset(string? value) |
| 10 | 543 | | { |
| 10 | 544 | | value.ThrowWhenNullOrWhiteSpace(); |
| | 545 | |
|
| 10 | 546 | | value = value.Trim(); |
| 10 | 547 | | if (value.EndsWith("UT", StringComparison.OrdinalIgnoreCase)) |
| 0 | 548 | | { |
| 0 | 549 | | return string.Format(CultureInfo.CurrentCulture, "{0}GMT", |
| 0 | 550 | | new object[] {value.TrimEnd("UT".ToCharArray())}); |
| | 551 | | } |
| | 552 | |
|
| 10 | 553 | | if (value.EndsWith("EST", StringComparison.OrdinalIgnoreCase)) |
| 0 | 554 | | { |
| 0 | 555 | | return string.Format(CultureInfo.CurrentCulture, "{0}-05:00", |
| 0 | 556 | | new object[] {value.TrimEnd("EST".ToCharArray())}); |
| | 557 | | } |
| | 558 | |
|
| 10 | 559 | | if (value.EndsWith("EDT", StringComparison.OrdinalIgnoreCase)) |
| 0 | 560 | | { |
| 0 | 561 | | return string.Format(CultureInfo.CurrentCulture, "{0}-04:00", |
| 0 | 562 | | new object[] {value.TrimEnd("EDT".ToCharArray())}); |
| | 563 | | } |
| | 564 | |
|
| 10 | 565 | | if (value.EndsWith("CST", StringComparison.OrdinalIgnoreCase)) |
| 0 | 566 | | { |
| 0 | 567 | | return string.Format(CultureInfo.CurrentCulture, "{0}-06:00", |
| 0 | 568 | | new object[] {value.TrimEnd("CST".ToCharArray())}); |
| | 569 | | } |
| | 570 | |
|
| 10 | 571 | | if (value.EndsWith("CDT", StringComparison.OrdinalIgnoreCase)) |
| 0 | 572 | | { |
| 0 | 573 | | return string.Format(CultureInfo.CurrentCulture, "{0}-05:00", |
| 0 | 574 | | new object[] {value.TrimEnd("CDT".ToCharArray())}); |
| | 575 | | } |
| | 576 | |
|
| 10 | 577 | | if (value.EndsWith("MST", StringComparison.OrdinalIgnoreCase)) |
| 0 | 578 | | { |
| 0 | 579 | | return string.Format(CultureInfo.CurrentCulture, "{0}-07:00", |
| 0 | 580 | | new object[] {value.TrimEnd("MST".ToCharArray())}); |
| | 581 | | } |
| | 582 | |
|
| 10 | 583 | | if (value.EndsWith("MDT", StringComparison.OrdinalIgnoreCase)) |
| 0 | 584 | | { |
| 0 | 585 | | return string.Format(CultureInfo.CurrentCulture, "{0}-06:00", |
| 0 | 586 | | new object[] {value.TrimEnd("MDT".ToCharArray())}); |
| | 587 | | } |
| | 588 | |
|
| 10 | 589 | | if (value.EndsWith("PST", StringComparison.OrdinalIgnoreCase)) |
| 0 | 590 | | { |
| 0 | 591 | | return string.Format(CultureInfo.CurrentCulture, "{0}-08:00", |
| 0 | 592 | | new object[] {value.TrimEnd("PST".ToCharArray())}); |
| | 593 | | } |
| | 594 | |
|
| 10 | 595 | | if (value.EndsWith("PDT", StringComparison.OrdinalIgnoreCase)) |
| 0 | 596 | | { |
| 0 | 597 | | return string.Format(CultureInfo.CurrentCulture, "{0}-07:00", |
| 0 | 598 | | new object[] {value.TrimEnd("PDT".ToCharArray())}); |
| | 599 | | } |
| | 600 | |
|
| 10 | 601 | | if (value.EndsWith("Z", StringComparison.OrdinalIgnoreCase)) |
| 0 | 602 | | { |
| 0 | 603 | | return string.Format(CultureInfo.CurrentCulture, "{0}GMT", new object[] {value.TrimEnd("Z".ToCharArray())}); |
| | 604 | | } |
| | 605 | |
|
| 10 | 606 | | if (value.EndsWith("A", StringComparison.OrdinalIgnoreCase)) |
| 0 | 607 | | { |
| 0 | 608 | | return string.Format(CultureInfo.CurrentCulture, "{0}-01:00", |
| 0 | 609 | | new object[] {value.TrimEnd("A".ToCharArray())}); |
| | 610 | | } |
| | 611 | |
|
| 10 | 612 | | if (value.EndsWith("M", StringComparison.OrdinalIgnoreCase)) |
| 0 | 613 | | { |
| 0 | 614 | | return string.Format(CultureInfo.CurrentCulture, "{0}-12:00", |
| 0 | 615 | | new object[] {value.TrimEnd("M".ToCharArray())}); |
| | 616 | | } |
| | 617 | |
|
| 10 | 618 | | if (value.EndsWith("N", StringComparison.OrdinalIgnoreCase)) |
| 0 | 619 | | { |
| 0 | 620 | | return string.Format(CultureInfo.CurrentCulture, "{0}+01:00", |
| 0 | 621 | | new object[] {value.TrimEnd("N".ToCharArray())}); |
| | 622 | | } |
| | 623 | |
|
| 10 | 624 | | if (value.EndsWith("Y", StringComparison.OrdinalIgnoreCase)) |
| 0 | 625 | | { |
| 0 | 626 | | return string.Format(CultureInfo.CurrentCulture, "{0}+12:00", |
| 0 | 627 | | new object[] {value.TrimEnd("Y".ToCharArray())}); |
| | 628 | | } |
| | 629 | |
|
| 10 | 630 | | return value; |
| 10 | 631 | | } |
| | 632 | | } |