Skip to content

Commit

Permalink
- Added datetime with milliseconds format to StringExtensions
Browse files Browse the repository at this point in the history
- Added DateTimeResponse class to Test/SampleClasses/misc.cs
- Added test for fix
  • Loading branch information
mstrYoda committed Oct 5, 2018
1 parent 13c3e6b commit d08ba7a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
12 changes: 12 additions & 0 deletions RestSharp.Tests/JsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,18 @@ public void Can_Deserialize_Dictionary_with_Null()
Assert.IsNull(dictionary["Null"]);
}

[Test]
public void Can_Deserialize_Date_With_Milliseconds()
{
const string content = "{ \"CreatedOn\": \"2018-10-01T14:39:00.234Z\" }";
JsonDeserializer json = new JsonDeserializer();
DateTimeResponse output = json.Deserialize<DateTimeResponse>(new RestResponse { Content = content });
DateTime expected = DateTime.Parse("2018-10-01 14:39:00.234", CultureInfo.InvariantCulture);

Assert.NotNull(output);
Assert.AreEqual(expected.ToString(), output.CreatedOn.ToString());
}

private static string CreateJsonWithUnderscores()
{
JsonObject doc = new JsonObject();
Expand Down
5 changes: 5 additions & 0 deletions RestSharp.Tests/SampleClasses/misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,9 @@ public class WrongNote
[DeserializeAs(Content = true)]
public string Text { get; set; }
}

public class DateTimeResponse
{
public DateTime CreatedOn { get; set; }
}
}
3 changes: 2 additions & 1 deletion RestSharp/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,14 @@ private static DateTime ParseFormattedDate(string input, CultureInfo culture)
"yyyy-MM-dd HH:mm:ssZ",
"yyyy-MM-ddTHH:mm:ss",
"yyyy-MM-ddTHH:mm:sszzzzzz",
"yyyy-MM-ddTHH:mm:ss.fffZ",
"M/d/yyyy h:mm:ss tt" // default format for invariant culture
};

if (DateTime.TryParseExact(input, formats, culture,
DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out var date))
return date;

return DateTime.TryParse(input, culture, DateTimeStyles.None, out date) ? date : default(DateTime);
}

Expand Down

0 comments on commit d08ba7a

Please sign in to comment.