Skip to content

Serialization of DateTime when TimeSeparator is a Dot #1227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alkampfergit opened this issue Feb 2, 2015 · 2 comments
Closed

Serialization of DateTime when TimeSeparator is a Dot #1227

alkampfergit opened this issue Feb 2, 2015 · 2 comments

Comments

@alkampfergit
Copy link

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)));   
@gmarz
Copy link
Contributor

gmarz commented Feb 19, 2015

Hi @alkampfergit, apologies for the delay.

Agreed, we should probably be ignoring culture here and it seems we're a bit inconsistent where we do and don't. For instance, in the RangeFilter we ignore culture but in the RangeQuery, we don't.

Can you share the exact query you're running? I'm assuming it's probably the RangeQuery? Just want to make we catch all the places where we ToString on dates and make sure we're applying the proper formatting.

@gmarz gmarz added Bug and removed v1.4.2 labels Feb 19, 2015
@gmarz gmarz added v1.4.4 and removed v1.4.3 labels Mar 27, 2015
@diogopineda
Copy link

I have a similar problem, Im trying to execute a QueryRaw with a Datetime, the query generated is
1736539391/registro/_search
{
"query": {"match_phrase": {
"campo_3346476a-0686-4442-8ad3-6888b68bacc7": "04/28/2014 14:57:42"
}}
}
And the result is ElasticsearchParseException[failed to parse date field [04/28/2014 14:57:42], tried both date format [dateOptionalTime], and timestamp number]; nested: IllegalArgumentException[Invalid format: "04/28/2014 14:57:42" is malformed at "/28/2014 14:57:42"];

Im my case I parse the datetime to a string with format InvariantCulture
term = Convert.ToDateTime(term).ToString("yyyy-MM-ddTHH:mm:ss");
And the query generated is :
1736539391/registro/_search
{
"query": {"match_phrase": {
"campo_3346476a-0686-4442-8ad3-6888b68bacc7": "2014-04-28T14:57:42"
}}
}
And works fine for the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants