Skip to content

Commit fa4b51e

Browse files
committed
Merge pull request #1496 from elastic/fix/force-invariant-culture-on-all-dates
Fix/force invariant culture on all dates
2 parents 43b58bc + 77cffab commit fa4b51e

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

Diff for: src/Nest/DSL/Query/RangeQueryDescriptor.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -188,25 +188,25 @@ public RangeQueryDescriptor<T> LowerOrEquals(string to)
188188

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

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

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

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

Diff for: src/Nest/Resolvers/ElasticContractResolver.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Reflection;
99
using System.Collections;
10+
using System.Globalization;
1011
using Nest.Resolvers.Converters;
1112

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

5455
else if (objectType == typeof(DateTime) || objectType == typeof(DateTime?))
55-
contract.Converter = new IsoDateTimeConverter();
56+
contract.Converter = new IsoDateTimeConverter() { Culture = CultureInfo.InvariantCulture };
5657

5758
else if (typeof(IHit<object>).IsAssignableFrom(objectType))
5859
contract.Converter = new DefaultHitConverter();

Diff for: src/Tests/Nest.Tests.Unit/Internals/Serialize/SerializeTests.cs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
4+
using System.Globalization;
35
using System.Reflection;
6+
using System.Threading;
47
using NUnit.Framework;
58
using Nest.Tests.MockData.Domain;
69
using Elasticsearch.Net;
10+
using FluentAssertions;
711

812
namespace Nest.Tests.Unit.Internals.Serialize
913
{
@@ -48,6 +52,24 @@ public void ClassWithCollectionSerializes()
4852
this.JsonEquals(json, MethodInfo.GetCurrentMethod());
4953
}
5054

55+
[Test]
56+
public void DateTimeIgnoresCurrentCulture()
57+
{
58+
var t = new Thread(() =>
59+
{
60+
var cultureInfo = new CultureInfo("IT-it");
61+
cultureInfo.DateTimeFormat.DateSeparator = ".";
62+
cultureInfo.DateTimeFormat.TimeSeparator = ".";
63+
Thread.CurrentThread.CurrentCulture = cultureInfo;
64+
Thread.CurrentThread.CurrentUICulture = cultureInfo;
65+
66+
var serialized = _client.Serializer.Serialize(new DateTime(2015, 01, 30, 08, 52, 32, 443)).Utf8String();
67+
serialized.Should().EndWith(":32.443\"");
68+
});
69+
t.Start();
70+
t.Join(TimeSpan.FromSeconds(5));
71+
}
72+
5173
[Test]
5274
public void SerializingSearchIsFastEnough()
5375
{

Diff for: src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@
416416
<Compile Include="Reproduce\Reproduce1396Tests.cs" />
417417
<Compile Include="Reproduce\Reproduce1199Tests.cs" />
418418
<Compile Include="Reproduce\Reproduce1146Tests.cs" />
419+
<Compile Include="Reproduce\Reproduce1440Tests.cs" />
419420
<Compile Include="Reproduce\Reproduce1464Tests.cs" />
420421
<Compile Include="Reproduce\Reproduce629Tests.cs" />
421422
<Compile Include="Reproduce\Reproduce1187Tests.cs" />

0 commit comments

Comments
 (0)