Skip to content

Commit

Permalink
thread_local localtime()
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed May 2, 2024
1 parent 93fe5a9 commit c270399
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
31 changes: 17 additions & 14 deletions ww/eventloop/base/hlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ static void logger_init(logger_t* logger) {
logger_t* logger_create() {
// init gmtoff here
time_t ts = time(NULL);
struct tm* local_tm = localtime(&ts);
int local_hour = local_tm->tm_hour;
static _Thread_local struct tm local_tm;
localtime_r(&ts,&local_tm);

int local_hour = local_tm.tm_hour;
struct tm* gmt_tm = gmtime(&ts);
int gmt_hour = gmt_tm->tm_hour;
s_gmtoff = (local_hour - gmt_hour) * SECONDS_PER_HOUR;
Expand Down Expand Up @@ -203,12 +205,13 @@ const char* logger_get_cur_file(logger_t* logger) {
#endif

static void logfile_name(const char* filepath, time_t ts, char* buf, int len) {
struct tm* tm = localtime(&ts);
static _Thread_local struct tm tm;
localtime_r(&ts,&tm);
snprintf(buf, len, "%s.%04d%02d%02d.log",
filepath,
tm->tm_year+1900,
tm->tm_mon+1,
tm->tm_mday);
tm.tm_year+1900,
tm.tm_mon+1,
tm.tm_mday);
}
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
Expand Down Expand Up @@ -315,16 +318,16 @@ int logger_print(logger_t* logger, int level, const char* fmt, ...) {
us = tm.wMilliseconds * 1000;
#else
struct timeval tv;
struct tm* tm = NULL;
static _Thread_local struct tm tm;
gettimeofday(&tv, NULL);
time_t tt = tv.tv_sec;
tm = localtime(&tt);
year = tm->tm_year + 1900;
month = tm->tm_mon + 1;
day = tm->tm_mday;
hour = tm->tm_hour;
min = tm->tm_min;
sec = tm->tm_sec;
localtime_r(&tt,&tm);
year = tm.tm_year + 1900;
month = tm.tm_mon + 1;
day = tm.tm_mday;
hour = tm.tm_hour;
min = tm.tm_min;
sec = tm.tm_sec;
us = tv.tv_usec;
#endif

Expand Down
26 changes: 15 additions & 11 deletions ww/eventloop/base/htime.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "htime.h"
#include <time.h>

static const char* s_weekdays[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

Expand Down Expand Up @@ -73,23 +74,26 @@ datetime_t datetime_now() {
}

datetime_t datetime_localtime(time_t seconds) {
struct tm* tm = localtime(&seconds);
static _Thread_local struct tm tm;
localtime_r(&seconds,&tm);
datetime_t dt;
dt.year = tm->tm_year + 1900;
dt.month = tm->tm_mon + 1;
dt.day = tm->tm_mday;
dt.hour = tm->tm_hour;
dt.min = tm->tm_min;
dt.sec = tm->tm_sec;
dt.year = tm.tm_year + 1900;
dt.month = tm.tm_mon + 1;
dt.day = tm.tm_mday;
dt.hour = tm.tm_hour;
dt.min = tm.tm_min;
dt.sec = tm.tm_sec;
return dt;
}

time_t datetime_mktime(datetime_t* dt) {
struct tm tm;
time_t ts;
time(&ts);
struct tm* ptm = localtime(&ts);
memcpy(&tm, ptm, sizeof(struct tm));
static _Thread_local struct tm ptm;
localtime_r(&ts,&ptm);

memcpy(&tm, &ptm, sizeof(struct tm));
tm.tm_year = dt->year - 1900;
tm.tm_mon = dt->month - 1;
tm.tm_mday = dt->day;
Expand Down Expand Up @@ -225,10 +229,10 @@ time_t cron_next_timeout(int minute, int hour, int day, int week, int month) {
MONTHLY,
YEARLY,
} period_type = MINUTELY;
struct tm tm;
time_t tt;
time(&tt);
tm = *localtime(&tt);
static _Thread_local struct tm tm;
localtime_r(&tt,&tm);
time_t tt_round = 0;

tm.tm_sec = 0;
Expand Down

0 comments on commit c270399

Please sign in to comment.