Skip to content

Commit ac4cc90

Browse files
author
Andrew Ochsner
committed
Fixing some feedback on #1300
1 parent e9bff48 commit ac4cc90

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

src/Elasticsearch.Net/Extensions/StringExtensions.cs

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,55 @@ internal static NameValueCollection ToNameValueCollection(this string queryStrin
2626

2727
return queryParameters;
2828
}
29+
2930
internal static TimeSpan? ToTimeSpan(this string s)
3031
{
3132
if (s.IsNullOrEmpty())
3233
return null;
33-
long wholeNumber;
34-
if (long.TryParse(s, out wholeNumber))
35-
{
36-
return TimeSpan.FromMilliseconds(wholeNumber);
37-
}
38-
double decimalNumber;
39-
var unit = s.Substring(s.Length - 1);
40-
if (s.Length <= 1 || !double.TryParse(s.Substring(0, s.Length - 1), out decimalNumber))
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)
4172
{
4273
return null;
4374
}
44-
switch (unit)
75+
catch (OverflowException)
4576
{
46-
case "s":
47-
return TimeSpan.FromSeconds(decimalNumber);
48-
case "m":
49-
return TimeSpan.FromMinutes(decimalNumber);
50-
case "h":
51-
return TimeSpan.FromHours(decimalNumber);
52-
case "d":
53-
return TimeSpan.FromDays(decimalNumber);
54-
case "w":
55-
return TimeSpan.FromDays(decimalNumber * 7);
56-
default:
57-
return null;
77+
return null;
5878
}
5979
}
6080
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,13 @@ public void CanConvertQueryStringToNameValueCollectionWithoutQuestionMark()
4848
[TestCase("90s", 90 * s)]
4949
[TestCase("1m", 1 * m)]
5050
[TestCase("1h", 1 * h)]
51+
[TestCase("1H", 1 * h)]
5152
[TestCase("1d", 1 * d)]
5253
[TestCase("1w", 1 * w)]
5354
[TestCase("0", 0)]
5455
[TestCase("1", 1)]
56+
[TestCase("1S", 1)]
57+
[TestCase("1.5ms", 1)]
5558
public void ParseElasticSearchTimeUnit_Success(string value, double timeInMillis)
5659
{
5760
// act
@@ -68,6 +71,7 @@ public void ParseElasticSearchTimeUnit_Success(string value, double timeInMillis
6871
[TestCase("2M")]
6972
[TestCase("2y")]
7073
[TestCase("s")]
74+
[TestCase("1.5S")]
7175
public void ParseElasticSearchTimeUnit_Fail_ReturnsNull(string value)
7276
{
7377
// act

0 commit comments

Comments
 (0)