12
12
namespace Elastic . Clients . Elasticsearch ;
13
13
14
14
/// <summary>
15
- /// Represents a time value
15
+ /// Represents a duration value.
16
16
/// </summary>
17
- [ JsonConverter ( typeof ( TimeConverter ) ) ]
18
- public sealed class Time : IComparable < Time > , IEquatable < Time > , IUrlParameter
17
+ [ JsonConverter ( typeof ( DurationConverter ) ) ]
18
+ public sealed class Duration : IComparable < Duration > , IEquatable < Duration > , IUrlParameter
19
19
{
20
20
private const double MicrosecondsInATick = 0.1 ; // 10 ticks = 1 microsecond
21
21
private const double MillisecondsInADay = MillisecondsInAnHour * 24 ;
@@ -40,18 +40,18 @@ public sealed class Time : IComparable<Time>, IEquatable<Time>, IUrlParameter
40
40
41
41
private static readonly double FLOAT_TOLERANCE = 1e-7 ; // less than 1 nanosecond
42
42
43
- private Time ( int specialFactor , bool specialValue )
43
+ private Duration ( int specialFactor , bool specialValue )
44
44
{
45
45
if ( ! specialValue )
46
46
throw new ArgumentException ( "this constructor is only for static TimeValues" ) ;
47
47
48
48
StaticTimeValue = specialFactor ;
49
49
}
50
50
51
- public Time ( TimeSpan timeSpan )
51
+ public Duration ( TimeSpan timeSpan )
52
52
: this ( timeSpan . TotalMilliseconds ) { }
53
53
54
- public Time ( double milliseconds )
54
+ public Duration ( double milliseconds )
55
55
{
56
56
if ( Math . Abs ( milliseconds - - 1 ) < FLOAT_TOLERANCE )
57
57
StaticTimeValue = - 1 ;
@@ -61,14 +61,14 @@ public Time(double milliseconds)
61
61
Reduce ( milliseconds ) ;
62
62
}
63
63
64
- public Time ( double factor , TimeUnit interval )
64
+ public Duration ( double factor , TimeUnit interval )
65
65
{
66
66
Factor = factor ;
67
67
Interval = interval ;
68
68
Milliseconds = GetExactMilliseconds ( Factor . Value , Interval . Value ) ;
69
69
}
70
70
71
- public Time ( string timeUnit )
71
+ public Duration ( string timeUnit )
72
72
{
73
73
if ( timeUnit . IsNullOrEmpty ( ) )
74
74
throw new ArgumentException ( "Expression string is empty" , nameof ( timeUnit ) ) ;
@@ -85,17 +85,17 @@ public Time(string timeUnit)
85
85
86
86
public double ? Milliseconds { get ; private set ; }
87
87
88
- public static Time MinusOne { get ; } = new Time ( - 1 , true ) ;
88
+ public static Duration MinusOne { get ; } = new Duration ( - 1 , true ) ;
89
89
90
- public static Time Zero { get ; } = new Time ( 0 , true ) ;
90
+ public static Duration Zero { get ; } = new Duration ( 0 , true ) ;
91
91
92
92
private int ? StaticTimeValue { get ; }
93
93
94
- public static implicit operator Time ( TimeSpan span ) => new ( span ) ;
94
+ public static implicit operator Duration ( TimeSpan span ) => new ( span ) ;
95
95
96
- public static implicit operator Time ( double milliseconds ) => new ( milliseconds ) ;
96
+ public static implicit operator Duration ( double milliseconds ) => new ( milliseconds ) ;
97
97
98
- public static implicit operator Time ( string expression ) => new ( expression ) ;
98
+ public static implicit operator Duration ( string expression ) => new ( expression ) ;
99
99
100
100
private void ParseExpression ( string timeUnit )
101
101
{
@@ -147,7 +147,7 @@ private void ParseExpression(string timeUnit)
147
147
Milliseconds = GetExactMilliseconds ( Factor . Value , Interval . Value ) ;
148
148
}
149
149
150
- public int CompareTo ( Time other )
150
+ public int CompareTo ( Duration other )
151
151
{
152
152
if ( other == null )
153
153
return 1 ;
@@ -182,21 +182,21 @@ public int CompareTo(Time other)
182
182
183
183
private static bool IsIntegerGreaterThanZero ( double d ) => Math . Abs ( d % 1 ) < double . Epsilon ;
184
184
185
- public static bool operator < ( Time left , Time right ) => left . CompareTo ( right ) < 0 ;
185
+ public static bool operator < ( Duration left , Duration right ) => left . CompareTo ( right ) < 0 ;
186
186
187
- public static bool operator <= ( Time left , Time right ) => left . CompareTo ( right ) < 0 || left . Equals ( right ) ;
187
+ public static bool operator <= ( Duration left , Duration right ) => left . CompareTo ( right ) < 0 || left . Equals ( right ) ;
188
188
189
- public static bool operator > ( Time left , Time right ) => left . CompareTo ( right ) > 0 ;
189
+ public static bool operator > ( Duration left , Duration right ) => left . CompareTo ( right ) > 0 ;
190
190
191
- public static bool operator >= ( Time left , Time right ) => left . CompareTo ( right ) > 0 || left . Equals ( right ) ;
191
+ public static bool operator >= ( Duration left , Duration right ) => left . CompareTo ( right ) > 0 || left . Equals ( right ) ;
192
192
193
- public static bool operator == ( Time left , Time right ) =>
193
+ public static bool operator == ( Duration left , Duration right ) =>
194
194
ReferenceEquals ( left , null ) ? right is null : left . Equals ( right ) ;
195
195
196
- public static bool operator != ( Time left , Time right ) => ! ( left == right ) ;
196
+ public static bool operator != ( Duration left , Duration right ) => ! ( left == right ) ;
197
197
198
198
/// <summary>
199
- /// Converts this instance of <see cref="Time " /> to an instance of <see cref="TimeSpan" />.
199
+ /// Converts this instance of <see cref="Duration " /> to an instance of <see cref="TimeSpan" />.
200
200
/// For values in <see cref="TimeUnit.Microseconds" /> and <see cref="TimeUnit.Nanoseconds" />, value will be rounded to
201
201
/// the nearest Tick.
202
202
/// All other values will be rounded to the nearest Millisecond.
@@ -206,7 +206,7 @@ public int CompareTo(Time other)
206
206
/// special time values <see cref="MinusOne" /> and <see cref="Zero" /> do not have a <see cref="TimeSpan" />
207
207
/// representation.
208
208
/// </para>
209
- /// <para>instance of <see cref="Time " /> has no value for <see cref="Interval" /></para>
209
+ /// <para>instance of <see cref="Duration " /> has no value for <see cref="Interval" /></para>
210
210
/// </exception>
211
211
public TimeSpan ToTimeSpan ( )
212
212
{
@@ -256,7 +256,7 @@ string IUrlParameter.GetString(ITransportConfiguration config)
256
256
return Milliseconds . ToString ( ) ;
257
257
}
258
258
259
- public bool Equals ( Time other )
259
+ public bool Equals ( Duration other )
260
260
{
261
261
if ( ReferenceEquals ( null , other ) )
262
262
return false ;
@@ -286,7 +286,7 @@ public override bool Equals(object obj)
286
286
if ( obj . GetType ( ) != GetType ( ) )
287
287
return false ;
288
288
289
- return Equals ( ( Time ) obj ) ;
289
+ return Equals ( ( Duration ) obj ) ;
290
290
}
291
291
292
292
public override int GetHashCode ( ) => StaticTimeValue . HasValue
@@ -392,33 +392,33 @@ private static string ExponentFormat(double d)
392
392
}
393
393
}
394
394
395
- internal sealed class TimeConverter : JsonConverter < Time >
395
+ internal sealed class DurationConverter : JsonConverter < Duration >
396
396
{
397
- public override Time ? Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
397
+ public override Duration ? Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
398
398
{
399
399
var token = reader . TokenType ;
400
400
401
401
switch ( token )
402
402
{
403
403
case JsonTokenType . String :
404
- return new Time ( reader . GetString ( ) ) ;
404
+ return new Duration ( reader . GetString ( ) ) ;
405
405
case JsonTokenType . Number :
406
406
var milliseconds = reader . GetInt64 ( ) ;
407
407
if ( milliseconds == - 1 )
408
- return Time . MinusOne ;
408
+ return Duration . MinusOne ;
409
409
if ( milliseconds == 0 )
410
- return Time . Zero ;
411
- return new Time ( milliseconds ) ;
410
+ return Duration . Zero ;
411
+ return new Duration ( milliseconds ) ;
412
412
default :
413
413
return null ;
414
414
}
415
415
}
416
416
417
- public override void Write ( Utf8JsonWriter writer , Time value , JsonSerializerOptions options )
417
+ public override void Write ( Utf8JsonWriter writer , Duration value , JsonSerializerOptions options )
418
418
{
419
- if ( value == Time . MinusOne )
419
+ if ( value == Duration . MinusOne )
420
420
writer . WriteNumberValue ( - 1 ) ;
421
- else if ( value == Time . Zero )
421
+ else if ( value == Duration . Zero )
422
422
writer . WriteNumberValue ( 0 ) ;
423
423
else if ( value . Factor . HasValue && value . Interval . HasValue )
424
424
writer . WriteStringValue ( value . ToString ( ) ) ;
0 commit comments