testing_clock.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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_now(void) {
  23. return testingClock;
  24. }
  25. UA_DateTime UA_DateTime_nowLocalTime(void) {
  26. return testingClock;
  27. }
  28. UA_DateTime UA_DateTime_nowMonotonic(void) {
  29. return testingClock;
  30. }
  31. void
  32. UA_fakeSleep(UA_UInt32 duration) {
  33. testingClock += duration * UA_MSEC_TO_DATETIME;
  34. }
  35. /* 1 millisecond = 1,000,000 Nanoseconds */
  36. #define NANO_SECOND_MULTIPLIER 1000000
  37. #ifdef _WIN32
  38. void
  39. UA_realSleep(UA_UInt32 duration) {
  40. Sleep(duration);
  41. }
  42. #else
  43. void
  44. UA_realSleep(UA_UInt32 duration) {
  45. UA_UInt32 sec = duration / 1000;
  46. UA_UInt32 ns = (duration % 1000) * NANO_SECOND_MULTIPLIER;
  47. struct timespec sleepValue;
  48. sleepValue.tv_sec = sec;
  49. sleepValue.tv_nsec = ns;
  50. nanosleep(&sleepValue, NULL);
  51. }
  52. #endif