testing_clock.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. #ifdef _WIN32
  18. # include <windows.h>
  19. #endif
  20. #include "testing_clock.h"
  21. UA_DateTime testingClock = 0;
  22. UA_DateTime UA_DateTime_now(void) {
  23. return testingClock;
  24. }
  25. UA_DateTime UA_DateTime_nowMonotonic(void) {
  26. return testingClock;
  27. }
  28. UA_DateTime UA_DateTime_localTimeUtcOffset(void) {
  29. return 0;
  30. }
  31. void
  32. UA_fakeSleep(UA_UInt32 duration) {
  33. testingClock += duration * UA_DATETIME_MSEC;
  34. }
  35. /* 1 millisecond = 1,000,000 Nanoseconds */
  36. #define NANO_SECOND_MULTIPLIER 1000000
  37. void
  38. UA_realSleep(UA_UInt32 duration) {
  39. #ifdef _WIN32
  40. Sleep(duration);
  41. #else
  42. UA_UInt32 sec = duration / 1000;
  43. UA_UInt32 ns = (duration % 1000) * NANO_SECOND_MULTIPLIER;
  44. struct timespec sleepValue;
  45. sleepValue.tv_sec = sec;
  46. sleepValue.tv_nsec = ns;
  47. nanosleep(&sleepValue, NULL);
  48. #endif
  49. }