浏览代码

win32 change gmtime to gmtime_s

StalderT 6 年之前
父节点
当前提交
3e51e870bf
共有 3 个文件被更改,包括 18 次插入12 次删除
  1. 7 5
      include/ua_types.h
  2. 10 6
      plugins/ua_clock.c
  3. 1 1
      tests/testing-plugins/testing_clock.c

+ 7 - 5
include/ua_types.h

@@ -169,8 +169,7 @@ UA_STRING(char *chars) {
  * DateTime
  * ^^^^^^^^
  * An instance in time. A DateTime value is encoded as a 64-bit signed integer
- * which represents the number of 100 nanosecond intervals since January 1, 1601
- * (UTC). */
+ * which represents the number of 100 nanosecond intervals since January 1, 1601 */
 typedef int64_t UA_DateTime;
 
 /* Multiply to convert units for time difference computations */
@@ -181,15 +180,18 @@ typedef int64_t UA_DateTime;
 #define UA_DATETIME_TO_MSEC (UA_DATETIME_TO_USEC / 1000.0)
 #define UA_DATETIME_TO_SEC (UA_DATETIME_TO_MSEC / 1000.0)
 
-/* Datetime of 1 Jan 1970 00:00 UTC */
+/* Datetime of 1 Jan 1970 00:00 */
 #define UA_DATETIME_UNIX_EPOCH (11644473600LL * UA_SEC_TO_DATETIME)
 
-/* The current time */
-UA_DateTime UA_EXPORT UA_DateTime_now(void);
+/* The current time in UTC time*/
+UA_DateTime UA_EXPORT UA_DateTime_nowUtcTime(void);
 
 /* The current time in local time*/
 UA_DateTime UA_EXPORT UA_DateTime_nowLocalTime(void);
 
+/* Compatibility macro. Should be remove in the future */
+#define UA_DateTime_now() UA_DateTime_nowUtcTime()
+
 /* CPU clock invariant to system time changes. Use only for time diffs, not
  * current time */
 UA_DateTime UA_EXPORT UA_DateTime_nowMonotonic(void);

+ 10 - 6
plugins/ua_clock.c

@@ -40,7 +40,7 @@
 
 #include "ua_types.h"
 
-UA_DateTime UA_DateTime_now(void) {
+UA_DateTime UA_DateTime_nowUtcTime(void) {
 #if defined(_WIN32)
     /* Windows filetime has the same definition as UA_DateTime */
     FILETIME ft;
@@ -62,17 +62,21 @@ UA_DateTime UA_DateTime_now(void) {
 static
 UA_DateTime UA_DateTime_diffLocalTimeUTC(void) {
     time_t gmt, rawtime = time(NULL);
-    struct tm *ptm;
 
-#if !defined(_WIN32)
+#ifdef _WIN32
+    struct tm ptm;
+    gmtime_s(&ptm, &rawtime);
+    // Request that mktime() looksup dst in timezone database
+    ptm.tm_isdst = -1;
+    gmt = mktime(&ptm);
+#else
+    struct tm *ptm;
     struct tm gbuf;
     ptm = gmtime_r(&rawtime, &gbuf);
-#else
-    ptm = gmtime(&rawtime);
-#endif
     // Request that mktime() looksup dst in timezone database
     ptm->tm_isdst = -1;
     gmt = mktime(ptm);
+#endif
 
     return (UA_DateTime) ((int)difftime(rawtime, gmt) * UA_SEC_TO_DATETIME);
 }

+ 1 - 1
tests/testing-plugins/testing_clock.c

@@ -24,7 +24,7 @@
 
 UA_DateTime testingClock = 0;
 
-UA_DateTime UA_DateTime_now(void) {
+UA_DateTime UA_DateTime_nowUtcTime(void) {
     return testingClock;
 }