Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / February 2007

Tip: Looking for answers? Try searching our database.

TimeSpan inadequacies

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris - 02 Feb 2007 17:28 GMT
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
Göran Andersson - 02 Feb 2007 20:13 GMT
> 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
>
[quoted text clipped - 13 lines]
> 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
[quoted text clipped - 3 lines]
>
> -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.

Signature

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

Mattias Sjögren - 03 Feb 2007 08:21 GMT
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

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Göran Andersson - 11 Feb 2007 04:41 GMT
> Göran,
>
[quoted text clipped - 7 lines]
>
> 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/frlr
fsystemiconvertibleclasstopic.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.

Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.