Browse Source

implemented filetime

Leon Urbas 11 years ago
parent
commit
7f0b4b1c95
1 changed files with 19 additions and 7 deletions
  1. 19 7
      src/opcua_time.c

+ 19 - 7
src/opcua_time.c

@@ -3,6 +3,9 @@
  *
  *  Created on: Feb 5, 2014
  *      Author: opcua
+ *
+ * code inspired by
+ * http://stackoverflow.com/questions/3585583/convert-unix-linux-time-to-windows-filetime
  */
 #include "opcua_builtInDatatypes.h"
 #include "opcua_advancedDatatypes.h"
@@ -11,12 +14,21 @@
 #include <errno.h>
 #include <stdarg.h>
 
-//get the current Server Time
-UA_DateTime opcua_getTime()
-{
-	//TODO call System Time function
-	UA_DateTime time = 10000000;
-	return time;
-}
+// number of seconds from 1 Jan. 1601 00:00 to 1 Jan 1970 00:00 UTC
+#define FILETIME_UNIXTIME_BIAS_SEC 11644473600LL
+//
+#define HUNDRED_NANOSEC_PER_USEC 10LL
+#define HUNDRED_NANOSEC_PER_SEC (HUNDRED_NANOSEC_PER_USEC * 1000000LL)
 
+// IEC 62541-6 §5.2.2.5  A DateTime value shall be encoded as a 64-bit signed integer
+// which represents the number of 100 nanosecond intervals since January 1, 1601 (UTC).
+
+UA_DateTime opcua_getTime() {
+	UA_DateTime dateTime;
+	struct timeval tv;
+	gettimeofday(&tv, NULL);
+	dateTime = (tv.tv_sec + FILETIME_UNIXTIME_BIAS_SEC)
+			* HUNDRED_NANOSEC_PER_SEC + tv.tv_usec * HUNDRED_NANOSEC_PER_USEC;
+	return dateTime;
+}