0

I have a problem with migrating working program which is written on linux to windows. I clean it a little bit but following code snippet always give me an error.

code snippet:

...
struct timeval mytime;
gettimeofday(&mytime, (struct timezone*)0);
tseconds = (double) (mytime.tv_sec + mytime.tv_usec*1.0e-6);
...

stacktrace:

Error   1   error C2079: 'mytime' uses undefined struct 'timeval'
Warning 2   warning C4013: 'gettimeofday' undefined; assuming extern returning int
Error   3   error C2224: left of '.tv_sec' must have struct/union type
Error   4   error C2224: left of '.tv_usec' must have struct/union type
5   IntelliSense: incomplete type is not allowed
6   IntelliSense: incomplete type is not allowed
7   IntelliSense: incomplete type is not allowed

If someone could help me I would be very grateful!

MNie
  • 1,347
  • 1
  • 15
  • 35
  • 1
    Have you done the right #include statements on top of your source? Some .h files are different in Windows than in Unix. – Painted Black Oct 24 '14 at 13:56
  • duplicate? http://stackoverflow.com/questions/2494356/how-to-use-gettimeofday-or-something-equivalent-with-visual-studio-c-2008 – dave Oct 24 '14 at 14:03
  • 1
    You might use a framework like [Glib](https://developer.gnome.org/glib/stable/) or (if you can code in C++) [poco](http://pocoproject.org/), [boost](http://boost.org/), [Qt](http://qt-project.org/)... – Basile Starynkevitch Oct 24 '14 at 14:15

1 Answers1

0

@Dave thanks for link I add to my code below snippets:

const __int64 DELTA_EPOCH_IN_MICROSECS = 11644473600000000;
struct timeval2 {
    __int32 tv_sec;
    __int32 tv_usec;
};
struct timezone2
{
    __int32  tz_minuteswest; /* minutes W of Greenwich */
    bool  tz_dsttime;     /* type of dst correction */
};
int gettimeofday(struct timeval2 *tv/*in*/, struct timezone2 *tz/*in*/)
{
    FILETIME ft;
    __int64 tmpres = 0;
    TIME_ZONE_INFORMATION tz_winapi;
    int rez = 0;

    ZeroMemory(&ft, sizeof(ft));
    ZeroMemory(&tz_winapi, sizeof(tz_winapi));

    GetSystemTimeAsFileTime(&ft);

    tmpres = ft.dwHighDateTime;
    tmpres <<= 32;
    tmpres |= ft.dwLowDateTime;

    /*converting file time to unix epoch*/
    tmpres /= 10;  /*convert into microseconds*/
    tmpres -= DELTA_EPOCH_IN_MICROSECS;
    tv->tv_sec = (__int32) (tmpres*0.000001);
    tv->tv_usec = (tmpres % 1000000);


    //_tzset(),don't work properly, so we use GetTimeZoneInformation
    rez = GetTimeZoneInformation(&tz_winapi);
    tz->tz_dsttime = (rez == 2) ? true : false;
    tz->tz_minuteswest = tz_winapi.Bias + ((rez == 2) ? tz_winapi.DaylightBias : 0);

    return 0;
}

and the code builds correctly :)!

MNie
  • 1,347
  • 1
  • 15
  • 35