Home All Groups Group Topic Archive Search About

TimeSpan inadequacies

Author
2 Feb 2007 5:28 PM
Chris
Just wanting to vent my dissatisfaction with the TimeSpan type.  It
isn't IConvertible and there is no TypeCode for it; so, when someone
passes my interface an Object and I need to deal with it according to
its type, I have to make a special case for it.

Jay Harlow responds on a thread subject

"How to Format a TimeSpan Without the Decimal Part of the Seconds"

"...when I need custom formatting of a TimeSpan is "convert"
it to a DateTime, then use custom DateTime formatting..."

This is a weak solution.  (No disrepect to Jay.) TimeSpans are
durations of time; DateTimes are specific points in time.  The
semantic difference is between telling your friend you will meet them
in one hour versus meeting them at 1:00 PM.  What if you have the
value 370 days.  I'm pretty sure that DateTime is not going to handle
that well.  One year and five days you say?  Really, for all years?
No.  Aside from leap year problems there are issues with Julian versus
Gregorian calendars, the Jewish calendar, etc.  The value 370 days is
perfectly meaningful when talking about intervals, but is meaningless
for DateTimes.

Just hoping that Microsoft picks up on this and makes some
improvements at some point.  I don't know; maybe they decided there
was no other type that preserved the same meaning and so consiously
left it out of Convert and IConvertable.  At least there should be a
TypeCode.

-Chris

Author
2 Feb 2007 8:13 PM
Göran_Andersson
Chris wrote:
> Just wanting to vent my dissatisfaction with the TimeSpan type.  It
> isn't IConvertible and there is no TypeCode for it; so, when someone
> passes my interface an Object and I need to deal with it according to
> its type, I have to make a special case for it.

Only the common language runtime types have type codes. There are only
15 types that are, so TimeSpan is part of the overwhelming majority that
aren't.

You have to make a special case for any data type that is passed to your
interface that isn't a common language runtime type.

Show quote
> Jay Harlow responds on a thread subject
>
> "How to Format a TimeSpan Without the Decimal Part of the Seconds"
>
> "...when I need custom formatting of a TimeSpan is "convert"
> it to a DateTime, then use custom DateTime formatting..."
>
> This is a weak solution.  (No disrepect to Jay.) TimeSpans are
> durations of time; DateTimes are specific points in time.  The
> semantic difference is between telling your friend you will meet them
> in one hour versus meeting them at 1:00 PM.  What if you have the
> value 370 days.  I'm pretty sure that DateTime is not going to handle
> that well.  One year and five days you say?  Really, for all years?
> No.  Aside from leap year problems there are issues with Julian versus
> Gregorian calendars, the Jewish calendar, etc.  The value 370 days is
> perfectly meaningful when talking about intervals, but is meaningless
> for DateTimes.

I don't really see why using DateTime to get custom formatting of a
TimeSpan would be a problem, as long as you use it for TimeSpan values
where you can get a reasonable formatting.

Expressing a TimeSpan in years or months is impossible without having a
set starting time to relate it to. Expressing it in weeks is hardly
meaningful either.

Just get the highest meaningful unit from the TimeSpan, for example by
using the Days property. Subtract the days from the TimeSpan and convert
the reminder to a DateTime so that you can format it.

> Just hoping that Microsoft picks up on this and makes some
> improvements at some point.  I don't know; maybe they decided there
> was no other type that preserved the same meaning and so consiously
> left it out of Convert and IConvertable.  At least there should be a
> TypeCode.
>
> -Chris
>

I don't think that adding TimeSpan to the common language runtime types
is really an option. Giving it a TypeCode anyway would be misleading.

--
Göran Andersson
_____
http://www.guffa.com
Author
3 Feb 2007 8:21 AM
Mattias Sjögren
Göran,

>Only the common language runtime types have type codes. There are only
>15 types that are, so TimeSpan is part of the overwhelming majority that
>aren't.

What's your definition of a CLR type? I don't see why DateTime and
DBNull would be any more of a CLR type than TimeSpan, yet they both
have a TypeCode.


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
11 Feb 2007 4:41 AM
Göran_Andersson
Mattias Sjögren wrote:
> Göran,
>
>> Only the common language runtime types have type codes. There are only
>> 15 types that are, so TimeSpan is part of the overwhelming majority that
>> aren't.
>
> What's your definition of a CLR type? I don't see why DateTime and
> DBNull would be any more of a CLR type than TimeSpan, yet they both
> have a TypeCode.
>
>
> Mattias
>

The definition is not mine.

"The common language runtime types are Boolean, SByte, Byte, Int16,
UInt16, Int32, UInt32, Int64, UInt64, Single, Double, Decimal, DateTime,
Char, and String."

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiconvertibleclasstopic.asp

I wasn't entirely correct in saying that only CLR types have type codes,
as there are also type codes for DBNull, Empty and Object.

--
Göran Andersson
_____
http://www.guffa.com

AddThis Social Bookmark Button