Presenting TimeSpanHelper

A TimeSpan is a structure that represents a time interval or a duration. To make the everything as clear as possible, a well designed system should not only know the quantity, but also the unit of that quantity. In order to create a TimeSpan that represents the right value, you will probably use one of the FromXXX (Hours, Days, Minutes, Seconds) methods as following:

double value = 1;
string unit = "Day";

TimeSpan actual;
if (unit == "Day")
{
 actual = TimeSpan.FromDays(value);
}
else if (unit == "Hour")
{
 actual = TimeSpan.FromHours(value);
}
// more else if statements...

After a while you get bored of writing that same if-else (or switch) construct and you end up wishing for something like:

double value = 1;
string unit = "Day";

TimeUnit timeUnit = EnumHelper.Parse<timeUnit>(unit);
TimeSpan actual = TimeSpanHelper.Create(value, timeUnit);

Well, from now on you can find it in BeTimvwFramework ;)

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>