ua_clock.c 1.1 KB

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