ua_clock.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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) Julius Pfrommer, Fraunhofer IOSB
  5. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  6. * Copyright 2017 (c) Thomas Stalder
  7. */
  8. #ifdef UA_ARCHITECTURE_WIN32
  9. #ifndef _BSD_SOURCE
  10. # define _BSD_SOURCE
  11. #endif
  12. #include <open62541/types.h>
  13. #include <time.h>
  14. /* Backup definition of SLIST_ENTRY on mingw winnt.h */
  15. # ifdef SLIST_ENTRY
  16. # pragma push_macro("SLIST_ENTRY")
  17. # undef SLIST_ENTRY
  18. # define POP_SLIST_ENTRY
  19. # endif
  20. # include <windows.h>
  21. /* restore definition */
  22. # ifdef POP_SLIST_ENTRY
  23. # undef SLIST_ENTRY
  24. # undef POP_SLIST_ENTRY
  25. # pragma pop_macro("SLIST_ENTRY")
  26. # endif
  27. UA_DateTime UA_DateTime_now(void) {
  28. /* Windows filetime has the same definition as UA_DateTime */
  29. FILETIME ft;
  30. SYSTEMTIME st;
  31. GetSystemTime(&st);
  32. SystemTimeToFileTime(&st, &ft);
  33. ULARGE_INTEGER ul;
  34. ul.LowPart = ft.dwLowDateTime;
  35. ul.HighPart = ft.dwHighDateTime;
  36. return (UA_DateTime)ul.QuadPart;
  37. }
  38. /* Credit to https://stackoverflow.com/questions/13804095/get-the-time-zone-gmt-offset-in-c */
  39. UA_Int64 UA_DateTime_localTimeUtcOffset(void) {
  40. time_t gmt, rawtime = time(NULL);
  41. struct tm ptm;
  42. gmtime_s(&ptm, &rawtime);
  43. // Request that mktime() looksup dst in timezone database
  44. ptm.tm_isdst = -1;
  45. gmt = mktime(&ptm);
  46. return (UA_Int64) (difftime(rawtime, gmt) * UA_DATETIME_SEC);
  47. }
  48. UA_DateTime UA_DateTime_nowMonotonic(void) {
  49. LARGE_INTEGER freq, ticks;
  50. QueryPerformanceFrequency(&freq);
  51. QueryPerformanceCounter(&ticks);
  52. UA_Double ticks2dt = UA_DATETIME_SEC / (UA_Double)freq.QuadPart;
  53. return (UA_DateTime)(ticks.QuadPart * ticks2dt);
  54. }
  55. #endif /* UA_ARCHITECTURE_WIN32 */