Get time in milliseconds on Windows and Linux

Please note that in this case the time expressed in milliseconds is not a conversion of the system time but is a number of milliseconds elapsed by a specific time in the past different according to operating system used. This value is useful if you want to calculate a timeout based to millisecods unit.




 In Microsoft Windows you can call directly this API:

GetTickCount();

This call, based to MSDN documentation, return the number of milliseconds that have elapsed since the system was started, up to 49.7 days.

In Linux you can use this ready-made function:

#include <sys/time.h>

unsigned long GetMillisecondsTime()
{
   struct timeval tv;       
   if(gettimeofday(&tv, NULL) != 0) return 0;
   return (unsigned long)((tv.tv_sec * 1000ul) + (tv.tv_usec / 1000ul));
}

This function return the number of milliseconds elapsed since Epoch.

Comments

Popular posts from this blog

Access GPIO from Linux user space

Android: adb push and read-only file system error

Tree in SQL database: The Nested Set Model