Skip to content

Serialization of DateTime when TimeSeparator is a Dot #1227

@alkampfergit

Description

@alkampfergit

I've a unit test where I'm able to repro the problem, I explicitly set Italian culture with a dot as separator for DateTimeFormat.TimeSeparator

 var cultureInfo = new CultureInfo("IT-it");
cultureInfo.DateTimeFormat.DateSeparator = ".";
cultureInfo.DateTimeFormat.TimeSeparator = ".";
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;

//I issue the query here

The query fails with an exception

ElasticsearchParseException[failed to parse date field [2015-01-30T08.52.32.443], tried both date format [dateOptionalTime], and timestamp number]; nested: IllegalArgumentException[Invalid format: "2015-01-30T08.52.32.443" is malformed at ".32.443"]

I Elastic Search needs semicolon to separate hours, minutes and milliseconds, and rejects the query. When a DateTime is converted to string, localization should not be considered.

Actually I've patched my code using string instead of DateTime in Nest query.

public const String DateTimeFormatForNestQuery = "yyyy-MM-ddThh:mm:ss.fff";

...

String startValueFormatted = startValue.ToString(DateTimeFormatForNestQuery, CultureInfo.InvariantCulture);
String endValueFormatted = endValue.ToString(DateTimeFormatForNestQuery, CultureInfo.InvariantCulture);

.OnField(d => d.PublishDate)
       .GreaterOrEquals(startValueFormatted)
       .LowerOrEquals(endValueFormatted)));   

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions