@@ -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 )
@@ -120,8 +83,6 @@ public Time(string timeUnit)
12083 Milliseconds = ( long ) this . Factor ;
12184 }
12285
123- public TimeSpan ToTimeSpan ( ) => TimeSpan . FromMilliseconds ( this . Milliseconds ) ;
124-
12586 public int CompareTo ( Time other )
12687 {
12788 if ( other == null ) return 1 ;
@@ -136,17 +97,18 @@ public int CompareTo(Time other)
13697 public static bool operator > ( Time left , Time right ) => left . CompareTo ( right ) > 0 ;
13798 public static bool operator >= ( Time left , Time right ) => left . CompareTo ( right ) > 0 || left . Equals ( right ) ;
13899
139- public static bool operator == ( Time left , Time right ) =>
100+ public static bool operator == ( Time left , Time right ) =>
140101 object . ReferenceEquals ( left , null ) ? object . ReferenceEquals ( right , null ) : left . Equals ( right ) ;
141102
142103 public static bool operator != ( Time left , Time right ) =>
143104 ! object . ReferenceEquals ( left , null ) && ! object . ReferenceEquals ( right , null ) && ! left . Equals ( right ) ;
144105
106+ public TimeSpan ToTimeSpan ( ) => TimeSpan . FromMilliseconds ( this . Milliseconds ) ;
107+
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}
0 commit comments