Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Porting time APIs from Linux to Visual Studio 2008

I have an application that I am porting to Microsoft Visual Studio 2008 that builds and runs fine on Linux.

I am having trouble with the time routines, my Linux code looks like this:

#include <sys/types.h>
#include <sys/time.h>

typedef long long Usec;

inline Usec timevalToUsec(const timeval &tv)
{
  return (((Usec) tv.tv_sec) * 1000000) + ((Usec) tv.tv_usec);
}

But the compiler fails on the sys/time.h header file:

fatal error C1083: Cannot open include file:
      'sys/time.h': No such file or directory

If I change the include to just time.h I get a different error with timeval not being defined:

error C4430: missing type specifier - int assumed.
      Note: C++ does not support default-int

This is due to the timeval not being defined.

Is including time.h instead of sys/time.h correct, and if so, where do I get the definition of struct timeval in Microsoft Visual Studio 2008?

like image 435
WilliamKF Avatar asked Feb 22 '23 07:02

WilliamKF


2 Answers

The winsock2.h header fill will pull in struct timeval since it's used in calls like select.

like image 77
David Schwartz Avatar answered Mar 06 '23 18:03

David Schwartz


It exists, just not in "sys/time.h". Timeval is in, interestingly, Winsock2.h.

like image 38
Joe Avatar answered Mar 06 '23 19:03

Joe