fix tc_common time zone bug

This commit is contained in:
ruanshudong 2021-03-24 09:32:51 +08:00
parent 057eadfe56
commit e02534af69
2 changed files with 19 additions and 9 deletions

View File

@ -257,7 +257,7 @@ public:
* extern long timezone;
* need extern long timezone;
*
* @param sString GMT格式的时间
* @param sString GMT格式的时间
* @param sString time in GMT format
* @param stTm
* @param stTm converted Time Structure
@ -270,7 +270,7 @@ public:
* @brief .
* @brief Format time string to timestamp
*
* @param sString
* @param sString
* @param sString format time string
* @param sFormat
* @param sFormat format of formatted string time
@ -309,7 +309,7 @@ public:
* @brief tm.
* @brief Convert time into tm.
*
* @param t
* @param t UTC时间戳
* @param t time structure
*/
static void tm2time(const time_t &t, struct tm &tt);
@ -318,7 +318,7 @@ public:
* @brief time_t转换成tm(localtime_r, !!!)
* @brief Convert time_t to tm (Don't use system's localtime_r. The function will be slowed down.)
*
* @param t
* @param t UTC时间戳
* @param t time structure
* @param sFormat
* @param sFormat Target format to be converted, default to compact format

View File

@ -488,12 +488,14 @@ public:
{
struct tm timeinfo;
time_t secs, local_secs, gmt_secs;
// UTC时间戳
time(&secs);
//带时区时间
TC_Port::localtime_r(&secs, &timeinfo);
local_secs = ::mktime(&timeinfo);
timezone_local = string(timeinfo.tm_zone);
//不带时区时间
TC_Port::gmtime_r(&secs, &timeinfo);
@ -502,9 +504,13 @@ public:
timezone_diff_secs = local_secs - gmt_secs;
}
static string timezone_local;
static int64_t timezone_diff_secs;
};
string TimezoneHelper::timezone_local;
int64_t TimezoneHelper::timezone_diff_secs = 0;
@ -554,6 +560,10 @@ void TC_Common::tm2time(const time_t &t, struct tm &tt)
TC_Port::gmtime_r(&localt, &tt);
static string local_timezone = TimezoneHelper::timezone_local;
tt.tm_zone = const_cast<char *>(local_timezone.c_str());
// tt.tm_zone = TimezoneHelper::timezone_local.c_str();
tt.tm_gmtoff = TimezoneHelper::timezone_diff_secs;
}
string TC_Common::tm2str(const time_t &t, const string &sFormat)
@ -566,10 +576,10 @@ string TC_Common::tm2str(const time_t &t, const string &sFormat)
void TC_Common::tm2tm(const time_t &t, struct tm &tt)
{
static TimezoneHelper helper;
time_t localt = t + TimezoneHelper::timezone_diff_secs;
TC_Port::gmtime_r(&localt, &tt);
tm2time(t, tt);
// static TimezoneHelper helper;
// time_t localt = t + TimezoneHelper::timezone_diff_secs;
// TC_Port::gmtime_r(&localt, &tt);
}