Skip to content

Commit 7d50e9c

Browse files
committed
Fixes and improvements to Time
- Always reduce to lowest form (e.g. 60000ms => 1m) - Implicit conversions from millisconds now serialize the same as conversions from TimeSpan and string - Ensure Time.ToString() == TimeSpan.ToTimeUnit()
1 parent c72f3f3 commit 7d50e9c

File tree

10 files changed

+1036
-989
lines changed

10 files changed

+1036
-989
lines changed

paket.lock

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
NUGET
22
remote: https://www.nuget.org/api/v2
33
specs:
4-
Bogus (3.0.0.3)
4+
Bogus (3.0.4)
55
Newtonsoft.Json (>= 7.0 < 8.0) - framework: >= net40
66
CsQuery (1.3.4)
77
DiffPlex (1.2.1)
8-
FluentAssertions (4.0.0)
8+
FluentAssertions (4.2.1)
99
System.Collections (>= 4.0.10) - framework: dnxcore50
1010
System.Diagnostics.Debug (>= 4.0.10) - framework: dnxcore50
1111
System.Globalization (>= 4.0.10) - framework: dnxcore50
@@ -25,13 +25,13 @@ NUGET
2525
Humanizer (1.37.7)
2626
JetBrains.Profiler.Kernel.Windows.Api (104.0.20151218.125646)
2727
Wave (4.0.0)
28-
Microsoft.CodeAnalysis.Analyzers (1.0.0)
29-
Microsoft.CodeAnalysis.Common (1.0.0)
30-
Microsoft.CodeAnalysis.Analyzers (1.0.0)
31-
System.Collections.Immutable (>= 1.1.36)
32-
System.Reflection.Metadata (>= 1.0.21)
33-
Microsoft.CodeAnalysis.CSharp (1.0.0)
34-
Microsoft.CodeAnalysis.Common (1.0.0)
28+
Microsoft.CodeAnalysis.Analyzers (1.1.0)
29+
Microsoft.CodeAnalysis.Common (1.1.1)
30+
Microsoft.CodeAnalysis.Analyzers (>= 1.1.0)
31+
System.Collections.Immutable (>= 1.1.37)
32+
System.Reflection.Metadata (>= 1.1.0)
33+
Microsoft.CodeAnalysis.CSharp (1.1.1)
34+
Microsoft.CodeAnalysis.Common (1.1.1)
3535
NDesk.Options (0.2.1)
3636
Newtonsoft.Json (7.0.1)
3737
RazorMachine (2.6.1)
@@ -156,7 +156,7 @@ NUGET
156156
System.Runtime (>= 4.0.0) - framework: dnxcore50
157157
System.Runtime (>= 4.0.20) - framework: dnxcore50
158158
System.Runtime.Extensions (>= 4.0.10) - framework: dnxcore50
159-
System.Reflection.Metadata (1.0.22)
159+
System.Reflection.Metadata (1.1.0)
160160
System.Collections (>= 4.0.0) - framework: dnxcore50
161161
System.Collections.Immutable (>= 1.1.37) - framework: dnxcore50, portable-net45+win80
162162
System.Diagnostics.Debug (>= 4.0.0) - framework: dnxcore50
@@ -272,7 +272,7 @@ NUGET
272272
xunit (2.1.0)
273273
xunit.assert (2.1.0)
274274
xunit.core (2.1.0)
275-
xunit.abstractions (2.0.0) - framework: >= net45, dnx451, dnxcore50, monoandroid, monotouch, portable-net45+win80+wp80+wpa81, xamarinios, winv4.5, wpv8.0
275+
xunit.abstractions (2.0.0) - framework: >= net45, dnx451, dnxcore50, monoandroid, monotouch, portable-net45+win80+wp80+wpa81, xamarinios, winv4.5, wpv8.1, wpv8.0
276276
xunit.assert (2.1.0)
277277
System.Collections (>= 4.0.0) - framework: dnxcore50
278278
System.Diagnostics.Debug (>= 4.0.0) - framework: dnxcore50
@@ -296,11 +296,11 @@ NUGET
296296
System.Runtime.Extensions (>= 4.0.0) - framework: dnxcore50
297297
System.Threading.Tasks (>= 4.0.0) - framework: dnxcore50
298298
xunit.abstractions (>= 2.0.0) - framework: dnxcore50
299-
xunit.extensibility.core (2.1.0) - framework: >= net45, dnx451, dnxcore50, monoandroid, monotouch, portable-net45+win80+wp80+wpa81, xamarinios, winv4.5, wpv8.0
300-
xunit.extensibility.execution (2.1.0) - framework: >= net45, dnx451, dnxcore50, monoandroid, monotouch, portable-net45+win80+wp80+wpa81, xamarinios, winv4.5, wpv8.0
301-
xunit.extensibility.core (2.1.0) - framework: >= net45, dnx451, dnxcore50, monoandroid, monotouch, portable-net45+win80+wp80+wpa81, xamarinios, winv4.5, wpv8.0
299+
xunit.extensibility.core (2.1.0) - framework: >= net45, dnx451, dnxcore50, monoandroid, monotouch, portable-net45+win80+wp80+wpa81, xamarinios, winv4.5, wpv8.1, wpv8.0
300+
xunit.extensibility.execution (2.1.0) - framework: >= net45, dnx451, dnxcore50, monoandroid, monotouch, portable-net45+win80+wp80+wpa81, xamarinios, winv4.5, wpv8.1, wpv8.0
301+
xunit.extensibility.core (2.1.0) - framework: >= net45, dnx451, dnxcore50, monoandroid, monotouch, portable-net45+win80+wp80+wpa81, xamarinios, winv4.5, wpv8.1, wpv8.0
302302
xunit.abstractions (2.0.0)
303-
xunit.extensibility.execution (2.1.0) - framework: >= net45, dnx451, dnxcore50, monoandroid, monotouch, portable-net45+win80+wp80+wpa81, xamarinios, winv4.5, wpv8.0
303+
xunit.extensibility.execution (2.1.0) - framework: >= net45, dnx451, dnxcore50, monoandroid, monotouch, portable-net45+win80+wp80+wpa81, xamarinios, winv4.5, wpv8.1, wpv8.0
304304
System.Collections (>= 4.0.0) - framework: dnxcore50
305305
System.Diagnostics.Debug (>= 4.0.0) - framework: dnxcore50
306306
System.Globalization (>= 4.0.0) - framework: dnxcore50
@@ -315,21 +315,21 @@ NUGET
315315
System.Threading (>= 4.0.0) - framework: dnxcore50
316316
System.Threading.Tasks (>= 4.0.0) - framework: dnxcore50
317317
xunit.abstractions (>= 2.0.0) - framework: dnxcore50
318-
xunit.extensibility.core (2.1.0) - framework: >= net45, dnx451, dnxcore50, monoandroid, monotouch, xamarinios, winv4.5, wpv8.0
318+
xunit.extensibility.core (2.1.0) - framework: >= net45, dnx451, dnxcore50, monoandroid, monotouch, xamarinios, winv4.5, wpv8.1, wpv8.0
319319

