ua_clock.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #if !defined(_XOPEN_SOURCE) && !defined(_WRS_KERNEL)
  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_DATETIME_SEC) + (tv.tv_usec * UA_DATETIME_USEC) + UA_DATETIME_UNIX_EPOCH;
  53. #endif
  54. }
  55. /* Credit to https://stackoverflow.com/questions/13804095/get-the-time-zone-gmt-offset-in-c */
  56. UA_Int64 UA_DateTime_localTimeUtcOffset(void) {
  57. time_t gmt, rawtime = time(NULL);
  58. #ifdef _WIN32
  59. struct tm ptm;
  60. gmtime_s(&ptm, &rawtime);
  61. // Request that mktime() looksup dst in timezone database
  62. ptm.tm_isdst = -1;
  63. gmt = mktime(&ptm);
  64. #else
  65. struct tm *ptm;
  66. struct tm gbuf;
  67. ptm = gmtime_r(&rawtime, &gbuf);
  68. // Request that mktime() looksup dst in timezone database
  69. ptm->tm_isdst = -1;
  70. gmt = mktime(ptm);
  71. #endif
  72. return (UA_Int64) (difftime(rawtime, gmt) * UA_DATETIME_SEC);
  73. }
  74. UA_DateTime UA_DateTime_nowMonotonic(void) {
  75. #if defined(_WIN32)
  76. LARGE_INTEGER freq, ticks;
  77. QueryPerformanceFrequency(&freq);
  78. QueryPerformanceCounter(&ticks);
  79. UA_Double ticks2dt = UA_DATETIME_SEC / (UA_Double)freq.QuadPart;
  80. return (UA_DateTime)(ticks.QuadPart * ticks2dt);
  81. #elif defined(__APPLE__) || defined(__MACH__)
  82. /* OS X does not have clock_gettime, use clock_get_time */
  83. clock_serv_t cclock;
  84. mach_timespec_t mts;
  85. host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
  86. clock_get_time(cclock, &mts);
  87. mach_port_deallocate(mach_task_self(), cclock);
  88. return (mts.tv_sec * UA_DATETIME_SEC) + (mts.tv_nsec / 100);
  89. #elif !defined(CLOCK_MONOTONIC_RAW)
  90. struct timespec ts;
  91. clock_gettime(CLOCK_MONOTONIC, &ts);
  92. return (ts.tv_sec * UA_DATETIME_SEC) + (ts.tv_nsec / 100);
  93. #else
  94. struct timespec ts;
  95. clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
  96. return (ts.tv_sec * UA_DATETIME_SEC) + (ts.tv_nsec / 100);
  97. #endif
  98. }