Format Milliseconds to Human readable Time.

16 05 2008


For profiling performance we usually use functions such as GetTickCount() etc to get the time in millisecond resolution. Since they are in milliseconds, its a bit difficult for us to interpret, because our real world time is expressed in hours, minutes and seconds. There is a handy function in the shell to convert the time in milliseconds to human readable form.


You can use the function – StrFromTimeInterval() for converting time in milliseconds to human readable form. See the code snippet below.

#include "Shlwapi.h"
...
// Get the time in milli-seconds.
DWORD dwTickCount = GetTickCount();

// Declare the buffer to hold the string.
const int BufferSize = 255;
TCHAR CurrentTime[BufferSize];

// Get formatted time from tickcount.
StrFromTimeInterval( CurrentTime,
                     BufferSize,
                     dwTickCount,
                     3 ); // hr:min:sec


Its very useful while logging performance related data. And to be honest, several years ago I’ve written a utility function in my project to implement the same, without knowing about this function. Indeed, got a hand full of bugs. 😉

BTW, don’t forget to add Shlwapi.lib to the project settings.


Targeted Audience – Beginners.