I want to pass time interval as a string (to program args) and then I want to parse it to TimeSpan
. I created simple class:
public static class TimeSpanConverter
{
public static TimeSpan Convert(string input)
{
var units = new Dictionary<string, int>()
{
{@"(\d+)(ms|mili[|s]|milisecon[|s])", 1 },
{@"(\d+)(s|sec|second[|s])", 1000 },
{@"(\d+)(m|min[|s])", 60000 },
{@"(\d+)(h|hour[|s])", 3600000 },
{@"(\d+)(d|day[|s])", 86400000 },
{@"(\d+)(w|week[|s])", 604800000 },
};
var timespan = new TimeSpan();
foreach(var x in units)
{
var matches = Regex.Matches(input, x.Key);
foreach(Match match in matches)
{
var amount = System.Convert.ToInt32(match.Groups[1].Value);
timespan = timespan.Add(TimeSpan.FromMilliseconds(x.Value * amount));
}
}
return timespan;
}
}
Which is working, and I can easly parse like this:
var interval = TimeSpanConverter.Convert("1day13hour2min12s52ms");
What do you think about that? How can I improve that? I have a feeling, that it is not well coded.