Skip to content

Commit 1ff0f2d

Browse files
committed
Merge pull request #1301 from aochsner/develop
Initial proposal Fixes #1300
2 parents f76f695 + ac4cc90 commit 1ff0f2d

File tree

2 files changed

+129
-33
lines changed

2 files changed

+129
-33
lines changed

src/Elasticsearch.Net/Extensions/StringExtensions.cs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Specialized;
1+
using System;
2+
using System.Collections.Specialized;
23

34
namespace Elasticsearch.Net
45
{
@@ -25,6 +26,56 @@ internal static NameValueCollection ToNameValueCollection(this string queryStrin
2526

2627
return queryParameters;
2728
}
28-
29+
30+
internal static TimeSpan? ToTimeSpan(this string s)
31+
{
32+
if (s.IsNullOrEmpty())
33+
return null;
34+
try
35+
{
36+
long millis;
37+
if (s.EndsWith("S"))
38+
{
39+
millis = long.Parse(s.Substring(0, s.Length - 1));
40+
}
41+
else if (s.EndsWith("ms"))
42+
{
43+
millis = (long) (double.Parse(s.Substring(0, s.Length - 2)));
44+
}
45+
else if (s.EndsWith("s"))
46+
{
47+
millis = (long) (double.Parse(s.Substring(0, s.Length - 1))*1000);
48+
}
49+
else if (s.EndsWith("m"))
50+
{
51+
millis = (long) (double.Parse(s.Substring(0, s.Length - 1))*60*1000);
52+
}
53+
else if (s.EndsWith("H") || s.EndsWith("h"))
54+
{
55+
millis = (long) (double.Parse(s.Substring(0, s.Length - 1))*60*60*1000);
56+
}
57+
else if (s.EndsWith("d"))
58+
{
59+
millis = (long) (double.Parse(s.Substring(0, s.Length - 1))*24*60*60*1000);
60+
}
61+
else if (s.EndsWith("w"))
62+
{
63+
millis = (long) (double.Parse(s.Substring(0, s.Length - 1))*7*24*60*60*1000);
64+
}
65+
else
66+
{
67+
millis = long.Parse(s);
68+
}
69+
return TimeSpan.FromMilliseconds(millis);
70+
}
71+
catch (FormatException)
72+
{
73+
return null;
74+
}
75+
catch (OverflowException)
76+
{
77+
return null;
78+
}
79+
}
2980
}
3081
}

src/Tests/Nest.Tests.Unit/Extensions/StringExtensionsTests.cs

Lines changed: 76 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,80 @@
33

44
namespace Nest.Tests.Unit.Extensions
55
{
6-
[TestFixture]
7-
public class StringExtensionsTests
8-
{
9-
[Test]
10-
public void CanConvertQueryStringToNameValueCollectionWithQuestionMark()
11-
{
12-
// Arrange
13-
var queryString = "?test1=one&test2=two";
14-
15-
// Act
16-
var queryCollection = queryString.ToNameValueCollection();
17-
18-
// Assert
19-
Assert.IsTrue(queryCollection["test1"] == "one");
20-
Assert.IsTrue(queryCollection["test2"] == "two");
21-
}
22-
23-
[Test]
24-
public void CanConvertQueryStringToNameValueCollectionWithoutQuestionMark()
25-
{
26-
// Arrange
27-
var queryString = "test1=testone&test2=testtwo";
28-
29-
// Act
30-
var queryCollection = queryString.ToNameValueCollection();
31-
32-
// Assert
33-
Assert.IsTrue(queryCollection["test1"] == "testone");
34-
Assert.IsTrue(queryCollection["test2"] == "testtwo");
35-
}
36-
}
6+
[TestFixture]
7+
public class StringExtensionsTests
8+
{
9+
[Test]
10+
public void CanConvertQueryStringToNameValueCollectionWithQuestionMark()
11+
{
12+
// Arrange
13+
var queryString = "?test1=one&test2=two";
14+
15+
// Act
16+
var queryCollection = queryString.ToNameValueCollection();
17+
18+
// Assert
19+
Assert.IsTrue(queryCollection["test1"] == "one");
20+
Assert.IsTrue(queryCollection["test2"] == "two");
21+
}
22+
23+
[Test]
24+
public void CanConvertQueryStringToNameValueCollectionWithoutQuestionMark()
25+
{
26+
// Arrange
27+
var queryString = "test1=testone&test2=testtwo";
28+
29+
// Act
30+
var queryCollection = queryString.ToNameValueCollection();
31+
32+
// Assert
33+
Assert.IsTrue(queryCollection["test1"] == "testone");
34+
Assert.IsTrue(queryCollection["test2"] == "testtwo");
35+
}
36+
37+
private const int s = 1000;
38+
private const int m = 60 * s;
39+
private const int h = 60 * m;
40+
private const int d = 24 * h;
41+
private const int w = 7 * d;
42+
43+
/// <summary />
44+
[TestCase("0s", 0)]
45+
[TestCase("1s", 1 * s)]
46+
[TestCase("-1s", -1 * s)]
47+
[TestCase("1.5s", 1.5 * s)]
48+
[TestCase("90s", 90 * s)]
49+
[TestCase("1m", 1 * m)]
50+
[TestCase("1h", 1 * h)]
51+
[TestCase("1H", 1 * h)]
52+
[TestCase("1d", 1 * d)]
53+
[TestCase("1w", 1 * w)]
54+
[TestCase("0", 0)]
55+
[TestCase("1", 1)]
56+
[TestCase("1S", 1)]
57+
[TestCase("1.5ms", 1)]
58+
public void ParseElasticSearchTimeUnit_Success(string value, double timeInMillis)
59+
{
60+
// act
61+
Assert.That(value.ToTimeSpan().Value.TotalMilliseconds, Is.EqualTo(timeInMillis));
62+
}
63+
64+
[TestCase(null)]
65+
[TestCase("")]
66+
[TestCase(" ")]
67+
[TestCase("1.5")]
68+
[TestCase("2s2")]
69+
[TestCase("2ss")]
70+
[TestCase("4m2s")]
71+
[TestCase("2M")]
72+
[TestCase("2y")]
73+
[TestCase("s")]
74+
[TestCase("1.5S")]
75+
public void ParseElasticSearchTimeUnit_Fail_ReturnsNull(string value)
76+
{
77+
// act
78+
Assert.That(value.ToTimeSpan(), Is.Null);
79+
}
80+
81+
}
3782
}

0 commit comments

Comments
 (0)