Reusing the timespan parsing code

pull/18/head
Kohsuke Kawaguchi 2013-04-20 18:26:44 -07:00
parent 231db43ba1
commit 18d02115f4
2 changed files with 17 additions and 11 deletions

View File

@ -255,10 +255,10 @@ namespace Advapi32
/// </summary> /// </summary>
public uint Delay; public uint Delay;
public SC_ACTION(SC_ACTION_TYPE type, uint delay) public SC_ACTION(SC_ACTION_TYPE type, TimeSpan delay)
{ {
this.Type = type; this.Type = type;
this.Delay = delay; this.Delay = (uint)delay.TotalMilliseconds;
} }
} }

View File

@ -108,17 +108,23 @@ namespace winsw
} }
else else
{ {
string v = e.InnerText; return ParseTimeSpan(e.InnerText);
foreach (var s in SUFFIX) {
if (v.EndsWith(s.Key))
{
return TimeSpan.FromMilliseconds(int.Parse(v.Substring(0,v.Length-s.Key.Length).Trim())*s.Value);
}
}
return TimeSpan.FromMilliseconds(int.Parse(v));
} }
} }
private TimeSpan ParseTimeSpan(string v)
{
v = v.Trim();
foreach (var s in SUFFIX)
{
if (v.EndsWith(s.Key))
{
return TimeSpan.FromMilliseconds(int.Parse(v.Substring(0, v.Length - s.Key.Length).Trim()) * s.Value);
}
}
return TimeSpan.FromMilliseconds(int.Parse(v));
}
private static readonly Dictionary<string,long> SUFFIX = new Dictionary<string,long> { private static readonly Dictionary<string,long> SUFFIX = new Dictionary<string,long> {
{ "ms", 1 }, { "ms", 1 },
{ "sec", 1000L }, { "sec", 1000L },
@ -491,7 +497,7 @@ namespace winsw
throw new Exception("Invalid failure action: " + action); throw new Exception("Invalid failure action: " + action);
} }
XmlAttribute delay = n.Attributes["delay"]; XmlAttribute delay = n.Attributes["delay"];
r.Add(new SC_ACTION(type, delay!=null ? uint.Parse(delay.Value) : 0)); r.Add(new SC_ACTION(type, delay != null ? ParseTimeSpan(delay.Value) : TimeSpan.Zero));
} }
return r; return r;
} }