Introduction
Whenever you deal with Timers, Timeouts, or anything which involves calculation on dates you will end up dealing with the TimeSpan
class.
I don't find particularly readable to write the following code :
var theTimespan = new TimeSpan(0, 5, 0, 0, 0);
and find much more easy to read this one:
var theTimespan = 5.Hours();
Extension methods
Use these methods (which extends the int class) to create TimeSpan
the easy and readable way.
public static TimeSpan Days(this int value)
{
return new TimeSpan(value, 0, 0, 0, 0);
}
public static TimeSpan Years(this int value)
{
var dt = DateTime.Now.AddYears(value);
return (dt - DateTime.Now).Duration();
}
public static TimeSpan Hours(this int value)
{
return new TimeSpan(0, value, 0, 0, 0);
}
public static TimeSpan Minutes(this int value)
{
return new TimeSpan(0, 0, value, 0, 0);
}
public static TimeSpan Seconds(this int value)
{
return new TimeSpan(0, 0, 0, value, 0);
}
public static TimeSpan Milliseconds(this int value)
{
return new TimeSpan(0, 0, 0, 0, value);
}
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.