When it comes to duration, the little things can mean a lot

by Michael S. Kaplan, published on 2006/11/07 05:20 -05:00, original URI: http://blogs.msdn.com/b/michkap/archive/2006/11/07/1013908.aspx


At the recent MVP Webcast that I did, one of the viewers (I want to say it was Mike Dunn?) asked what the difference was between the (new in Vista) GetDurationFormat and StringFromTimeInterval.

At the time, my answer was easy -- I know what the former is since it is an NLS function that colleague Mike Dolenga wrote and which starts its life in the Vista release of Windows. And I have no idea what StringFromTimeInterval is!

Anyway, after spending a little time looking it up and realizing that he was actually referring to the shlwapi StrFromTimeInterval function, and so I figured that now that I knew what we were talking about, I could answer intelligently. :-)

The major differences:

StrFromTimeInterval has no way to specify a locale, no way to specify format strings, takes its time in a DWORD that represents milliseconds, and is limited to hours/minutes/seconds with milliseconds being ignored.

On the other hand, GetDurationFormat (and its counterpart GetDurationFormatEx that takes locale names rather than LCIDs) lets you specify the locale to use or use your own formatting string, can take either a SYSTEMTIME struct or a 64 bit unsigned integer representing the number of 100 nanosecond intervals in the duration to specify, and can return days/hours/minutes/seconds/milliseconds/microseconds, as desired.

Both will return a formatted string, but the NLS function has a lot more functionality. :-)

Oh, also the Shell function supports an "A" version, which we don't do anymore.

And another interestingly weird difference is that StrFromTimeInterval does load up its hours/minutes/seconds strings based on localized resources (presumably the current UI language, though the documentation did not specify details on this subject). Perhaps someone from the Shell side can explain how that part of it works....

 

This post brought to you by ยต (U+00b5, a.k.a. MICRO SIGN)


Sander on 7 Nov 2006 5:52 AM:

The first link is broken (missing http://)

Michael S. Kaplan on 7 Nov 2006 6:30 AM:

Whoops, looks like they are fixed now (the other GetDurationFormat links were also broken). Thanks. :-)

Mike Dunn on 7 Nov 2006 2:06 PM:

I think you mentioned that GetDurationFormat was asked for by WMP, for use in their time displays. GetDurationFormat returns a string like "42:37" for 42 minutes and 37 seconds, just the formatting that a media player would need.

You can see StrFromTimeInterval in action in the IE download progress window, it would show "42 min 37 sec".

Michael S. Kaplan on 7 Nov 2006 2:11 PM:

Ah, but you can use GetDurationFormat to get that same format by providing your own formatting string -- so it is much more powerful. :-)


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