Browse Source

Reusing the timespan parsing code

Kohsuke Kawaguchi 12 years ago
parent
commit
18d02115f4
2 changed files with 16 additions and 10 deletions
  1. 2 2
      Advapi32.cs
  2. 14 8
      ServiceDescriptor.cs

+ 2 - 2
Advapi32.cs

@@ -255,10 +255,10 @@ namespace Advapi32
         /// </summary>
         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.Delay = delay;
+            this.Delay = (uint)delay.TotalMilliseconds;
         }
     }
 

+ 14 - 8
ServiceDescriptor.cs

@@ -108,15 +108,21 @@ namespace winsw
             }
             else
             {
-                string v = 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 ParseTimeSpan(e.InnerText);
+            }
+        }
+
+        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));
             }
+            return TimeSpan.FromMilliseconds(int.Parse(v));
         }
 
         private static readonly Dictionary<string,long> SUFFIX = new Dictionary<string,long> {
@@ -491,7 +497,7 @@ namespace winsw
                             throw new Exception("Invalid failure action: " + action);
                     }
                     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;
             }