gettimeofday.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* source:
  2. * cygwin-1.5.24-2-src/cygwin-1.5.24-2/winsup/mingw/mingwex/gettimeofday.c */
  3. /*
  4. * gettimeofday
  5. * Implementation according to:
  6. * The Open Group Base Specifications Issue 6
  7. * IEEE Std 1003.1, 2004 Edition
  8. */
  9. /*
  10. * THIS SOFTWARE IS NOT COPYRIGHTED
  11. *
  12. * This source code is offered for use in the public domain. You may
  13. * use, modify or distribute it freely.
  14. *
  15. * This code is distributed in the hope that it will be useful but
  16. * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
  17. * DISCLAIMED. This includes but is not limited to warranties of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  19. *
  20. * Contributed by:
  21. * Danny Smith <dannysmith@users.sourceforge.net>
  22. */
  23. #include <sys/time.h>
  24. #ifdef __MINGW32__
  25. #define WIN32_LEAN_AND_MEAN
  26. #include <windows.h>
  27. /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
  28. #define _W32_FT_OFFSET (116444736000000000ULL)
  29. int __cdecl gettimeofday(struct timeval* __restrict__ tp,
  30. void* __restrict__ tzp __attribute__((unused)))
  31. {
  32. union {
  33. unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
  34. FILETIME ft;
  35. } _now;
  36. if (tp) {
  37. GetSystemTimeAsFileTime(&_now.ft);
  38. tp->tv_usec = (long)((_now.ns100 / 10ULL) % 1000000ULL);
  39. tp->tv_sec = (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL);
  40. }
  41. /* Always return 0 as per Open Group Base Specifications Issue 6.
  42. Do not set errno on error. */
  43. return 0;
  44. }
  45. #endif // __MINGW32__