Convert GetTickCount() vxWorks to Linux

user195488 picture user195488 · Oct 11, 2011 · Viewed 10.1k times · Source

I am porting some vxWorks code to Linux.

I looked at this answer and it recommends CLOCK_MONOTONIC. Is that a suitable replacement for the following define:

#define GetTickCount()    ((1000.0 * (double)tickGet())/((double)sysClkRateGet())))

?

Answer

Robᵩ picture Robᵩ · Oct 11, 2011

GetTickCount is a windows API described thus:

Retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days

Yes, CLOCK_MONOTONIC is the correct POSIX clock to use. Here is untested code for you:

double GetTickCount(void) 
{
  struct timespec now;
  if (clock_gettime(CLOCK_MONOTONIC, &now))
    return 0;
  return now.tv_sec * 1000.0 + now.tv_nsec / 1000000.0;
}