Skip to content

Commit 0274695

Browse files
author
Dom
committed
looking at doing a simple deserialization if RFC1123 datetime
1 parent 99eac1e commit 0274695

4 files changed

Lines changed: 51 additions & 0 deletions

File tree

src/ServiceStack.Text/Common/DateTimeSerializer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ private static DateTime Prepare(this DateTime dateTime, bool parsedAsUtc=false)
6363
return ParseShortestXsdDateTime(dateTimeStr);
6464
}
6565

66+
public static DateTime ParseRFC1123DateTime(string dateTimeStr)
67+
{
68+
return DateTime.ParseExact(dateTimeStr, "r", CultureInfo.InvariantCulture);
69+
}
70+
6671
public static DateTime ParseShortestXsdDateTime(string dateTimeStr)
6772
{
6873
if (string.IsNullOrEmpty(dateTimeStr))

src/ServiceStack.Text/Common/DeserializeType.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ public static object ParseQuotedPrimitive(string value)
147147
}
148148
}
149149

150+
if (JsConfig.DateHandler == JsonDateHandler.RFC1123)
151+
{
152+
// check that we have RFC1123 date:
153+
// ddd, dd MMM yyyy HH:mm:ss GMT
154+
if (value.Length == 29 && (value.EndsWithInvariant("GMT")))
155+
{
156+
return DateTimeSerializer.ParseRFC1123DateTime(value);
157+
}
158+
}
159+
150160
return Serializer.UnescapeString(value);
151161
}
152162

tests/ServiceStack.Text.Tests/JsonTests/JsonDateTimeTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,35 @@ public void Can_serialize_json_date_rfc1123_unspecified()
524524
JsConfig.Reset();
525525
}
526526

527+
[Test]
528+
public void Can_deserialize_json_date_rfc1123_local()
529+
{
530+
JsConfig.DateHandler = JsonDateHandler.RFC1123;
531+
532+
const string json = @"""Tue, 12 Nov 2013 14:32:07 GMT""";
533+
var fromJson = JsonSerializer.DeserializeFromString<DateTime>(json);
534+
535+
var dateTime = new DateTime(2013, 11, 12, 14, 32, 07, DateTimeKind.Local);
536+
Assert.That(fromJson, Is.EqualTo(dateTime));
537+
Assert.That(fromJson.Kind, Is.EqualTo(dateTime.Kind));
538+
JsConfig.Reset();
539+
}
540+
541+
[Test]
542+
public void Can_deserialize_json_date_rfc1123_always_utc()
543+
{
544+
JsConfig.AlwaysUseUtc = true;
545+
JsConfig.DateHandler = JsonDateHandler.RFC1123;
546+
547+
const string json = @"""Tue, 12 Nov 2013 14:32:07 GMT""";
548+
var fromJson = JsonSerializer.DeserializeFromString<DateTime>(json);
549+
550+
var dateTime = new DateTime(2013, 11, 12, 14, 32, 07, DateTimeKind.Utc);
551+
Assert.That(fromJson, Is.EqualTo(dateTime));
552+
Assert.That(fromJson.Kind, Is.EqualTo(dateTime.Kind));
553+
JsConfig.Reset();
554+
}
555+
527556
#endregion
528557

529558
#region InteropTests

tests/ServiceStack.Text.Tests/Utils/DateTimeSerializerTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ public void ParseShortestXsdDateTime_works()
105105
Assert.That (shortDate, Is.EqualTo(new DateTime (2011, 9, 4)), "Day without leading 0");
106106
}
107107

108+
[Test]
109+
public void ParseRFC1123DateTime_works()
110+
{
111+
DateTime rfc1123Date = DateTimeSerializer.ParseRFC1123DateTime("Tue, 12 Nov 2013 14:32:07 GMT");
112+
Assert.That(rfc1123Date, Is.EqualTo(new DateTime(2013, 11, 12, 14,32,07)));
113+
}
114+
108115
[Test]
109116
public void TestSqlServerDateTime()
110117
{

0 commit comments

Comments
 (0)