TimeSpan inadequacies

C

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
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Chris said:
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.
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.
 
M

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
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Mattias said:
Göran,


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/d...ef/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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top