gettimeofday function for windows

Google Search Results

You arrived here after searching for the following phrases:

Click a phrase to jump to the first occurrence, or return to the search results.

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.

#include < time.h >

#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 /= 10;  /*convert into microseconds*/
    tmpres -= DELTA_EPOCH_IN_MICROSECS;
    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:

struct timeval now; struct timezone tzone;

gettimeofday(&now, NULL);

gettimeofday(&now, &tzone);

download [gettimeofday.c] program..

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Add to favorites
  • LinkedIn
  • Propeller
  • Reddit
  • StumbleUpon
  • Technorati
  • Twitter
  • Yahoo! Bookmarks

Related posts:

  1. Displaying execution time of a program
  2. Posix Directory Browsing API for Windows
  3. Why sizeof is an operator, not a function?
  4. How can I call a C function f(int,char,float) from my C++ code?
  5. dlwrapper: POSIX Wrapper for Windows Dynamic Library Loading Calls

12 Responses to “gettimeofday function for windows”

  1. [...] OpenAsthra presents class=”searchterm1″>gettimeofday function for windows posted at OpenAsthra, saying, “class=”searchterm1″>gettimeofday function for windows” [...]

  2. [...] refer another post for class=”searchterm1″>gettimeofday [...]

  3. Anuj Verma says:

    very useful post, saved me a lot of time, I’d searched the net could not find a working program, at last found here, and without tweaking a bit it worked straight away, thanks

  4. koos dering says:

    one would think conversion to usecs goes before adjusting by DELTA_EPOCH_IN_MICROSECS (really in microsecs- 8 zeroes 6 from micro, 2 from 60*60*24 -whole day)

  5. ponnada says:

    Thanks for pointing this out, I’d corrected the code

  6. schmendrick says:

    great article!

    thanks, this saved me some time!

  7. Hi dear ,
    Some really helpful information here. I Like of your website design . Did you make this templete yourself or did you get it from any templetes websites? Looks pretty cool for me . I use Joomla but wants something better.

  8. gagan says:

    Hey, thanks a lot for the information post . I have wondering if you make money from pay per post too? I heard that is not bad for a good page rank blog.

  9. fanny says:

    Hey , I finally decided to write a comment on your blog . I just wanted to say good job . I really enjoy reading your posts.

  10. Rich says:

    Hey, many thanks, this was exactly what I needed at 3am to get my unix
    app working under Win32. Many MANY thanks!

    Rich

  11. Omf says:

    Very, very useful.

  12. Abe says:

    Thnky you, for an excellent post

Leave a Reply