New Behavior reading dates

Oct 7, 2012 at 12:46 AM

I'm hoping for some understanding on the Json textReader, it seems that it may be reading dates differently now.  Is it actually parsing the date string, to a date?  If so, how does it handle: RFC3339 dates?  Previously, I had written date parsing code to parse the Google+ dates, like this one:  "updated": "2012-09-29T07:59:15.373Z".

So now, with my parse routine it breaks because this reader Value return a valid date:

var val = reader.Value.ToString();
createdAt = val.ParseGooglePlusDateTime();

So it now breaks on val.ParseGooglePlusDateTime(), because val is now a date!  Before, it was a string value.  So, something has changed and can I rely on the date I am getting from the Json textReader?  I mean it is accurate to which timezone, or is it the UTC date?


Oct 7, 2012 at 1:56 AM

From here:

..and using the first 2 examples, JsonTextReader is translating the first date (of UTC type) but then not calculating the offset from UTC in the 2nd case.  Perhaps there is no consideration for this offset, but then it is attempting to parse to a date and it does get it wrong.  Here is my sample code and results:


        public void testRFC3399()
            DateTime[] expected = new DateTime[] { 
                new DateTime(1985, 4, 12, 23,20,50,520, DateTimeKind.Utc),
                new DateTime(1996,12,20,0,39,57,DateTimeKind.Utc)
            string json = "{\"dates\":[{\"date\":\"1985-04-12T23:20:50.52Z\"}," +
                          "{\"date\":\"1996-12-19T16:39:57-08:00\"}" +
            StringReader txt = new StringReader(json);
            Newtonsoft.Json.JsonTextReader reader = new Newtonsoft.Json.JsonTextReader(txt);
            int x = 0;
            while (reader.Read())
                if (reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName && reader.Value.Equals("date"))
                    var val = reader.Value;
                    Console.WriteLine("{0}:{1}:{2}", reader.TokenType, reader.ValueType, val);
                    Assert.AreEqual(expected[x++], val);


==== results ====

Date:System.DateTime: 4/12/1985 11:20:50 PM (correct!)
Date:System.DateTime: 12/19/1996 5:39:57 PM (expecting utc value, format irrelevant: 1996-12-20T00:39:57)