Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Providing DateTime values in OData

I'm currently writing a special client application to allow our unit tests to work with an OData interface using the XML structure for atom feeds. All seems to be working properly, but i'm running into trouble when I need to pass a DateTime value as property.

I've written the following code that extracts the DateTime value from the property of the object and stores it in a specific format:

private static void GenerateProperty<T>(StringBuilder xml, T obj, PropertyInfo info)
        {
            // Extract the information about the property if it contains a value.
            if (info.GetValue(obj, null) == null) return;
            string type = info.GetGetMethod().ReturnType.ToString().Split('.').Last();
            string value = info.GetValue(obj, null).ToString();
            if (type == "DateTime")
                value = ((DateTime)info.GetValue(obj, null)).ToString("yyyy-mm-ddThh:mm:ss");
            if (type == "Boolean") value = value.ToLower();

            // Append the property to the generated XML.
            xml.Append(type.ToLower().Equals("string") ? 
                    string.Format("<d:{0}>{1}</d:{0}>", info.Name, value) : 
                    string.Format("<d:{0} m:type=\"Edm.{1}\">{2}</d:{0}>", info.Name, type, value));
        }

The code is heavy on reflection, but that's beside the point. The values returned by this code for a DateTime are in the following format: 2011-49-13T11:49:41Z

However, i'm receiving the following error from my OData Service:

Error processing request stream. Error encountered in converting the value from request payload for property 'Created' to type 'System.DateTime', which is the property's expected type. See inner exception for more detail. The string '2011-49-13T11:49:41Z' is not a valid AllXsd value. System.FormatException at System.Xml.XmlConvert.ToDateTime(String s, XmlDateTimeSerializationMode dateTimeOption) at System.Data.Services.Parsing.WebConvert.StringToPrimitive(String text, Type targetType) at System.Data.Services.Serializers.PlainXmlDeserializer.ConvertValuesForXml(Object value, String propertyName, Type typeToBeConverted)

So apparently it doesn't understand the DateTime format, but when I look at the documentation that's posted here: http://www.odata.org/developers/protocols/overview#AbstractTypeSystem

I'd expect it to be valid. Anyone have any experience with this?

like image 542
codingbunny Avatar asked Sep 13 '11 09:09

codingbunny


2 Answers

yyyy-mm-ddThh:mm:ss

should be

yyyy-MM-ddTHH:mm:ssZ

like image 172
kmcc049 Avatar answered Oct 19 '22 23:10

kmcc049


ToString("O") will solve the problem too.

like image 39
Yiding Avatar answered Oct 19 '22 22:10

Yiding