For a while I’ve been frustrated by .NET’s System.TimeSpan and its incoherent constructors. Without referring to MSDN, can you tell me what these three objects describe?
new TimeSpan(0,0,1) new TimeSpan(0,0,1,0) // appending a zero new TimeSpan(0,0,1,0,0) // appending another zero
It happens the first two both describe one second but the third describes one microsecond!
Since then I found the static methods
TimeSpan.FromMinutes, which are much more readable. It’s still a tad tedious to write ‘one hour and thirty five minutes’:
I’m fond of Python’s timedelta, which lets you write:
from datetime import timedelta timedelta(hours=1,minutes=35)
You can understand that without reading any Python documentation.
Inspired, and using C# 4’s optional parameters, I wrote the missing method:
Now we can write the instantly readable:
That makes me very happy. Help yourself to this code.