Преглед на файлове

fix localtime in logger

StalderT преди 7 години
родител
ревизия
26b3165821
променени са 4 файла, в които са добавени 31 реда и са изтрити 1 реда
  1. 3 0
      include/ua_types.h
  2. 23 0
      plugins/ua_clock.c
  3. 1 1
      plugins/ua_log_stdout.c
  4. 4 0
      tests/testing-plugins/testing_clock.c

+ 3 - 0
include/ua_types.h

@@ -187,6 +187,9 @@ typedef int64_t UA_DateTime;
 /* The current time */
 UA_DateTime UA_EXPORT UA_DateTime_now(void);
 
+/* The current time in local time*/
+UA_DateTime UA_EXPORT UA_DateTime_nowLocalTime(void);
+
 /* CPU clock invariant to system time changes. Use only for time diffs, not
  * current time */
 UA_DateTime UA_EXPORT UA_DateTime_nowMonotonic(void);

+ 23 - 0
plugins/ua_clock.c

@@ -58,6 +58,29 @@ UA_DateTime UA_DateTime_now(void) {
 #endif
 }
 
+/* credit to https://stackoverflow.com/questions/13804095/get-the-time-zone-gmt-offset-in-c */
+static
+UA_DateTime UA_DateTime_diffLocalTimeUTC(void) {
+    time_t gmt, rawtime = time(NULL);
+    struct tm *ptm;
+
+#if !defined(_WIN32)
+    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);
+
+    return (UA_DateTime) ((int)difftime(rawtime, gmt) * UA_SEC_TO_DATETIME);
+}
+
+UA_DateTime UA_DateTime_nowLocalTime(void) {
+    return UA_DateTime_now() + UA_DateTime_diffLocalTimeUTC();
+}
+
 UA_DateTime UA_DateTime_nowMonotonic(void) {
 #if defined(_WIN32)
     LARGE_INTEGER freq, ticks;

+ 1 - 1
plugins/ua_log_stdout.c

@@ -47,7 +47,7 @@ __attribute__((__format__(__printf__, 3 , 0)))
 void
 UA_Log_Stdout(UA_LogLevel level, UA_LogCategory category,
               const char *msg, va_list args) {
-    UA_String t = UA_DateTime_toString(UA_DateTime_now());
+    UA_String t = UA_DateTime_toString(UA_DateTime_nowLocalTime());
 #ifdef UA_ENABLE_MULTITHREADING
     pthread_mutex_lock(&printf_mutex);
 #endif

+ 4 - 0
tests/testing-plugins/testing_clock.c

@@ -28,6 +28,10 @@ UA_DateTime UA_DateTime_now(void) {
     return testingClock;
 }
 
+UA_DateTime UA_DateTime_nowLocalTime(void) {
+    return testingClock;
+}
+
 UA_DateTime UA_DateTime_nowMonotonic(void) {
     return testingClock;
 }