gettimeofday.c 756 B

123456789101112131415161718192021222324252627282930313233343536
  1. #include <time.h>
  2. #include <windows.h> //I've omitted context line
  3. #include "gettimeofday.h"
  4. int gettimeofday(struct timeval *tv, struct timezone *tz)
  5. {
  6. FILETIME ft;
  7. unsigned __int64 tmpres = 0;
  8. static int tzflag;
  9. if (NULL != tv) {
  10. GetSystemTimeAsFileTime(&ft);
  11. tmpres |= ft.dwHighDateTime;
  12. tmpres <<= 32;
  13. tmpres |= ft.dwLowDateTime;
  14. /*converting file time to unix epoch*/
  15. tmpres /= 10; /*convert into microseconds*/
  16. tmpres -= DELTA_EPOCH_IN_MICROSECS;
  17. tv->tv_sec = (long)(tmpres / 1000000UL);
  18. tv->tv_usec = (long)(tmpres % 1000000UL);
  19. }
  20. if (NULL != tz) {
  21. if (!tzflag) {
  22. _tzset();
  23. tzflag++;
  24. }
  25. tz->tz_minuteswest = _timezone / 60;
  26. tz->tz_dsttime = _daylight;
  27. }
  28. return 0;
  29. }