DateTime vs DateTimeOffset for ISO8601 format (possible bug?)

1,314 views
Skip to first unread message

Stuart Bale

unread,
May 24, 2012, 3:25:22 PM5/24/12
to servic...@googlegroups.com
Running these two tests:
            JsConfig.DateHandler = JsonDateHandler.ISO8601;
            const string json = @"2006-07-05T12:00:00.000+0000";

// This works ..
            var dateTimeOffset = JsonSerializer.DeserializeFromString<DateTimeOffset>(json);

// This throws an exception ..
            var dateTime = JsonSerializer.DeserializeFromString<DateTime>(json);

Exception Details: The string '2006-07-05T12:00:00.000+0000' is not a valid AllXsd value.
Stack Trace:
   at System.Xml.Schema.XsdDateTime..ctor(String text, XsdDateTimeFlags kinds)
   at System.Xml.XmlConvert.ToDateTime(String s, XmlDateTimeSerializationMode dateTimeOption)
   at ServiceStack.Text.Common.DateTimeSerializer.ParseShortestXsdDateTime(String dateTimeStr) in C:\src\ServiceStack.Text\src\ServiceStack.Text\Common\DateTimeSerializer.cs:line 60
   at ServiceStack.Text.Common.DeserializeBuiltin`1.<GetParseFn>b__b(String value) in C:\src\ServiceStack.Text\src\ServiceStack.Text\Common\DeserializeBuiltin.cs:line 58
   at ServiceStack.Text.Common.JsReader`1.<>c__DisplayClass7`1.<GetParseFn>b__2(String value) in C:\src\ServiceStack.Text\src\ServiceStack.Text\Common\JsReader.cs:line 41
   at ServiceStack.Text.Json.JsonReader`1.Parse(String value) in C:\src\ServiceStack.Text\src\ServiceStack.Text\Json\JsonReader.Generic.cs:line 82
   at ServiceStack.Text.JsonSerializer.DeserializeFromString[T](String value) in C:\src\ServiceStack.Text\src\ServiceStack.Text\JsonSerializer.cs:line 33

Interestingly, changing the json to this:

            const string json = @"2006-07-05T12:00:00.000+00:00";

works ok - the difference (albeit subtle), is the colon in the time offset portion.

Smells like a bug - can anyone else confirm?

Demis Bellot

unread,
May 24, 2012, 3:28:33 PM5/24/12
to servic...@googlegroups.com
How did you serialize the DateTime String? with the JsonSerializer?
If so, can you submit a failing test we can look at?

Stuart Bale

unread,
May 24, 2012, 3:43:03 PM5/24/12
to servic...@googlegroups.com
Hi Demis,

The json string is generated from a different source, but does comply with ISO 8601 standard, which supports the timezone part to either include or exclude a : (colon).  According to the standard..

4.2.5.1 Difference between local time and UTC of day 
When it is required to indicate the difference between local time and UTC of day, the representation of the 
difference can be expressed in hours and minutes, or hours only. It shall be expressed as positive (i.e. with 
the leading plus sign [+]) if the local time is ahead of or equal to UTC of day and as negative (i.e. with the ISO leading minus sign [-]) if it is behind UTC of day. The minutes time element of the difference may only be 
omitted if the difference between the time scales is exactly an integral number of hours. 

Example:
Basic format: 
±hhmm +0100 
±hh   +01 
Extended format: 
±hh:mm +01:00

And here's a more useful publicly available summary:

Hope that helps.

S

Stuart Bale

unread,
May 24, 2012, 3:53:20 PM5/24/12
to servic...@googlegroups.com
Also, interestingly it deserializes correctly to DateTimeOffset, but not DateTime - I imagine there must be a different codepath between the two?
S

Demis Bellot

unread,
May 24, 2012, 3:53:58 PM5/24/12
to servic...@googlegroups.com
Right, well submitting a failing unit test, would help the original author (i.e. its not me) find and fix the issue quicker. Somewhere here would be good:

Although for a truly speedy resolution - feel free to submit the fix as well :)

Stuart Bale

unread,
May 24, 2012, 4:04:56 PM5/24/12
to servic...@googlegroups.com
OK, cool - gotcha.

Not familiar with github, but will take a look over the weekend.

S
Reply all
Reply to author
Forward
0 new messages