|
16 | 16 | using System.Globalization;
|
17 | 17 | using System.IO;
|
18 | 18 | using System.Text.RegularExpressions;
|
| 19 | +using System.Collections.Generic; |
19 | 20 |
|
20 | 21 | namespace RestSharp
|
21 | 22 | {
|
@@ -47,39 +48,75 @@ public static bool IsSubclassOfRawGeneric(this Type toCheck, Type generic) {
|
47 | 48 | }
|
48 | 49 |
|
49 | 50 | public static DateTime ParseJsonDate(this string input) {
|
50 |
| - if (input.Contains("/Date(")) { |
51 |
| - var regex = new Regex(@"\\/Date\((\d+)(-|\+)?([0-9]{4})?\)\\/"); |
52 |
| - if (regex.IsMatch(input)) { |
53 |
| - var matches = regex.Matches(input); |
54 |
| - var match = matches[0]; |
55 |
| - var ms = Convert.ToInt64(match.Groups[1].Value); |
56 |
| - var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); |
57 |
| - var dt = epoch.AddMilliseconds(ms); |
58 |
| - |
59 |
| - // adjust if time zone modifier present |
60 |
| - if (match.Groups[3] != null) { |
61 |
| - var mod = DateTime.ParseExact(match.Groups[3].Value, "hhmm", CultureInfo.InvariantCulture); |
62 |
| - if (match.Groups[2].Value == "+") { |
63 |
| - dt = dt.Add(mod.TimeOfDay); |
64 |
| - } |
65 |
| - else { |
66 |
| - dt = dt.Subtract(mod.TimeOfDay); |
67 |
| - } |
68 |
| - } |
| 51 | + input = input.Replace("\n", ""); |
| 52 | + input = input.Replace("\r", ""); |
69 | 53 |
|
70 |
| - return dt; |
71 |
| - } |
| 54 | + if (input.StartsWith("\"")) { |
| 55 | + // remove leading/trailing quotes |
| 56 | + input = input.Substring(1, input.Length - 2); |
| 57 | + } |
| 58 | + |
| 59 | + if (input.Contains("/Date(")) { |
| 60 | + return ExtractDate(input, @"\\/Date\((-?\d+)(-|\+)?([0-9]{4})?\)\\/"); |
72 | 61 | }
|
73 | 62 | else if (input.Contains("new Date(")) {
|
74 |
| - // TODO: implement parsing |
| 63 | + input = input.Replace(" ", ""); |
| 64 | + // because all whitespace is removed, match against newDate( instead of new Date( |
| 65 | + return ExtractDate(input, @"newDate\((-?\d+)*\)"); |
| 66 | + } |
| 67 | + else if (input.Matches(@"([0-9-])*T([0-9\:]*)Z?")) { |
| 68 | + return ParseIso8601Date(input); |
75 | 69 | }
|
76 |
| - else if (input.Matches(@"([0-9-])*T([0-9\:]*)Z")) { |
77 |
| - // TODO: implement parsing |
| 70 | + |
| 71 | + return default(DateTime); |
| 72 | + } |
| 73 | + |
| 74 | + private static DateTime ParseIso8601Date(string input) { |
| 75 | + var formats = new string[] { |
| 76 | + "u", |
| 77 | + "s", |
| 78 | + "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'", |
| 79 | + "yyyy-MM-ddTHH:mm:ssZ", |
| 80 | + "yyyy-MM-dd HH:mm:ssZ", |
| 81 | + "yyyy-MM-ddTHH:mm:ss", |
| 82 | + "yyyy-MM-ddTHH:mm:sszzzzzz" |
| 83 | + }; |
| 84 | + |
| 85 | + DateTime date; |
| 86 | + if (DateTime.TryParseExact(input, formats, |
| 87 | + CultureInfo.InvariantCulture, |
| 88 | + DateTimeStyles.None, out date)) { |
| 89 | + return date; |
78 | 90 | }
|
79 | 91 |
|
80 | 92 | return default(DateTime);
|
81 | 93 | }
|
82 | 94 |
|
| 95 | + private static DateTime ExtractDate(string input, string pattern) { |
| 96 | + DateTime dt = DateTime.MinValue; |
| 97 | + var regex = new Regex(pattern); |
| 98 | + if (regex.IsMatch(input)) { |
| 99 | + var matches = regex.Matches(input); |
| 100 | + var match = matches[0]; |
| 101 | + var ms = Convert.ToInt64(match.Groups[1].Value); |
| 102 | + var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); |
| 103 | + dt = epoch.AddMilliseconds(ms); |
| 104 | + |
| 105 | + // adjust if time zone modifier present |
| 106 | + if (match.Groups.Count > 2 && match.Groups[3] != null) { |
| 107 | + var mod = DateTime.ParseExact(match.Groups[3].Value, "hhmm", CultureInfo.InvariantCulture); |
| 108 | + if (match.Groups[2].Value == "+") { |
| 109 | + dt = dt.Add(mod.TimeOfDay); |
| 110 | + } |
| 111 | + else { |
| 112 | + dt = dt.Subtract(mod.TimeOfDay); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + } |
| 117 | + return dt; |
| 118 | + } |
| 119 | + |
83 | 120 | public static bool Matches(this string input, string pattern) {
|
84 | 121 | return Regex.IsMatch(input, pattern);
|
85 | 122 | }
|
|
0 commit comments