|
29 | 29 | //#include "htime.h" |
30 | 30 | #define SECONDS_PER_HOUR 3600 |
31 | 31 | #define SECONDS_PER_DAY 86400 // 24*3600 |
32 | | -#define SECONDS_PER_WEEK 604800 // 7*24*3600; |
| 32 | +#define SECONDS_PER_WEEK 604800 // 7*24*3600 |
| 33 | + |
| 34 | +static inline struct tm* hv_localtime_r(time_t ts, struct tm* tm) { |
| 35 | +#ifdef _WIN32 |
| 36 | + localtime_s(tm, &ts); |
| 37 | +#else |
| 38 | + tm = localtime_r(&ts, tm); |
| 39 | +#endif |
| 40 | + return tm; |
| 41 | +} |
| 42 | + |
| 43 | +static inline struct tm* hv_gmtime_r(time_t ts, struct tm* tm) { |
| 44 | +#ifdef _WIN32 |
| 45 | + gmtime_s(tm, &ts); |
| 46 | +#else |
| 47 | + tm = gmtime_r(&ts, tm); |
| 48 | +#endif |
| 49 | + return tm; |
| 50 | +} |
33 | 51 |
|
34 | 52 | static int s_gmtoff = 28800; // 8*3600 |
35 | 53 | static void init_gmtoff() { |
36 | 54 | time_t ts = time(NULL); |
37 | | - struct tm* local_tm = localtime(&ts); |
38 | | - struct tm* gmt_tm = gmtime(&ts); |
39 | | - s_gmtoff = (local_tm->tm_hour - gmt_tm->tm_hour) * 3600 + |
40 | | - (local_tm->tm_min - gmt_tm->tm_min) * 60 + |
41 | | - (local_tm->tm_sec - gmt_tm->tm_sec); |
42 | | - |
43 | | - if (local_tm->tm_yday > gmt_tm->tm_yday) { |
| 55 | + struct tm local_tm, gmt_tm; |
| 56 | + memset(&local_tm, 0, sizeof(local_tm)); |
| 57 | + memset(&gmt_tm, 0, sizeof(gmt_tm)); |
| 58 | + hv_localtime_r(ts, &local_tm); |
| 59 | + hv_gmtime_r(ts, &gmt_tm); |
| 60 | + s_gmtoff = (local_tm.tm_hour - gmt_tm.tm_hour) * 3600 + |
| 61 | + (local_tm.tm_min - gmt_tm.tm_min) * 60 + |
| 62 | + (local_tm.tm_sec - gmt_tm.tm_sec); |
| 63 | + |
| 64 | + if (local_tm.tm_yday > gmt_tm.tm_yday) { |
44 | 65 | s_gmtoff += SECONDS_PER_DAY; |
45 | | - } else if (local_tm->tm_yday < gmt_tm->tm_yday) { |
| 66 | + } else if (local_tm.tm_yday < gmt_tm.tm_yday) { |
46 | 67 | s_gmtoff -= SECONDS_PER_DAY; |
47 | 68 | } |
48 | 69 | } |
@@ -222,12 +243,14 @@ const char* logger_get_cur_file(logger_t* logger) { |
222 | 243 | } |
223 | 244 |
|
224 | 245 | static void logfile_name(const char* filepath, time_t ts, char* buf, int len) { |
225 | | - struct tm* tm = localtime(&ts); |
| 246 | + struct tm tm; |
| 247 | + memset(&tm, 0, sizeof(tm)); |
| 248 | + hv_localtime_r(ts, &tm); |
226 | 249 | snprintf(buf, len, "%s.%04d%02d%02d.log", |
227 | 250 | filepath, |
228 | | - tm->tm_year+1900, |
229 | | - tm->tm_mon+1, |
230 | | - tm->tm_mday); |
| 251 | + tm.tm_year+1900, |
| 252 | + tm.tm_mon+1, |
| 253 | + tm.tm_mday); |
231 | 254 | } |
232 | 255 |
|
233 | 256 | static void logfile_truncate(logger_t* logger) { |
|
0 commit comments