-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Removed all System.TimeSpan's - Created a new Folder 'Utils' - Created a new class 'TimeStringConverter' with two sub-classes Earth-/KerbinTimeStringConverter Fixes RemoteTechnologiesGroup#192
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace RemoteTech | ||
{ | ||
/// <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.SecondsPerDay = 86400; | ||
this.SecondsPerHour = 3600; | ||
this.SecondsPerMinute = 60; | ||
} | ||
} | ||
|
||
/// <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.SecondsPerDay = 21600; | ||
this.SecondsPerHour = 3600; | ||
this.SecondsPerMinute = 60; | ||
} | ||
} | ||
|
||
/// <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> | ||
/// Constant value for the days per year | ||
/// </summary> | ||
public const int DAYS_PER_YEAR = 365; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Peppie84
Author
Owner
|
||
/// <summary> | ||
/// Get the seconds for one year | ||
/// </summary> | ||
protected uint SecondsPerYear { get { return TimeStringConverter.DAYS_PER_YEAR * this.SecondsPerDay; } } | ||
/// <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]*)")); | ||
This comment has been minimized.
Sorry, something went wrong.
Starstrider42
|
||
|
||
/// <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" the return value will be 0. | ||
/// </summary> | ||
/// <param name="duration">time string like "1d 2m 3s". Possible values: 0y 0d 0h 0m 0s</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; | ||
} | ||
} | ||
|
||
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> | ||
/// <returns>Given time as seconds converted to a time string like "1d 2m 3s"</returns> | ||
public String parseDouble(Double duration) | ||
{ | ||
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"); | ||
|
||
// kill the micro seconds | ||
s.Append(time.ToString("F2")); | ||
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); | ||
} | ||
} | ||
} |
3 comments
on commit 92192ac
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why have a separate Utils/
folder instead of putting it in SimpleTypes/
? Or why isn't RTUtils
in Utils/
? It seems a little inconsistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see this class as a "type" it's only a functional class, that was my reason to put this class into a new folder named "Utils".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed some things: ecc0b4c
This is NOT true, in either Kerbin or Earth days. See http://wiki.kerbalspaceprogram.com/wiki/Kerbin.