ua_clock.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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_VXWORKS
  9. #ifndef _DEFAULT_SOURCE
  10. # define _DEFAULT_SOURCE
  11. #endif
  12. /* On older systems we need to define _BSD_SOURCE.
  13. * _DEFAULT_SOURCE is an alias for that. */
  14. #ifndef _BSD_SOURCE
  15. # define _BSD_SOURCE
  16. #endif
  17. #include <time.h>
  18. #include <sys/time.h>
  19. #include <open62541/types.h>
  20. UA_DateTime UA_DateTime_now(void){
  21. struct timeval tv;
  22. gettimeofday(&tv, NULL);
  23. return (tv.tv_sec * UA_DATETIME_SEC) + (tv.tv_usec * UA_DATETIME_USEC) + UA_DATETIME_UNIX_EPOCH;
  24. }
  25. /* Credit to https://stackoverflow.com/questions/13804095/get-the-time-zone-gmt-offset-in-c */
  26. UA_Int64 UA_DateTime_localTimeUtcOffset(void){
  27. time_t gmt, rawtime = time(NULL);
  28. struct tm *ptm;
  29. struct tm gbuf;
  30. ptm = gmtime_r(&rawtime, &gbuf);
  31. // Request that mktime() looksup dst in timezone database
  32. ptm->tm_isdst = -1;
  33. gmt = mktime(ptm);
  34. return (UA_Int64) (difftime(rawtime, gmt) * UA_DATETIME_SEC);
  35. }
  36. UA_DateTime UA_DateTime_nowMonotonic(void) {
  37. struct timespec ts;
  38. #if !defined(CLOCK_MONOTONIC_RAW)
  39. clock_gettime(CLOCK_MONOTONIC, &ts);
  40. #else
  41. clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
  42. #endif
  43. return (ts.tv_sec * UA_DATETIME_SEC) + (ts.tv_nsec / 100);
  44. }
  45. #endif /* UA_ARCHITECTURE_VXWORKS */