Skip to content

Commit a0879d8

Browse files
authored
[time][fix]call safe time function (#891)
1 parent f4744e8 commit a0879d8

4 files changed

Lines changed: 49 additions & 4 deletions

File tree

include/ylt/easylog/appender.hpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ inline void to_int(int num, char *p, int &size) {
4747
p[--size] = c;
4848
}
4949

50+
inline std::tm localtime_safe(std::time_t timer) {
51+
std::tm bt{};
52+
#if defined(__unix__)
53+
localtime_r(&timer, &bt);
54+
#elif defined(_MSC_VER)
55+
localtime_s(&bt, &timer);
56+
#else
57+
static std::mutex mtx;
58+
std::lock_guard<std::mutex> lock(mtx);
59+
bt = *std::localtime(&timer);
60+
#endif
61+
return bt;
62+
}
63+
5064
inline char *get_time_str(const auto &now) {
5165
static thread_local char buf[33];
5266
static thread_local std::chrono::seconds last_sec_{};
@@ -63,7 +77,8 @@ inline char *get_time_str(const auto &now) {
6377

6478
last_sec_ = s;
6579
auto tm = std::chrono::system_clock::to_time_t(now);
66-
auto gmt = localtime(&tm);
80+
auto ltm = localtime_safe(tm);
81+
std::tm *gmt = &ltm;
6782

6883
to_int<3, '.'>(mill_sec, buf, size);
6984
to_int<2, ':'>(gmt->tm_sec, buf, size);

include/ylt/standalone/cinatra/time_util.hpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,26 @@ inline void to_hour(char *buf, int day, char c) { to_int<2>(day, c, buf); }
278278
inline void to_min(char *buf, int day, char c) { to_int<2>(day, c, buf); }
279279
inline void to_sec(char *buf, int day, char c) { to_int<2>(day, c, buf); }
280280

281+
inline std::tm gmtime_safe(std::time_t &timer) {
282+
std::tm bt{};
283+
#if defined(__unix__)
284+
gmtime_r(&timer, &bt);
285+
#elif defined(_MSC_VER)
286+
gmtime_s(&bt, &timer);
287+
#else
288+
static std::mutex mtx;
289+
std::lock_guard<std::mutex> lock(mtx);
290+
bt = *gmtime(&timer);
291+
#endif
292+
return bt;
293+
}
294+
281295
template <size_t Hour = 8, size_t N>
282296
inline std::string_view get_local_time_str(char (&buf)[N], std::time_t t,
283297
std::string_view format) {
284298
static_assert(N >= 20, "wrong buf");
285-
struct tm *loc_time = gmtime(&t);
299+
std::tm tm = gmtime_safe(t);
300+
std::tm *loc_time = &tm;
286301

287302
char *p = buf;
288303

include/ylt/util/time_util.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,11 +424,26 @@ inline void to_hour(char *buf, int day, char c) { to_int<2>(day, c, buf); }
424424
inline void to_min(char *buf, int day, char c) { to_int<2>(day, c, buf); }
425425
inline void to_sec(char *buf, int day, char c) { to_int<2>(day, c, buf); }
426426

427+
inline std::tm gmtime_safe(std::time_t &timer) {
428+
std::tm bt{};
429+
#if defined(__unix__)
430+
gmtime_r(&timer, &bt);
431+
#elif defined(_MSC_VER)
432+
gmtime_s(&bt, &timer);
433+
#else
434+
static std::mutex mtx;
435+
std::lock_guard<std::mutex> lock(mtx);
436+
bt = *gmtime(&timer);
437+
#endif
438+
return bt;
439+
}
440+
427441
template <size_t Hour = 8, size_t N>
428442
inline std::string_view get_local_time_str(char (&buf)[N], std::time_t t,
429443
std::string_view format) {
430444
static_assert(N >= 20, "wrong buf");
431-
struct tm *loc_time = gmtime(&t);
445+
std::tm tm = gmtime_safe(t);
446+
std::tm *loc_time = &tm;
432447

433448
char *p = buf;
434449

src/coro_http/tests/test_cinatra_websocket.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ TEST_CASE("test read write in different threads") {
317317
CHECK(data.resp_body == send_str);
318318
}
319319
};
320-
another_thread_lazy().via(coro_io::get_global_executor()).start([](auto &&) {
320+
another_thread_lazy().start([](auto &&) {
321321
});
322322

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

0 commit comments

Comments
 (0)