Skip to content

Fix/force invariant culture on all dates #1496

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

Merged
merged 2 commits into from
Jul 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Nest/DSL/Query/RangeQueryDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,25 +188,25 @@ public RangeQueryDescriptor<T> LowerOrEquals(string to)

public RangeQueryDescriptor<T> Greater(DateTime? from, string format = "yyyy-MM-dd'T'HH:mm:ss.fff")
{
this.Self.GreaterThan = from.HasValue ? from.Value.ToString(format) : null;
this.Self.GreaterThan = from.HasValue ? from.Value.ToString(format, CultureInfo.InvariantCulture) : null;
return this;
}

public RangeQueryDescriptor<T> GreaterOrEquals(DateTime? from, string format = "yyyy-MM-dd'T'HH:mm:ss.fff")
{
this.Self.GreaterThanOrEqualTo = from.HasValue ? from.Value.ToString(format) : null;
this.Self.GreaterThanOrEqualTo = from.HasValue ? from.Value.ToString(format, CultureInfo.InvariantCulture) : null;
return this;
}

public RangeQueryDescriptor<T> Lower(DateTime? to, string format = "yyyy-MM-dd'T'HH:mm:ss.fff")
{
this.Self.LowerThan = to.HasValue ? to.Value.ToString(format) : null;
this.Self.LowerThan = to.HasValue ? to.Value.ToString(format, CultureInfo.InvariantCulture) : null;
return this;
}

public RangeQueryDescriptor<T> LowerOrEquals(DateTime? to, string format = "yyyy-MM-dd'T'HH:mm:ss.fff")
{
this.Self.LowerThanOrEqualTo = to.HasValue ? to.Value.ToString(format) : null;
this.Self.LowerThanOrEqualTo = to.HasValue ? to.Value.ToString(format, CultureInfo.InvariantCulture) : null;
return this;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Nest/Resolvers/ElasticContractResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Reflection;
using System.Collections;
using System.Globalization;
using Nest.Resolvers.Converters;

namespace Nest.Resolvers
Expand Down Expand Up @@ -52,7 +53,7 @@ protected override JsonContract CreateContract(Type objectType)
contract.Converter = new AggregationConverter();

else if (objectType == typeof(DateTime) || objectType == typeof(DateTime?))
contract.Converter = new IsoDateTimeConverter();
contract.Converter = new IsoDateTimeConverter() { Culture = CultureInfo.InvariantCulture };

else if (typeof(IHit<object>).IsAssignableFrom(objectType))
contract.Converter = new DefaultHitConverter();
Expand Down
24 changes: 23 additions & 1 deletion src/Tests/Nest.Tests.Unit/Internals/Serialize/SerializeTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Threading;
using NUnit.Framework;
using Nest.Tests.MockData.Domain;
using Elasticsearch.Net;
using FluentAssertions;

namespace Nest.Tests.Unit.Internals.Serialize
{
Expand Down Expand Up @@ -48,6 +52,24 @@ public void ClassWithCollectionSerializes()
this.JsonEquals(json, MethodInfo.GetCurrentMethod());
}

[Test]
public void DateTimeIgnoresCurrentCulture()
{
var t = new Thread(() =>
{
var cultureInfo = new CultureInfo("IT-it");
cultureInfo.DateTimeFormat.DateSeparator = ".";
cultureInfo.DateTimeFormat.TimeSeparator = ".";
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;

var serialized = _client.Serializer.Serialize(new DateTime(2015, 01, 30, 08, 52, 32, 443)).Utf8String();
serialized.Should().EndWith(":32.443\"");
});
t.Start();
t.Join(TimeSpan.FromSeconds(5));
}

[Test]
public void SerializingSearchIsFastEnough()
{
Expand Down
1 change: 1 addition & 0 deletions src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@
<Compile Include="Reproduce\Reproduce1396Tests.cs" />
<Compile Include="Reproduce\Reproduce1199Tests.cs" />
<Compile Include="Reproduce\Reproduce1146Tests.cs" />
<Compile Include="Reproduce\Reproduce1440Tests.cs" />
<Compile Include="Reproduce\Reproduce1464Tests.cs" />
<Compile Include="Reproduce\Reproduce629Tests.cs" />
<Compile Include="Reproduce\Reproduce1187Tests.cs" />
Expand Down