forked from RemoteTechnologiesGroup/RemoteTech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeStringConverter.cs
More file actions
184 lines (168 loc) · 6.75 KB
/
Copy pathTimeStringConverter.cs
File metadata and controls
184 lines (168 loc) · 6.75 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace RemoteTech.SimpleTypes
{
/// <summary>
/// This class converts time strings like "1d 2m 2s" into a
/// double value as seconds and also vice versa, based on
/// earth time.
/// </summary>
class EarthTimeStringConverter : TimeStringConverter
{
/// <summary>
/// Define the base seconds for days, hours and minutes
/// </summary>
public EarthTimeStringConverter()
{
this.SecondsPerYear = 31536000; // = 365d
this.SecondsPerDay = 86400; // = 24h
this.SecondsPerHour = 3600; // = 60m
this.SecondsPerMinute = 60; // = 60s
}
}
/// <summary>
/// This class converts time strings like "1d 2m 2s" into a
/// double value as seconds and also vice versa, based on
/// kerbin time.
/// </summary>
class KerbinTimeStringConverter : TimeStringConverter
{
/// <summary>
/// Define the base seconds for days, hours and minutes
/// </summary>
public KerbinTimeStringConverter()
{
this.SecondsPerYear = 9201600; // = 426d
this.SecondsPerDay = 21600; // = 6h
this.SecondsPerHour = 3600; // = 60m
this.SecondsPerMinute = 60; // = 60s
}
}
/// <summary>
/// This class converts time strings like "1d 2m 2s" into a
/// double value as seconds and also vice versa.
/// </summary>
abstract class TimeStringConverter
{
/// <summary>
/// Get the seconds for one year
/// </summary>
protected uint SecondsPerYear;
/// <summary>
/// Get the seconds for one day
/// </summary>
protected uint SecondsPerDay;
/// <summary>
/// Get the seconds for one hour
/// </summary>
protected uint SecondsPerHour;
/// <summary>
/// Get the seconds for one minute
/// </summary>
protected uint SecondsPerMinute;
/// <summary>
/// Expression for parsing the time string
/// </summary>
private static readonly Regex DurationRegex = new Regex(
String.Format("{0}?{1}?{2}?{3}?{4}?",
@"(?:(?<seconds>\d*\.?\d+)\s*s[a-z]*[,\s]*)",
@"(?:(?<minutes>\d*\.?\d+)\s*m[a-z]*[,\s]*)",
@"(?:(?<hours>\d*\.?\d+)\s*h[a-z]*[,\s]*)",
@"(?:(?<days>\d*\.?\d+)\s*d[a-z]*[,\s]*)",
@"(?:(?<years>\d*\.?\d+)\s*y[a-z]*[,\s]*)"));
/// <summary>
/// This method will parse a time string like "1d 2m 3s" and returns the
/// seconds for this string. If no matching string was found with the
/// "DurationRegex" we'll try to parse the given duration string as seconds.
/// </summary>
/// <param name="duration">time string like "1d 2m 3s" or "500" (as seconds).
/// Possible suffixes: y,d,h,m and s</param>
/// <returns>Given time string converted in seconds</returns>
public Double parseString(String duration)
{
Double timeInSeconds = 0;
MatchCollection matches = TimeStringConverter.DurationRegex.Matches(duration);
foreach (Match match in matches)
{
if (match.Groups["seconds"].Success)
{
timeInSeconds += Double.Parse(match.Groups["seconds"].Value);
}
if (match.Groups["minutes"].Success)
{
timeInSeconds += Double.Parse(match.Groups["minutes"].Value) * this.SecondsPerMinute;
}
if (match.Groups["hours"].Success)
{
timeInSeconds += Double.Parse(match.Groups["hours"].Value) * this.SecondsPerHour;
}
if (match.Groups["days"].Success)
{
timeInSeconds += Double.Parse(match.Groups["days"].Value) * this.SecondsPerDay;
}
if (match.Groups["years"].Success)
{
timeInSeconds += Double.Parse(match.Groups["years"].Value) * this.SecondsPerYear;
}
}
// if we've no matches, try parsing the string as seconds
if (timeInSeconds == 0)
{
double tmpTimeinSeconds = 0.0;
if (Double.TryParse(duration, out tmpTimeinSeconds))
{
timeInSeconds = tmpTimeinSeconds;
}
}
return timeInSeconds;
}
/// <summary>
/// This method will parse a time as seconds and returns the time string of this.
/// </summary>
/// <param name="duration">Time as seconds</param>
/// <param name="withMicroSecs">[optional] Add the microsecs to the time string, default true</param>
/// <returns>Given time as seconds converted to a time string like "1d 2m 3s"</returns>
public String parseDouble(Double duration, bool withMicroSecs = true)
{
Double time = duration;
StringBuilder s = new StringBuilder();
// extract years
if (time >= this.SecondsPerYear)
time = this.calcFromSecondsToSring(time, s, this.SecondsPerYear, "y");
// extract days
if (time >= this.SecondsPerDay)
time = this.calcFromSecondsToSring(time, s, this.SecondsPerDay, "d");
// extract hours
if (time >= this.SecondsPerHour)
time = this.calcFromSecondsToSring(time, s, this.SecondsPerHour, "h");
// extract minutes
if (time >= this.SecondsPerMinute)
time = this.calcFromSecondsToSring(time, s, this.SecondsPerMinute, "m");
if (withMicroSecs)
{
s.Append(time.ToString("F2"));
}
else
{
s.Append(time.ToString("F0"));
}
s.Append("s");
return s.ToString();
}
/// <summary>
/// This method extracts the time segments
/// </summary>
/// <param name="time">Seconds to convert</param>
/// <param name="appandTo">Stringbuilder to appand to</param>
/// <param name="baseSeconds">Base for the calculation</param>
/// <param name="prefix">Will be appand to the string builder</param>
/// <returns>The remaining seconds</returns>
private Double calcFromSecondsToSring(Double time, StringBuilder appandTo, uint baseSeconds, String prefix)
{
appandTo.Append(Math.Floor(time / baseSeconds));
appandTo.Append(prefix);
return (time % baseSeconds);
}
}
}