windows doesnt have localtime_r
authorAndy Green <andy.green@linaro.org>
Sun, 21 Feb 2016 13:41:22 +0000 (21:41 +0800)
committerAndy Green <andy.green@linaro.org>
Sun, 21 Feb 2016 13:41:22 +0000 (21:41 +0800)
Signed-off-by: Andy Green <andy.green@linaro.org>
lib/libwebsockets.c
test-server/test-server-status.c

index 86625bed5c789048682e6c9a61a2e33294b4f9f5..5217265e013ba73d860afaa859ebbdbff94da89c 100644 (file)
@@ -923,24 +923,33 @@ lws_ensure_user_space(struct lws *wsi)
 LWS_VISIBLE void lwsl_emit_stderr(int level, const char *line)
 {
        time_t o_now = time(NULL);
-       struct tm o_now_time;
        unsigned long long now;
+       struct tm *ptm = NULL;
        char buf[300];
-       int n, ok = localtime_r(&o_now, &o_now_time) != NULL;
+#ifndef WIN32
+       struct tm tm;
+#endif
+       int n;
 
+#ifdef WIN32
+       ptm = localtime(&o_now);
+#else
+       if (localtime_r(&o_now, &tm))
+               ptm = &tm;
+#endif
        buf[0] = '\0';
        for (n = 0; n < LLL_COUNT; n++) {
                if (level != (1 << n))
                        continue;
                now = time_in_microseconds() / 100;
-               if (ok)
+               if (ptm)
                        sprintf(buf, "[%04d/%02d/%02d %02d:%02d:%02d:%04d] %s: ",
-                               o_now_time.tm_year + 1900,
-                               o_now_time.tm_mon,
-                               o_now_time.tm_mday,
-                               o_now_time.tm_hour,
-                               o_now_time.tm_min,
-                               o_now_time.tm_sec,
+                               ptm->tm_year + 1900,
+                               ptm->tm_mon,
+                               ptm->tm_mday,
+                               ptm->tm_hour,
+                               ptm->tm_min,
+                               ptm->tm_sec,
                                (int)(now % 10000), log_level_names[n]);
                else
                        sprintf(buf, "[%llu:%04d] %s: ",
index 5cf936d9e7210c5c41a3ee755cb22caf93b3cc3e..d8ebf068b62e85c75e481a69a5a5e43195ead3c9 100644 (file)
@@ -37,7 +37,10 @@ update_status(struct lws *wsi, struct per_session_data__lws_status *pss)
        char *p = cache;
        char date[128];
        time_t t;
+       struct tm *ptm;
+#ifndef WIN32
        struct tm tm;
+#endif
 
        p += snprintf(p, 512, " { %s, \"wsi\":\"%d\", \"conns\":[",
                     server_info, live_wsi);
@@ -45,10 +48,16 @@ update_status(struct lws *wsi, struct per_session_data__lws_status *pss)
        /* render the list */
        while (*pp) {
                t = (*pp)->tv_established.tv_sec;
+#ifdef WIN32
+               ptm = localtime(&t);
+               if (!ptm)
+#else
+               ptm = &tm;
                if (!localtime_r(&t, &tm))
+#endif
                        strcpy(date, "unknown");
                else
-                       strftime(date, sizeof(date), "%F %H:%M %Z", &tm);
+                       strftime(date, sizeof(date), "%F %H:%M %Z", ptm);
                if ((p - cache) > (sizeof(cache) - 512))
                        break;
                if (subsequent)