Projections DateTimePropertyEditor using System Locale for conversion

Topics: Localization
Jun 16, 2012 at 12:18 PM
Edited Jun 16, 2012 at 12:22 PM

I defined a projection query with a layout that includes a DateTime field from my content type as one of its properties. 

I then created a Projection Widget using this query and layout, but could not get the results to display. When debugging this I noticed a FormatException (String was not recognized as a valid DateTime) being thrown in the DateTimePropertyEditor.Format method by the call to Convert.ToDateTime(value):

        public dynamic Format(dynamic display, object value, dynamic formState) {
            return DateTimePropertyForm.FormatDateTime(display, Convert.ToDateTime(value), formState, _culture.Value);
        }

When the DateTime field is stored (Orchard.Framework), the SimpleFieldStorage.Set method uses the invariant culture when converting the DateTime value to string:

        public void Set<T>(string name, T value) {
            Setter(name, typeof(T), Convert.ToString(value, CultureInfo.InvariantCulture));
        }

The problem I believe is a localization issue in the projection module in the DateTimePropertyEditor.Format method. My system locale is set to en-AU so dates by default are formatted as "dd/mm/yyyy". DateTime fields are stored by Orchard using InvariantCulture (mm/dd/yyyy), but when the projection attempts to parse that value back out it is using the system locale, since the call to Convert.ToDateTime() is not passing in a format provider.

I fixed this by changing the code in that method to pass in the invariant culture:

        public dynamic Format(dynamic display, object value, dynamic formState) {
            return DateTimePropertyForm.FormatDateTime(display, Convert.ToDateTime(value, CultureInfo.InvariantCulture), formState, _culture.Value);
        }

Is this a reasonable solution? Should it be raised as a bug?

Coordinator
Jun 18, 2012 at 4:29 PM

This seems to be a wise bug fix. Could you please file a bug for this one, and include you change as a patch file ?

Thanks

Jun 19, 2012 at 5:55 AM

I created the following issue to track this:

http://orchard.codeplex.com/workitem/18756

I've also submitted a fix to a fork of the Orchard Projections module and generated a pull request (this is referenced in a comment in the above issue).

I'm new to this whole process so please let me know if I've gone about anything the wrong way. I created the issue here and not under the Orchard Projections project (orchardprojections.codeplex.com) as there are currently no issues in that project, so I presume issues are not being tracked there?

Also, the changes I applied in that project where to the "default" branch, as there doesn't appear to be a "contributions" branch in the orchard projections repository.