Span of influence not quite as big as a TimeSpan (aka TimeSpan shot first!)

by Michael S. Kaplan, published on 2007/12/04 10:16 -05:00, original URI:

It starts simple.

Beyond the obvious confusion of the meaning of the word Span in the title, intended to be vaguely reminiscent of a classic mistake in distance vs. time found in the original Star Wars movie involving parsecs originally intended to prove that Han Solo was braggart who talked out of his ass at times to try to get a job, later retcon'ed into an outrageous explanation (possibly in an effort to rehabilitate Han's character just like Lucas did with having him not shoot Greedo first!).

Unfortunately I think the stretch here is too great and I should just get on with the post and stop trying to rehabilitate the title's allusion; that goal is as flawed as that of George Lucas vis-a-vis Han.... :-)

Anyway, as it said, it (the technical issue being discussed today) starts simple.

With GetDurationFormat and GetDurationFormatEx, two functions added to Vista and Server 2008 by the NLS team.

Very handy functionality when you need something that "Formats a duration of time as a time string for a locale specified by identifier.".

Now of course you can pass your own format string which kind of then ignores the locale, but it is obviously intended to most often represent results that any to be properly respectful to user settings.

Let's contrast it with the nearest managed analogue, the TimeSpan structure, which is described as something that "Represents a time interval."

Unfortunately, unlike the ToString() overrides for other such classes -- e.g. DateTime.ToString() -- the TimeSpan.ToString() has no override taking an IFormatProvider, which means no culture-sensitive formatting for TimeSpan, despite the obvious need for it.

The one bit of happiness -- the number of ticks in TimeSpan (retrievable as TimeSpan.Ticks) is defined as

The smallest unit of time is the tick, which is equal to 100 nanoseconds. A tick can be negative or positive.

And the ullDuration parameter to GetDurationFormat is defined as:

[in] 64-bit unsigned integer that represents the number of 100-nanosecond intervals in the duration. If both lpDuration and ullDuration are present, then lpDuration takes precedence. If lpDuration is NULL and illustration is zero, then the duration is zero.

So it looks like you can format a TimeSpan using the ulDuration parameter of GetDurationFormat if you are willing to live with the >= Vista limitation on formatting (TimeSpan has been around at least since 1.1 of the .NET Framework, so you can use this on many more versions than exist for the unmanaged function).

(One caveat -- the managed function can be negative or positive, while the unmanaged function takes an unsigned value -- so you are limited in that way as well, by the need to store the sign separately and figure out how to integrate it in later, conceptually!)

So as the title indicates, the NLS team's scope of influence does not extend as far as a TimeSpan.... since the class seems to have stayed below the System.Globalization radar almost since the beginning of the .NET Framework....


This post brought to you by(U+17bf, a.k.a. KHMER VOWEL SIGN YA)

# Andrew Webb on 7 Dec 2007 6:50 AM:

Not only does TimeSpan's ToString not allow you to control formatting, but it also assumes that the decimal point is always the "." character.  Changing the current thread's current culture to fr-FR did not cause TimeSpan.ToString to use "," instead.  Naughty.  And given the way of things, unlikely to ever be fixed.

# Tanveer Badar on 24 Dec 2007 11:18 AM:

Why not file a bug on about it?

# Michael S. Kaplan on 25 Dec 2007 1:00 AM:

For an in-built design limitation that has existed for the last five versions of the product? Hmmm...

go to newer or older post, or back to index or month or day