ua_clock.c 1.7 KB

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