-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathTime.cs
170 lines (147 loc) · 5.52 KB
/
Time.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
namespace Nest
{
[JsonConverter(typeof(TimeJsonConverter))]
public class Time : IComparable<Time>, IEquatable<Time>
{
private static readonly Regex _expressionRegex = new Regex(@"^(?<factor>\d+(?:\.\d+)?)(?<interval>(?:y|M|w|d|h|m|s|ms))?$", RegexOptions.Compiled | RegexOptions.ExplicitCapture);
private static readonly long _year = (long)TimeSpan.FromDays(365).TotalMilliseconds;
private static readonly long _week = (long)TimeSpan.FromDays(7).TotalMilliseconds;
private static readonly long _day = (long)TimeSpan.FromDays(1).TotalMilliseconds;
private static readonly long _hour = (long)TimeSpan.FromHours(1).TotalMilliseconds;
private static readonly long _minute = (long)TimeSpan.FromMinutes(1).TotalMilliseconds;
private static readonly long _second = (long)TimeSpan.FromSeconds(1).TotalMilliseconds;
public double? Factor { get; }
public TimeUnit? Interval { get; }
public long Milliseconds { get; }
public static implicit operator Time(TimeSpan span) => new Time(span);
public static implicit operator Time(long milliseconds) => new Time(milliseconds);
public static implicit operator Time(string expression) => new Time(expression);
public Time(double factor, TimeUnit interval)
{
this.Factor = factor;
this.Interval = interval;
if (interval == TimeUnit.Year)
Milliseconds = (long)factor * _year;
else if (interval == TimeUnit.Week)
Milliseconds = (long)factor * _week;
else if (interval == TimeUnit.Day)
Milliseconds = (long)factor * _day;
else if (interval == TimeUnit.Hour)
Milliseconds = (long)factor * _hour;
else if (interval == TimeUnit.Minute)
Milliseconds = (long)factor * _minute;
else if (interval == TimeUnit.Second)
Milliseconds = (long)factor * _second;
else //ms
Milliseconds = (long)factor;
}
public Time(TimeSpan timeSpan)
{
var ms = timeSpan.TotalMilliseconds;
this.Milliseconds = (long)ms;
if (ms >= _year)
{
Factor = ms / _year;
Interval = TimeUnit.Year;
}
else if (ms >= _week)
{
Factor = ms / _week;
Interval = TimeUnit.Week;
}
else if (ms >= _day)
{
Factor = ms / _day;
Interval = TimeUnit.Day;
}
else if (ms >= _hour)
{
Factor = ms / _hour;
Interval = TimeUnit.Hour;
}
else if (ms >= _minute)
{
Factor = ms / _minute;
Interval = TimeUnit.Minute;
}
else if (ms >= _second)
{
Factor = ms / _second;
Interval = TimeUnit.Second;
}
else
{
Factor = ms;
Interval = TimeUnit.Millisecond;
}
}
public Time(long milliseconds)
{
this.Milliseconds = milliseconds;
}
public Time(string timeUnit)
{
if (timeUnit.IsNullOrEmpty()) throw new ArgumentException("Time expression string is empty", nameof(timeUnit));
var match = _expressionRegex.Match(timeUnit);
if (!match.Success) throw new ArgumentException($"Time expression '{timeUnit}' string is invalid", nameof(timeUnit));
this.Factor = double.Parse(match.Groups["factor"].Value, CultureInfo.InvariantCulture);
this.Interval = match.Groups["interval"].Success
? match.Groups["interval"].Value.ToEnum<TimeUnit>()
: TimeUnit.Millisecond;
if (this.Interval == TimeUnit.Year)
Milliseconds = (long)(this.Factor * _year);
else if (this.Interval == TimeUnit.Week)
Milliseconds = (long)(this.Factor * _week);
else if (this.Interval == TimeUnit.Day)
Milliseconds = (long)(this.Factor * _day);
else if (this.Interval == TimeUnit.Hour)
Milliseconds = (long)(this.Factor * _hour);
else if (this.Interval == TimeUnit.Minute)
Milliseconds = (long)(this.Factor * _minute);
else if (this.Interval == TimeUnit.Second)
Milliseconds = (long)(this.Factor * _second);
else //ms
Milliseconds = (long)this.Factor;
}
public TimeSpan ToTimeSpan() => TimeSpan.FromMilliseconds(this.Milliseconds);
public int CompareTo(Time other)
{
if (other == null) return 1;
if (this.Milliseconds == other.Milliseconds) return 0;
if (this.Milliseconds < other.Milliseconds) return -1;
return 1;
}
public static bool operator <(Time left, Time right) => left.CompareTo(right) < 0;
public static bool operator <=(Time left, Time right) => left.CompareTo(right) < 0 || left.Equals(right);
public static bool operator >(Time left, Time right) => left.CompareTo(right) > 0;
public static bool operator >=(Time left, Time right) => left.CompareTo(right) > 0 || left.Equals(right);
public static bool operator ==(Time left, Time right) =>
object.ReferenceEquals(left, null) ? object.ReferenceEquals(right, null) : left.Equals(right);
public static bool operator !=(Time left, Time right) =>
!object.ReferenceEquals(left, null) && !object.ReferenceEquals(right, null) && !left.Equals(right);
public override string ToString()
{
if (this.Factor == null) return this.Milliseconds.ToString(CultureInfo.InvariantCulture);
return this.Factor.Value.ToString("0.##", CultureInfo.InvariantCulture) +
this.Interval.GetValueOrDefault().GetStringValue();
}
public bool Equals(Time other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Milliseconds == other.Milliseconds;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Time)obj);
}
public override int GetHashCode() => this.Milliseconds.GetHashCode();
}
}