Skip to content

Commit 7d70af4

Browse files
committed
Merge pull request #194 from nberardi/patch-1
Added support for changing raw unix time to DateTime.
2 parents dbb7241 + 240225d commit 7d70af4

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

RestSharp.Tests/JsonTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,17 @@ public void Can_Deserialize_JScript_Json_Dates()
312312
Assert.Equal(new DateTime(1910, 9, 25, 9, 30, 25, DateTimeKind.Utc), bd.Value);
313313
}
314314

315+
[Fact]
316+
public void Can_Deserialize_Unix_Json_Dates()
317+
{
318+
var doc = CreateUnixDateJson();
319+
var d = new JsonDeserializer();
320+
var response = new RestResponse { Content = doc };
321+
var bd = d.Deserialize<Birthdate>(response);
322+
323+
Assert.Equal(new DateTime(2011, 6, 30, 8, 15, 46, DateTimeKind.Utc), bd.Value);
324+
}
325+
315326
[Fact]
316327
public void Can_Deserialize_JsonNet_Dates()
317328
{
@@ -511,6 +522,14 @@ private string CreateJScriptDateJson()
511522
return JsonConvert.SerializeObject(bd, new JavaScriptDateTimeConverter());
512523
}
513524

525+
private string CreateUnixDateJson()
526+
{
527+
var doc = new JObject();
528+
doc["Value"] = 1309421746;
529+
530+
return doc.ToString();
531+
}
532+
514533
private string CreateJson()
515534
{
516535
var doc = new JObject();

RestSharp/Extensions/StringExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ public static DateTime ParseJsonDate(this string input, CultureInfo culture)
101101

102102
input = input.RemoveSurroundingQuotes();
103103

104+
long unix;
105+
if (Int64.TryParse(input, out unix))
106+
{
107+
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
108+
return epoch.AddSeconds(unix);
109+
}
110+
104111
if (input.Contains("/Date("))
105112
{
106113
return ExtractDate(input, @"\\/Date\((-?\d+)(-|\+)?([0-9]{4})?\)\\/", culture);

0 commit comments

Comments
 (0)