320320
GROUP build
321321
NUGET
322322
remote: https://www.nuget.org/api/v2
323323
specs:
324-
FAKE (4.5.6)
324+
FAKE (4.14.5)
325325
FSharp.Data (2.2.5)
326326
Zlib.Portable (>= 1.10.0) - framework: portable-net40+sl50+wp80+win80
327327
gitlink (2.2.0)
328-
Node.js (4.0.0)
329-
NoGit (0.0.9)
330-
Node.js (>= 0.10.26.1)
331-
Npm (1.4.15.2)
332-
Node.js (>= 0.10.26.1)
333-
NoGit (>= 0.0.8)
328+
Node.js (5.3.0)
329+
NoGit (0.1.0)
330+
Node.js (>= 0.12.0)
331+
Npm (3.5.2)
332+
Node.js (>= 0.12.7)
333+
NoGit (>= 0.1.0)
334334
xunit.runner.console (2.1.0)
335335
Zlib.Portable (1.11.0) - framework: portable-net40+sl50+wp80+win80

src/CodeGeneration/CodeGeneration.LowLevelClient/Views/_Requests.Generated.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ namespace Nest
7979
var setter = "value";
8080
if (fieldType == "Time")
8181
{
82-
setter += ".ToTimeSpan()";
82+
setter += ".ToString()";
8383
}
8484
<text>///<summary>@kv.Value.Description</summary>
8585
@if(!string.IsNullOrWhiteSpace(kv.Value.DeprecatedInFavorOf))
8686
{
8787
<text>[Obsolete("Scheduled to be removed in 2.0, use @kv.Value.DeprecatedInFavorOf instead")]</text>
8888
}
89-
public @Raw(fieldType) @(mm) { get { return Q@(Raw("<" + fieldType + ">"))("@original"); } set { Q@(Raw("<" + fieldType + ">"))("@original", @setter); } }
89+
public @Raw(fieldType) @(mm) { get { return Q@(Raw("<" + fieldType + ">"))("@original"); } set { Q("@original", @setter); } }
9090
</text>
9191
}
9292
@if (method.RequestTypeUnmapped)

