Skip to content

Commit

Permalink
[time][fix]call safe time function (#891)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Feb 5, 2025
1 parent f4744e8 commit a0879d8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
17 changes: 16 additions & 1 deletion include/ylt/easylog/appender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ inline void to_int(int num, char *p, int &size) {
p[--size] = c;
}

inline std::tm localtime_safe(std::time_t timer) {
std::tm bt{};
#if defined(__unix__)
localtime_r(&timer, &bt);
#elif defined(_MSC_VER)
localtime_s(&bt, &timer);
#else
static std::mutex mtx;
std::lock_guard<std::mutex> lock(mtx);
bt = *std::localtime(&timer);
#endif
return bt;
}

inline char *get_time_str(const auto &now) {
static thread_local char buf[33];
static thread_local std::chrono::seconds last_sec_{};
Expand All @@ -63,7 +77,8 @@ inline char *get_time_str(const auto &now) {

last_sec_ = s;
auto tm = std::chrono::system_clock::to_time_t(now);
auto gmt = localtime(&tm);
auto ltm = localtime_safe(tm);
std::tm *gmt = &ltm;

to_int<3, '.'>(mill_sec, buf, size);
to_int<2, ':'>(gmt->tm_sec, buf, size);
Expand Down
17 changes: 16 additions & 1 deletion include/ylt/standalone/cinatra/time_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,26 @@ inline void to_hour(char *buf, int day, char c) { to_int<2>(day, c, buf); }
inline void to_min(char *buf, int day, char c) { to_int<2>(day, c, buf); }
inline void to_sec(char *buf, int day, char c) { to_int<2>(day, c, buf); }

inline std::tm gmtime_safe(std::time_t &timer) {
std::tm bt{};
#if defined(__unix__)
gmtime_r(&timer, &bt);
#elif defined(_MSC_VER)
gmtime_s(&bt, &timer);
#else
static std::mutex mtx;
std::lock_guard<std::mutex> lock(mtx);
bt = *gmtime(&timer);
#endif
return bt;
}

template <size_t Hour = 8, size_t N>
inline std::string_view get_local_time_str(char (&buf)[N], std::time_t t,
std::string_view format) {
static_assert(N >= 20, "wrong buf");
struct tm *loc_time = gmtime(&t);
std::tm tm = gmtime_safe(t);
std::tm *loc_time = &tm;

char *p = buf;

Expand Down
17 changes: 16 additions & 1 deletion include/ylt/util/time_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,26 @@ inline void to_hour(char *buf, int day, char c) { to_int<2>(day, c, buf); }
inline void to_min(char *buf, int day, char c) { to_int<2>(day, c, buf); }
inline void to_sec(char *buf, int day, char c) { to_int<2>(day, c, buf); }

inline std::tm gmtime_safe(std::time_t &timer) {
std::tm bt{};
#if defined(__unix__)
gmtime_r(&timer, &bt);
#elif defined(_MSC_VER)
gmtime_s(&bt, &timer);
#else
static std::mutex mtx;
std::lock_guard<std::mutex> lock(mtx);
bt = *gmtime(&timer);
#endif
return bt;
}

template <size_t Hour = 8, size_t N>
inline std::string_view get_local_time_str(char (&buf)[N], std::time_t t,
std::string_view format) {
static_assert(N >= 20, "wrong buf");
struct tm *loc_time = gmtime(&t);
std::tm tm = gmtime_safe(t);
std::tm *loc_time = &tm;

char *p = buf;

Expand Down
2 changes: 1 addition & 1 deletion src/coro_http/tests/test_cinatra_websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ TEST_CASE("test read write in different threads") {
CHECK(data.resp_body == send_str);
}
};
another_thread_lazy().via(coro_io::get_global_executor()).start([](auto &&) {
another_thread_lazy().start([](auto &&) {
});

auto lazy = [client, weak, &send_str]() -> async_simple::coro::Lazy<void> {
Expand Down

0 comments on commit a0879d8

Please sign in to comment.