diff --git a/X10D/src/StringExtensions.cs b/X10D/src/StringExtensions.cs
index 5ba81bc..431a7c5 100644
--- a/X10D/src/StringExtensions.cs
+++ b/X10D/src/StringExtensions.cs
@@ -210,5 +210,16 @@
{
return extension ? (new NetworkCredential(String.Empty, str).Password) : str.ToString();
}
+
+ ///
+ /// Parses a shorthand time span string (e.g. 3w 2d 1.5h) and converts it to an instance of
+ /// .
+ ///
+ /// The input string.
+ /// Returns an instance of .
+ public static TimeSpan ToTimeSpan(this string str)
+ {
+ return TimeSpanParser.Parse(str);
+ }
}
}
diff --git a/X10D/src/TimeSpanParser.cs b/X10D/src/TimeSpanParser.cs
new file mode 100644
index 0000000..1cdd1ca
--- /dev/null
+++ b/X10D/src/TimeSpanParser.cs
@@ -0,0 +1,77 @@
+namespace X10D
+{
+ #region Using Directives
+
+ using System;
+ using System.Diagnostics;
+ using System.Text.RegularExpressions;
+
+ #endregion
+
+ public static class TimeSpanParser
+ {
+ ///
+ /// Parses a shorthand time span string (e.g. 3w 2d 1.5h) and converts it to an instance of
+ /// .
+ ///
+ /// The input string.
+ /// Returns an instance of .
+ public static TimeSpan Parse(string input)
+ {
+ const string realNumberPattern = @"([0-9]*\.[0-9]+|[0-9]+)";
+ string pattern = $@"^(?:{realNumberPattern} *w)? *" +
+ $@"(?:{realNumberPattern} *d)? *" +
+ $@"(?:{realNumberPattern} *h)? *" +
+ $@"(?:{realNumberPattern} *m)? *" +
+ $@"(?:{realNumberPattern} *s)? *" +
+ $@"(?:{realNumberPattern} *ms)?$";
+
+ Match match = Regex.Match(input, pattern);
+ double weeks = 0, days = 0, hours = 0, minutes = 0, seconds = 0, milliseconds = 0;
+
+ if (match.Groups[1].Success)
+ {
+ weeks = Double.Parse(match.Groups[1].Value);
+ }
+
+ if (match.Groups[2].Success)
+ {
+ days = Double.Parse(match.Groups[2].Value);
+ }
+
+ if (match.Groups[3].Success)
+ {
+ hours = Double.Parse(match.Groups[3].Value);
+ }
+
+ if (match.Groups[4].Success)
+ {
+ minutes = Double.Parse(match.Groups[4].Value);
+ }
+
+ if (match.Groups[5].Success)
+ {
+ seconds = Double.Parse(match.Groups[5].Value);
+ }
+
+ if (match.Groups[6].Success)
+ {
+ milliseconds = Double.Parse(match.Groups[6].Value);
+ }
+
+ Trace.WriteLine($"Input: {input}");
+ Trace.WriteLine($"Parsed: {weeks}w {days}d {hours}h {minutes}m {seconds}s {milliseconds}ms");
+
+ TimeSpan span = TimeSpan.Zero;
+
+ span += TimeSpan.FromDays(weeks * 7);
+ span += TimeSpan.FromDays(days);
+ span += TimeSpan.FromHours(hours);
+ span += TimeSpan.FromMinutes(minutes);
+ span += TimeSpan.FromSeconds(seconds);
+ span += TimeSpan.FromMilliseconds(milliseconds);
+
+ return span;
+ }
+ }
+}