impl timegm on windows

This commit is contained in:
lixianjing 2023-06-28 16:42:30 +08:00
parent 18cd780459
commit 5a6e17769d

View File

@ -25,6 +25,28 @@
#include "tkc/date_time.h"
#include "tkc/mem.h"
static int32_t get_local_timezone() {
int32_t timezone = 0;
struct tm tlocal;
struct tm tgmt;
time_t tutc = 0;
time(&tutc);
memset(&tlocal, 0x00, sizeof(tlocal));
localtime_s(&tlocal, &tutc);
memset(&tgmt, 0x00, sizeof(tgmt));
gmtime_s(&tgmt, &tutc);
timezone = tlocal.tm_hour - tgmt.tm_hour;
if (timezone < -11) {
timezone += 24;
} else if (timezone > 12) {
timezone -= 24;
}
return timezone;
}
#ifdef WIN32
#include <windows.h>
#pragma comment(lib, "Ws2_32.lib")
@ -83,6 +105,32 @@ static ret_t date_time_set_now_impl(date_time_t* dt) {
}
}
static uint64_t date_time_to_time_impl(date_time_t* dt) {
struct tm t;
time_t tvalue = 0;
int32_t timezone = get_local_timezone();
return_value_if_fail(dt != NULL, RET_BAD_PARAMS);
memset(&t, 0x00, sizeof(t));
t.tm_sec = dt->second;
t.tm_min = dt->minute;
t.tm_hour = dt->hour;
t.tm_mday = dt->day;
t.tm_mon = dt->month - 1;
t.tm_year = dt->year - 1900;
t.tm_wday = dt->wday;
t.tm_isdst = -1;
if (t.tm_hour < timezone) {
int32_t temp = timezone;
timezone = t.tm_hour;
t.tm_hour = temp;
}
tvalue = mktime(&t) + timezone * 60 * 60;
return tvalue;
}
#else
#include <sys/time.h>
#include <unistd.h>
@ -130,27 +178,7 @@ static ret_t date_time_set_now_impl(date_time_t* dt) {
return RET_OK;
}
#endif
static ret_t date_time_from_time_impl(date_time_t* dt, int64_t timeval) {
time_t tm = timeval;
struct tm* t = gmtime(&tm);
return_value_if_fail(dt != NULL, RET_BAD_PARAMS);
memset(dt, 0x00, sizeof(date_time_t));
dt->second = t->tm_sec;
dt->minute = t->tm_min;
dt->hour = t->tm_hour;
dt->day = t->tm_mday;
dt->month = t->tm_mon + 1;
dt->year = t->tm_year + 1900;
dt->wday = t->tm_wday;
return RET_OK;
}
static int64_t date_time_to_time_impl(date_time_t* dt) {
static uint64_t date_time_to_time_impl(date_time_t* dt) {
struct tm t;
time_t tvalue = 0;
return_value_if_fail(dt != NULL, RET_BAD_PARAMS);
@ -169,6 +197,26 @@ static int64_t date_time_to_time_impl(date_time_t* dt) {
return tvalue;
}
#endif
static ret_t date_time_from_time_impl(date_time_t* dt, uint64_t timeval) {
time_t tm = timeval;
struct tm* t = gmtime(&tm);
return_value_if_fail(dt != NULL, RET_BAD_PARAMS);
memset(dt, 0x00, sizeof(date_time_t));
dt->second = t->tm_sec;
dt->minute = t->tm_min;
dt->hour = t->tm_hour;
dt->day = t->tm_mday;
dt->month = t->tm_mon + 1;
dt->year = t->tm_year + 1900;
dt->wday = t->tm_wday;
return RET_OK;
}
uint64_t stm_now_ms();
uint64_t stm_now_us();
void stm_time_init(void);