ua_clock.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. /* Enable POSIX features */
  4. #ifndef _XOPEN_SOURCE
  5. # define _XOPEN_SOURCE 600
  6. #endif
  7. #ifndef _DEFAULT_SOURCE
  8. # define _DEFAULT_SOURCE
  9. #endif
  10. /* On older systems we need to define _BSD_SOURCE.
  11. * _DEFAULT_SOURCE is an alias for that. */
  12. #ifndef _BSD_SOURCE
  13. # define _BSD_SOURCE
  14. #endif
  15. #include <time.h>
  16. #ifdef _WIN32
  17. /* Backup definition of SLIST_ENTRY on mingw winnt.h */
  18. # ifdef SLIST_ENTRY
  19. # pragma push_macro("SLIST_ENTRY")
  20. # undef SLIST_ENTRY
  21. # define POP_SLIST_ENTRY
  22. # endif
  23. # include <windows.h>
  24. /* restore definition */
  25. # ifdef POP_SLIST_ENTRY
  26. # undef SLIST_ENTRY
  27. # undef POP_SLIST_ENTRY
  28. # pragma pop_macro("SLIST_ENTRY")
  29. # endif
  30. #else
  31. # include <sys/time.h>
  32. #endif
  33. #if defined(__APPLE__) || defined(__MACH__)
  34. # include <mach/clock.h>
  35. # include <mach/mach.h>
  36. #endif
  37. #include "ua_types.h"
  38. UA_DateTime UA_DateTime_now(void) {
  39. #if defined(_WIN32)
  40. /* Windows filetime has the same definition as UA_DateTime */
  41. FILETIME ft;
  42. SYSTEMTIME st;
  43. GetSystemTime(&st);
  44. SystemTimeToFileTime(&st, &ft);
  45. ULARGE_INTEGER ul;
  46. ul.LowPart = ft.dwLowDateTime;
  47. ul.HighPart = ft.dwHighDateTime;
  48. return (UA_DateTime)ul.QuadPart;
  49. #else
  50. struct timeval tv;
  51. gettimeofday(&tv, NULL);
  52. return (tv.tv_sec * UA_SEC_TO_DATETIME) + (tv.tv_usec * UA_USEC_TO_DATETIME) + UA_DATETIME_UNIX_EPOCH;
  53. #endif
  54. }
  55. UA_DateTime UA_DateTime_nowMonotonic(void) {
  56. #if defined(_WIN32)
  57. LARGE_INTEGER freq, ticks;
  58. QueryPerformanceFrequency(&freq);
  59. QueryPerformanceCounter(&ticks);
  60. UA_Double ticks2dt = UA_SEC_TO_DATETIME / (UA_Double)freq.QuadPart;
  61. return (UA_DateTime)(ticks.QuadPart * ticks2dt);
  62. #elif defined(__APPLE__) || defined(__MACH__)
  63. /* OS X does not have clock_gettime, use clock_get_time */
  64. clock_serv_t cclock;
  65. mach_timespec_t mts;
  66. host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
  67. clock_get_time(cclock, &mts);
  68. mach_port_deallocate(mach_task_self(), cclock);
  69. return (mts.tv_sec * UA_SEC_TO_DATETIME) + (mts.tv_nsec / 100);
  70. #elif !defined(CLOCK_MONOTONIC_RAW)
  71. struct timespec ts;
  72. clock_gettime(CLOCK_MONOTONIC, &ts);
  73. return (ts.tv_sec * UA_SEC_TO_DATETIME) + (ts.tv_nsec / 100);
  74. #else
  75. struct timespec ts;
  76. clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
  77. return (ts.tv_sec * UA_SEC_TO_DATETIME) + (ts.tv_nsec / 100);
  78. #endif
  79. }