opcua_time.c 943 B

1234567891011121314151617181920212223242526272829303132
  1. /*
  2. * opcua_time.c
  3. *
  4. * Created on: Feb 5, 2014
  5. * Author: opcua
  6. *
  7. * code inspired by
  8. * http://stackoverflow.com/questions/3585583/convert-unix-linux-time-to-windows-filetime
  9. */
  10. #include "opcua_builtInDatatypes.h"
  11. #include "opcua_advancedDatatypes.h"
  12. #include <sys/time.h>
  13. // Number of seconds from 1 Jan. 1601 00:00 to 1 Jan 1970 00:00 UTC
  14. #define FILETIME_UNIXTIME_BIAS_SEC 11644473600LL
  15. // Factors
  16. #define HUNDRED_NANOSEC_PER_USEC 10LL
  17. #define HUNDRED_NANOSEC_PER_SEC (HUNDRED_NANOSEC_PER_USEC * 1000000LL)
  18. // IEC 62541-6 §5.2.2.5 A DateTime value shall be encoded as a 64-bit signed integer
  19. // which represents the number of 100 nanosecond intervals since January 1, 1601 (UTC).
  20. UA_DateTime opcua_time_now() {
  21. UA_DateTime dateTime;
  22. struct timeval tv;
  23. gettimeofday(&tv, NULL);
  24. dateTime = (tv.tv_sec + FILETIME_UNIXTIME_BIAS_SEC)
  25. * HUNDRED_NANOSEC_PER_SEC + tv.tv_usec * HUNDRED_NANOSEC_PER_USEC;
  26. return dateTime;
  27. }