testing_clock.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /* Enable POSIX features */
  5. #if !defined(_XOPEN_SOURCE) && !defined(_WRS_KERNEL)
  6. # define _XOPEN_SOURCE 600
  7. #endif
  8. #ifndef _DEFAULT_SOURCE
  9. # define _DEFAULT_SOURCE
  10. #endif
  11. /* On older systems we need to define _BSD_SOURCE.
  12. * _DEFAULT_SOURCE is an alias for that. */
  13. #ifndef _BSD_SOURCE
  14. # define _BSD_SOURCE
  15. #endif
  16. #include <time.h>
  17. #include "testing_clock.h"
  18. #ifdef _WIN32
  19. #include <windows.h> /* WinAPI */
  20. #endif
  21. UA_DateTime testingClock = 0;
  22. UA_DateTime UA_DateTime_diffLocalTimeUTC(void){
  23. return 0;
  24. }
  25. UA_DateTime UA_DateTime_nowUtcTime(void) {
  26. return testingClock;
  27. }
  28. UA_DateTime UA_DateTime_nowLocalTime(void) {
  29. return testingClock;
  30. }
  31. UA_DateTime UA_DateTime_nowMonotonic(void) {
  32. return testingClock;
  33. }
  34. void
  35. UA_fakeSleep(UA_UInt32 duration) {
  36. testingClock += duration * UA_MSEC_TO_DATETIME;
  37. }
  38. /* 1 millisecond = 1,000,000 Nanoseconds */
  39. #define NANO_SECOND_MULTIPLIER 1000000
  40. #ifdef _WIN32
  41. void
  42. UA_realSleep(UA_UInt32 duration) {
  43. Sleep(duration);
  44. }
  45. #else
  46. void
  47. UA_realSleep(UA_UInt32 duration) {
  48. UA_UInt32 sec = duration / 1000;
  49. UA_UInt32 ns = (duration % 1000) * NANO_SECOND_MULTIPLIER;
  50. struct timespec sleepValue;
  51. sleepValue.tv_sec = sec;
  52. sleepValue.tv_nsec = ns;
  53. nanosleep(&sleepValue, NULL);
  54. }
  55. #endif