ua_clock.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 2018 (c) Jose Cabral, fortiss GmbH
  5. */
  6. #ifdef UA_ARCHITECTURE_ECOS
  7. #include <open62541/types.h>
  8. UA_DateTime UA_DateTime_now(void) {
  9. return UA_DateTime_nowMonotonic();
  10. }
  11. /* Credit to https://stackoverflow.com/questions/13804095/get-the-time-zone-gmt-offset-in-c */
  12. UA_Int64 UA_DateTime_localTimeUtcOffset(void){
  13. time_t gmt, rawtime = time(NULL);
  14. struct tm *ptm;
  15. struct tm gbuf;
  16. ptm = gmtime_r(&rawtime, &gbuf);
  17. // Request that mktime() looksup dst in timezone database
  18. ptm->tm_isdst = -1;
  19. gmt = mktime(ptm);
  20. return (UA_Int64) (difftime(rawtime, gmt) * UA_DATETIME_SEC);
  21. }
  22. UA_DateTime UA_DateTime_nowMonotonic(void) {
  23. cyg_tick_count_t TaskTime = cyg_current_time();
  24. UA_DateTimeStruct UATime;
  25. UATime.milliSec = (UA_UInt16) TaskTime;
  26. struct timespec ts;
  27. ts.tv_sec = UATime.milliSec / 1000;
  28. ts.tv_nsec = (UATime.milliSec % 1000) * 1000000;
  29. return (ts.tv_sec * UA_DATETIME_SEC) + (ts.tv_nsec / 100) + UA_DATETIME_UNIX_EPOCH;
  30. }
  31. #endif /* UA_ARCHITECTURE_ECOS */