src/Nest/CommonAbstractions/Request/RequestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected RequestBase(Func<RouteValues, RouteValues> pathSelector)
5050

5151
protected TOut Q<TOut>(string name) => RequestState.RequestParameters.GetQueryStringValue<TOut>(name);
5252

53-
protected void Q<TIn>(string name, TIn value) => RequestState.RequestParameters.AddQueryStringValue(name, value);
53+
protected void Q(string name, object value) => RequestState.RequestParameters.AddQueryStringValue(name, value);
5454
}
5555

5656
public abstract class RequestDescriptorBase<TDescriptor, TParameters, TInterface> : RequestBase<TParameters>, IDescriptor

src/Nest/CommonOptions/TimeUnit/Time.cs

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ public class Time : IComparable<Time>, IEquatable<Time>
1010
{
1111
private static readonly Regex _expressionRegex = new Regex(@"^(?<factor>\d+(?:\.\d+)?)(?<interval>(?:y|M|w|d|h|m|s|ms))?$", RegexOptions.Compiled | RegexOptions.ExplicitCapture);
1212

13-
private static readonly long _year = (long)TimeSpan.FromDays(365).TotalMilliseconds;
14-
private static readonly long _week = (long)TimeSpan.FromDays(7).TotalMilliseconds;
15-
private static readonly long _day = (long)TimeSpan.FromDays(1).TotalMilliseconds;
16-
private static readonly long _hour = (long)TimeSpan.FromHours(1).TotalMilliseconds;
17-
private static readonly long _minute = (long)TimeSpan.FromMinutes(1).TotalMilliseconds;
18-
private static readonly long _second = (long)TimeSpan.FromSeconds(1).TotalMilliseconds;
13+
private static readonly double _year = TimeSpan.FromDays(365).TotalMilliseconds;
14+
private static readonly double _week = TimeSpan.FromDays(7).TotalMilliseconds;
15+
private static readonly double _day = TimeSpan.FromDays(1).TotalMilliseconds;
16+
private static readonly double _hour = TimeSpan.FromHours(1).TotalMilliseconds;
17+
private static readonly double _minute = TimeSpan.FromMinutes(1).TotalMilliseconds;
18+
private static readonly double _second = TimeSpan.FromSeconds(1).TotalMilliseconds;
1919

20-
public double? Factor { get; }
21-
public TimeUnit? Interval { get; }
22-
public long Milliseconds { get; }
20+
public double? Factor { get; private set; }
21+
public TimeUnit? Interval { get; private set; }
22+
public double Milliseconds { get; private set; }
2323

2424
public static implicit operator Time(TimeSpan span) => new Time(span);
25-
public static implicit operator Time(long milliseconds) => new Time(milliseconds);
25+
public static implicit operator Time(double milliseconds) => new Time(milliseconds);
2626
public static implicit operator Time(string expression) => new Time(expression);
2727

2828
public Time(double factor, TimeUnit interval)
@@ -48,49 +48,12 @@ public Time(double factor, TimeUnit interval)
4848

4949
public Time(TimeSpan timeSpan)
5050
{
51-
var ms = timeSpan.TotalMilliseconds;
52-
this.Milliseconds = (long)ms;
53-
54-
if (ms >= _year)
55-
{
56-
Factor = ms / _year;
57-
Interval = TimeUnit.Year;
58-
}
59-
else if (ms >= _week)
60-
{
61-
Factor = ms / _week;
62-
Interval = TimeUnit.Week;
63-
}
64-
else if (ms >= _day)
65-
{
66-
Factor = ms / _day;
67-
Interval = TimeUnit.Day;
68-
}
69-
else if (ms >= _hour)
70-
{
71-
Factor = ms / _hour;
72-
Interval = TimeUnit.Hour;
73-
}
74-
else if (ms >= _minute)
75-
{
76-
Factor = ms / _minute;
77-
Interval = TimeUnit.Minute;
78-
}
79-
else if (ms >= _second)
80-
{
81-
Factor = ms / _second;
82-
Interval = TimeUnit.Second;
83-
}
84-
else
85-
{
86-
Factor = ms;
87-
Interval = TimeUnit.Millisecond;
88-
}
51+
Reduce(timeSpan.TotalMilliseconds);
8952
}
9053

91-
public Time(long milliseconds)
54+
public Time(double milliseconds)
9255
{
93-
this.Milliseconds = milliseconds;
56+
Reduce(milliseconds);
9457
}
9558

9659
public Time(string timeUnit)
@@ -134,7 +97,7 @@ public int CompareTo(Time other)
13497
public static bool operator >(Time left, Time right) => left.CompareTo(right) > 0;
13598
public static bool operator >=(Time left, Time right) => left.CompareTo(right) > 0 || left.Equals(right);
13699

137-
public static bool operator ==(Time left, Time right) =>
100+
public static bool operator ==(Time left, Time right) =>
138101
object.ReferenceEquals(left, null) ? object.ReferenceEquals(right, null) : left.Equals(right);
139102

140103
public static bool operator !=(Time left, Time right) =>
@@ -144,9 +107,8 @@ public int CompareTo(Time other)
144107

145108
public override string ToString()
146109
{
147-
if (this.Factor == null) return this.Milliseconds.ToString(CultureInfo.InvariantCulture);
148-
return this.Factor.Value.ToString("0.##", CultureInfo.InvariantCulture) +
149-
this.Interval.GetValueOrDefault().GetStringValue();
110+
var factor = this.Factor.Value.ToString("0.##", CultureInfo.InvariantCulture);
111+
return (this.Interval.HasValue) ? factor + this.Interval.Value.GetStringValue() : factor;
150112
}
151113

152114
public bool Equals(Time other)
@@ -166,5 +128,48 @@ public override bool Equals(object obj)
166128

167129
public override int GetHashCode() => this.Milliseconds.GetHashCode();
168130

131+
private void Reduce(double ms)
132+
{
133+
this.Milliseconds = ms;
134+
135+
if (ms >= _year)
136+
{
137+
Factor = ms / _year;
138+
Interval = TimeUnit.Year;
139+
}
140+
else if (ms >= _week)
141+
{
142+
Factor = ms / _week;
143+
Interval = TimeUnit.Week;
144+
}
145+
else if (ms >= _day)
146+
{
147+
Factor = ms / _day;
148+
Interval = TimeUnit.Day;
149+
}
150+
else if (ms >= _hour)
151+
{
152+
Factor = ms / _hour;
153+
Interval = TimeUnit.Hour;
154+
}
155+
else if (ms >= _minute)
156+
{
157+
Factor = ms / _minute;
158+
Interval = TimeUnit.Minute;
159+
}
160+
else if (ms >= _second)
161+
{
162+
Factor = ms / _second;
163+
Interval = TimeUnit.Second;
164+
}
165+
else
166+
{
167+
Factor = ms;
168+
// If milliseconds is < 0 then don't set an interval.
169+
// This is used when setting things like index.refresh_interval = -1 (the only case where a unit isn't required)
170+
if (ms > 0)
171+
Interval = TimeUnit.Millisecond;
172+
}
173+
}
169174
}
170175
}

src/Nest/CommonOptions/TimeUnit/TimeJsonConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ internal class TimeJsonConverter : JsonConverter
88
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
99
{
1010
var v = value as Time;
11-
if (v.Factor.HasValue)
11+
if (v.Factor.HasValue && v.Interval.HasValue)
1212
writer.WriteValue(v.ToString());
13-
else writer.WriteValue(v.Milliseconds);
13+
else writer.WriteValue((long)v.Milliseconds);
1414
}
1515

1616
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)

0 commit comments

Comments
 (0)