Skip to content

Commit 35af7f3

Browse files
committed
log: create function get_current_time to include windows support
Signed-off-by: lecaros <[email protected]>
1 parent d7ee0b2 commit 35af7f3

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/flb_log.c

+27-1
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,32 @@ struct flb_log *flb_log_create(struct flb_config *config, int type,
561561
return log;
562562
}
563563

564+
#ifdef _WIN32
565+
#include "windows.h"
566+
567+
#define WINDOWS_EPOCH_OFFSET 116444736000000000ULL
568+
569+
void get_current_time(struct timespec *ts)
570+
{
571+
FILETIME ft;
572+
ULARGE_INTEGER li;
573+
574+
GetSystemTimeAsFileTime(&ft);
575+
li.LowPart = ft.dwLowDateTime;
576+
li.HighPart = ft.dwHighDateTime;
577+
578+
// Convert to Unix epoch
579+
uint64_t time = (li.QuadPart - WINDOWS_EPOCH_OFFSET) / 10;
580+
ts->tv_sec = time / 1000000;
581+
ts->tv_nsec = (time % 1000000) * 1000;
582+
}
583+
#else
584+
void get_current_time(struct timespec *ts)
585+
{
586+
clock_gettime(CLOCK_REALTIME, ts);
587+
}
588+
#endif
589+
564590
int flb_log_construct(struct log_message *msg, int *ret_len,
565591
int type, const char *file, int line, const char *fmt, va_list *args)
566592
{
@@ -620,7 +646,7 @@ int flb_log_construct(struct log_message *msg, int *ret_len,
620646
}
621647
#endif // FLB_LOG_NO_CONTROL_CHARS
622648

623-
clock_gettime(CLOCK_REALTIME, &ts);
649+
get_current_time(&ts);
624650
current = localtime_r(&ts.tv_sec, &result);
625651

626652
if (current == NULL) {

0 commit comments

Comments
 (0)