ua_clock.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. *
  4. * Copyright 2016-2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  5. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  6. * Copyright 2017 (c) Thomas Stalder, Blue Time Concept SA
  7. */
  8. /* Enable POSIX features */
  9. #if !defined(_XOPEN_SOURCE) && !defined(_WRS_KERNEL)
  10. # define _XOPEN_SOURCE 600
  11. #endif
  12. #ifndef _DEFAULT_SOURCE
  13. # define _DEFAULT_SOURCE
  14. #endif
  15. /* On older systems we need to define _BSD_SOURCE.
  16. * _DEFAULT_SOURCE is an alias for that. */
  17. #ifndef _BSD_SOURCE
  18. # define _BSD_SOURCE
  19. #endif
  20. #include <time.h>
  21. #ifdef _WIN32
  22. /* Backup definition of SLIST_ENTRY on mingw winnt.h */
  23. # ifdef SLIST_ENTRY
  24. # pragma push_macro("SLIST_ENTRY")
  25. # undef SLIST_ENTRY
  26. # define POP_SLIST_ENTRY
  27. # endif
  28. # include <windows.h>
  29. /* restore definition */
  30. # ifdef POP_SLIST_ENTRY
  31. # undef SLIST_ENTRY
  32. # undef POP_SLIST_ENTRY
  33. # pragma pop_macro("SLIST_ENTRY")
  34. # endif
  35. #else
  36. # include <sys/time.h>
  37. #endif
  38. #if defined(__APPLE__) || defined(__MACH__)
  39. # include <mach/clock.h>
  40. # include <mach/mach.h>
  41. #endif
  42. #include "ua_types.h"
  43. #if defined(UA_FREERTOS)
  44. #include <task.h>
  45. #endif
  46. UA_DateTime UA_DateTime_now(void) {
  47. #if defined(_WIN32)
  48. /* Windows filetime has the same definition as UA_DateTime */
  49. FILETIME ft;
  50. SYSTEMTIME st;
  51. GetSystemTime(&st);
  52. SystemTimeToFileTime(&st, &ft);
  53. ULARGE_INTEGER ul;
  54. ul.LowPart = ft.dwLowDateTime;
  55. ul.HighPart = ft.dwHighDateTime;
  56. return (UA_DateTime)ul.QuadPart;
  57. #else
  58. struct timeval tv;
  59. gettimeofday(&tv, NULL);
  60. return (tv.tv_sec * UA_DATETIME_SEC) + (tv.tv_usec * UA_DATETIME_USEC) + UA_DATETIME_UNIX_EPOCH;
  61. #endif
  62. }
  63. /* Credit to https://stackoverflow.com/questions/13804095/get-the-time-zone-gmt-offset-in-c */
  64. UA_Int64 UA_DateTime_localTimeUtcOffset(void) {
  65. time_t gmt, rawtime = time(NULL);
  66. #ifdef _WIN32
  67. struct tm ptm;
  68. gmtime_s(&ptm, &rawtime);
  69. // Request that mktime() looksup dst in timezone database
  70. ptm.tm_isdst = -1;
  71. gmt = mktime(&ptm);
  72. #else
  73. struct tm *ptm;
  74. struct tm gbuf;
  75. ptm = gmtime_r(&rawtime, &gbuf);
  76. // Request that mktime() looksup dst in timezone database
  77. ptm->tm_isdst = -1;
  78. gmt = mktime(ptm);
  79. #endif
  80. return (UA_Int64) (difftime(rawtime, gmt) * UA_DATETIME_SEC);
  81. }
  82. UA_DateTime UA_DateTime_nowMonotonic(void) {
  83. #if defined(_WIN32)
  84. LARGE_INTEGER freq, ticks;
  85. QueryPerformanceFrequency(&freq);
  86. QueryPerformanceCounter(&ticks);
  87. UA_Double ticks2dt = UA_DATETIME_SEC / (UA_Double)freq.QuadPart;
  88. return (UA_DateTime)(ticks.QuadPart * ticks2dt);
  89. #elif defined(__APPLE__) || defined(__MACH__)
  90. /* OS X does not have clock_gettime, use clock_get_time */
  91. clock_serv_t cclock;
  92. mach_timespec_t mts;
  93. host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
  94. clock_get_time(cclock, &mts);
  95. mach_port_deallocate(mach_task_self(), cclock);
  96. return (mts.tv_sec * UA_DATETIME_SEC) + (mts.tv_nsec / 100);
  97. #elif !defined(CLOCK_MONOTONIC_RAW)
  98. # if defined(UA_FREERTOS)
  99. portTickType TaskTime = xTaskGetTickCount();
  100. UA_DateTimeStruct UATime;
  101. UATime.milliSec = (UA_UInt16) TaskTime;
  102. struct timespec ts;
  103. ts.tv_sec = UATime.milliSec/1000;
  104. ts.tv_nsec = (UATime.milliSec % 1000)* 1000000;
  105. return (ts.tv_sec * UA_DATETIME_SEC) + (ts.tv_nsec / 100);
  106. # else
  107. struct timespec ts;
  108. clock_gettime(CLOCK_MONOTONIC, &ts);
  109. return (ts.tv_sec * UA_DATETIME_SEC) + (ts.tv_nsec / 100);
  110. # endif
  111. #else
  112. struct timespec ts;
  113. clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
  114. return (ts.tv_sec * UA_DATETIME_SEC) + (ts.tv_nsec / 100);
  115. #endif
  116. }