ua_clock.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #if defined(UA_FREERTOS)
  39. #include <task.h>
  40. #endif
  41. UA_DateTime UA_DateTime_now(void) {
  42. #if defined(_WIN32)
  43. /* Windows filetime has the same definition as UA_DateTime */
  44. FILETIME ft;
  45. SYSTEMTIME st;
  46. GetSystemTime(&st);
  47. SystemTimeToFileTime(&st, &ft);
  48. ULARGE_INTEGER ul;
  49. ul.LowPart = ft.dwLowDateTime;
  50. ul.HighPart = ft.dwHighDateTime;
  51. return (UA_DateTime)ul.QuadPart;
  52. #else
  53. struct timeval tv;
  54. gettimeofday(&tv, NULL);
  55. return (tv.tv_sec * UA_DATETIME_SEC) + (tv.tv_usec * UA_DATETIME_USEC) + UA_DATETIME_UNIX_EPOCH;
  56. #endif
  57. }
  58. /* Credit to https://stackoverflow.com/questions/13804095/get-the-time-zone-gmt-offset-in-c */
  59. UA_Int64 UA_DateTime_localTimeUtcOffset(void) {
  60. time_t gmt, rawtime = time(NULL);
  61. #ifdef _WIN32
  62. struct tm ptm;
  63. gmtime_s(&ptm, &rawtime);
  64. // Request that mktime() looksup dst in timezone database
  65. ptm.tm_isdst = -1;
  66. gmt = mktime(&ptm);
  67. #else
  68. struct tm *ptm;
  69. struct tm gbuf;
  70. ptm = gmtime_r(&rawtime, &gbuf);
  71. // Request that mktime() looksup dst in timezone database
  72. ptm->tm_isdst = -1;
  73. gmt = mktime(ptm);
  74. #endif
  75. return (UA_Int64) (difftime(rawtime, gmt) * UA_DATETIME_SEC);
  76. }
  77. UA_DateTime UA_DateTime_nowMonotonic(void) {
  78. #if defined(_WIN32)
  79. LARGE_INTEGER freq, ticks;
  80. QueryPerformanceFrequency(&freq);
  81. QueryPerformanceCounter(&ticks);
  82. UA_Double ticks2dt = UA_DATETIME_SEC / (UA_Double)freq.QuadPart;
  83. return (UA_DateTime)(ticks.QuadPart * ticks2dt);
  84. #elif defined(__APPLE__) || defined(__MACH__)
  85. /* OS X does not have clock_gettime, use clock_get_time */
  86. clock_serv_t cclock;
  87. mach_timespec_t mts;
  88. host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
  89. clock_get_time(cclock, &mts);
  90. mach_port_deallocate(mach_task_self(), cclock);
  91. return (mts.tv_sec * UA_DATETIME_SEC) + (mts.tv_nsec / 100);
  92. #elif !defined(CLOCK_MONOTONIC_RAW)
  93. # if defined(UA_FREERTOS)
  94. portTickType TaskTime = xTaskGetTickCount();
  95. UA_DateTimeStruct UATime;
  96. UATime.milliSec = (UA_UInt16) TaskTime;
  97. struct timespec ts;
  98. ts.tv_sec = UATime.milliSec/1000;
  99. ts.tv_nsec = (UATime.milliSec % 1000)* 1000000;
  100. return (ts.tv_sec * UA_DATETIME_SEC) + (ts.tv_nsec / 100);
  101. # else
  102. struct timespec ts;
  103. clock_gettime(CLOCK_MONOTONIC, &ts);
  104. return (ts.tv_sec * UA_DATETIME_SEC) + (ts.tv_nsec / 100);
  105. # endif
  106. #else
  107. struct timespec ts;
  108. clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
  109. return (ts.tv_sec * UA_DATETIME_SEC) + (ts.tv_nsec / 100);
  110. #endif
  111. }