testing_clock.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. #include "testing_clock.h"
  5. #include <time.h>
  6. UA_DateTime testingClock = 0;
  7. UA_DateTime UA_DateTime_now(void) {
  8. return testingClock;
  9. }
  10. UA_DateTime UA_DateTime_nowMonotonic(void) {
  11. return testingClock;
  12. }
  13. UA_DateTime UA_DateTime_localTimeUtcOffset(void) {
  14. return 0;
  15. }
  16. void
  17. UA_fakeSleep(UA_UInt32 duration) {
  18. testingClock += duration * UA_DATETIME_MSEC;
  19. }
  20. /* 1 millisecond = 1,000,000 Nanoseconds */
  21. #define NANO_SECOND_MULTIPLIER 1000000
  22. void
  23. UA_realSleep(UA_UInt32 duration) {
  24. #ifdef _WIN32
  25. Sleep(duration);
  26. #else
  27. UA_UInt32 sec = duration / 1000;
  28. UA_UInt32 ns = (duration % 1000) * NANO_SECOND_MULTIPLIER;
  29. struct timespec sleepValue;
  30. sleepValue.tv_sec = sec;
  31. sleepValue.tv_nsec = ns;
  32. nanosleep(&sleepValue, NULL);
  33. #endif
  34. }
  35. void
  36. UA_comboSleep(UA_UInt32 duration) {
  37. UA_fakeSleep(duration);
  38. UA_realSleep(duration);
  39. }