none
gettimeofday

    Question

  • there's a function gettimeofday in linux, however, is there any windows API realize the same function?

    i got a little confused with getlocaltime and getsystemtime, which one should i choose? and which head file should i include? i wanna find the codes.

    Wednesday, December 26, 2007 12:11 PM

Answers

  • Hi~

    I'm really sorry that I copied from below page.

    http://www.openasthra.com/c-tidbits/gettimeofday-function-for-windows/

     

    here is source code.

    http://www.openasthra.com/wp-content/uploads/gettimeofday.c

     

     here is total contents.

    The gettimeofday() function obtains the current time, expressed as seconds and microseconds since the Epoch, and store it in the timeval structure pointed to by tv. As posix says gettimeoday should return zero and should not reserve any value for error, this function returns zero. Here is the program, I?ve given definition struct timezeone and for others I didn?t give as all other data types definitions are available in windows include files itself.

    Code Block
    #include < time.h >
    #include <windows.h> //I've ommited this line.
    #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
      #define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
    #else
      #define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
    #endif
     
    struct timezone 
    {
      int  tz_minuteswest; /* minutes W of Greenwich */
      int  tz_dsttime;     /* type of dst correction */
    };
     
    int gettimeofday(struct timeval *tv, struct timezone *tz)
    {
      FILETIME ft;
      unsigned __int64 tmpres = 0;
      static int tzflag;
     
      if (NULL != tv)
      {
        GetSystemTimeAsFileTime(&ft);
     
        tmpres |= ft.dwHighDateTime;
        tmpres <<= 32;
        tmpres |= ft.dwLowDateTime;
     
        /*converting file time to unix epoch*/
        tmpres -= DELTA_EPOCH_IN_MICROSECS; 
        tmpres /= 10;  /*convert into microseconds*/
        tv->tv_sec = (long)(tmpres / 1000000UL);
        tv->tv_usec = (long)(tmpres % 1000000UL);
      }
     
      if (NULL != tz)
      {
        if (!tzflag)
        {
          _tzset();
          tzflag++;
        }
        tz->tz_minuteswest = _timezone / 60;
        tz->tz_dsttime = _daylight;
      }
     
      return 0;
    }
     
    Usage
    Code Block
    gettimeofday(&now, NULL); 
    gettimeofday(&now, &tzone); 

     

     

     Good Luck
    Wednesday, December 26, 2007 1:33 PM

All replies

  • Hi~

    Below page show how to using gettimeofday on windows.

    http://www.openasthra.com/c-tidbits/gettimeofday-function-for-windows/

     

    Good Luck.

    Thanks for that page.

     

    Wednesday, December 26, 2007 12:25 PM
  • i've tried many times, but i'm so sorry that i just can't open that page...would you please copy it here or just save tha page and send it to me ? my email address is: echo1004@163.com

     

    thanks so much.
    Wednesday, December 26, 2007 1:15 PM
  • Hi~

    I'm really sorry that I copied from below page.

    http://www.openasthra.com/c-tidbits/gettimeofday-function-for-windows/

     

    here is source code.

    http://www.openasthra.com/wp-content/uploads/gettimeofday.c

     

     here is total contents.

    The gettimeofday() function obtains the current time, expressed as seconds and microseconds since the Epoch, and store it in the timeval structure pointed to by tv. As posix says gettimeoday should return zero and should not reserve any value for error, this function returns zero. Here is the program, I?ve given definition struct timezeone and for others I didn?t give as all other data types definitions are available in windows include files itself.

    Code Block
    #include < time.h >
    #include <windows.h> //I've ommited this line.
    #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
      #define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
    #else
      #define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
    #endif
     
    struct timezone 
    {
      int  tz_minuteswest; /* minutes W of Greenwich */
      int  tz_dsttime;     /* type of dst correction */
    };
     
    int gettimeofday(struct timeval *tv, struct timezone *tz)
    {
      FILETIME ft;
      unsigned __int64 tmpres = 0;
      static int tzflag;
     
      if (NULL != tv)
      {
        GetSystemTimeAsFileTime(&ft);
     
        tmpres |= ft.dwHighDateTime;
        tmpres <<= 32;
        tmpres |= ft.dwLowDateTime;
     
        /*converting file time to unix epoch*/
        tmpres -= DELTA_EPOCH_IN_MICROSECS; 
        tmpres /= 10;  /*convert into microseconds*/
        tv->tv_sec = (long)(tmpres / 1000000UL);
        tv->tv_usec = (long)(tmpres % 1000000UL);
      }
     
      if (NULL != tz)
      {
        if (!tzflag)
        {
          _tzset();
          tzflag++;
        }
        tz->tz_minuteswest = _timezone / 60;
        tz->tz_dsttime = _daylight;
      }
     
      return 0;
    }
     
    Usage
    Code Block
    gettimeofday(&now, NULL); 
    gettimeofday(&now, &tzone); 

     

     

     Good Luck
    Wednesday, December 26, 2007 1:33 PM
  •  

    scor7910 ,thanks for your patience, i've now solved this problem.

    God bless you~~~

    Thursday, December 27, 2007 8:07 AM
  • i've tried many times, but i'm so sorry that i just can't open that page...would you please copy it here or just save tha page and send it to me ? my email address is: echo1004@163.com

     

    thanks so much.

    This page has been migrated to http://www.cpp-programming.net/c-tidbits/gettimeofday-function-for-windows/
    That could be the reason you are not able to find..
    • Proposed as answer by techgeeknet Monday, February 18, 2013 11:12 AM
    Friday, October 30, 2009 1:06 AM
  • I found this thread, which is very helpful, but I'm having problems implementing it.  When I compile I get a "conversion from 'timeval*' to non-scalar type 'timeval requested' error.  Any ideas?
    Tuesday, March 30, 2010 10:24 PM
  • Show your code.

     

    Tuesday, March 30, 2010 10:37 PM
  • This is good, except that the epoch should be subtracted after the conversion of tmpres to microseconds (from 100 nanoseconds). 

     

     /*converting file time to unix epoch*/
     tmpres /= 10; /*convert into microseconds*/
    
     tmpres -= DELTA_EPOCH_IN_MICROSECS;

    Making this change will actually work, otherwise tv_sec will overflow and you'll have a nonsense value. 

    NOTE: The resolution of GetSystemTimeAsFileTime() is only 15625 microseconds (if you make successive calls to gettimeofday() until it changes value). It's not much better than calling GetLocalTime() which is accurate to between 15000 and 31000 microseconds. This differs significantly from the unix implementations which are accurate close to the microsecond. 

    • Edited by Mergatroid Wednesday, May 26, 2010 10:23 PM accuracy note
    Wednesday, May 26, 2010 7:58 PM
  • its in refcode.net

    Monday, February 18, 2013 11:16 AM
  • cpp-programming.net can't be accessed, 

    http://www.refcode.net/2013/02/gettimeofday-for-windows_13.html

    Tuesday, February 19, 2013 2:05 AM
  • Thanks Man .... i just add it in new header file and it worked :) :)
    Monday, June 16, 2014 7:41 PM