Commit dda88128 authored by zhaoyunfei's avatar zhaoyunfei

localtime_r用cpu计算的方式代替

parent 0a2e5931
......@@ -1418,7 +1418,8 @@ void LogMessage::Init(const char* file,
data_->outvec_ = NULL;
WallTime now = WallTime_Now();
data_->timestamp_ = static_cast<time_t>(now);
localtime_r(&data_->timestamp_, &data_->tm_time_);
// localtime_r(&data_->timestamp_, &data_->tm_time_);
FastSecondToDate(data_->timestamp_, &data_->tm_time_, GetCurTimeZone());
data_->usecs_ = static_cast<int32>((now - data_->timestamp_) * 1000000);
data_->num_chars_to_log_ = 0;
......
......@@ -220,6 +220,54 @@ WallTime WallTime_Now() {
return CycleClock_Now() * 0.000001;
}
int _GetCurTimeZone(){
int timezone = 0;
time_t t1, t2 ;
struct tm *tm_local, *tm_utc;
time(&t1);
t2 = t1;
tm_local = localtime(&t1);
t1 = mktime(tm_local) ;
tm_utc = gmtime(&t2);
t2 = mktime(tm_utc);
timezone = int((t1 - t2) / 3600);
return timezone;
}
static int32 g_timezone = _GetCurTimeZone();
int32_t GetCurTimeZone(){
return g_timezone;
}
int FastSecondToDate(const time_t& unix_sec, struct tm* tm, int time_zone) {
static const int kHoursInDay = 24;
static const int kMinutesInHour = 60;
static const int kDaysFromUnixTime = 2472632;
static const int kDaysFromYear = 153;
static const int kMagicUnkonwnFirst = 146097;
static const int kMagicUnkonwnSec = 1461;
tm->tm_sec = unix_sec % kMinutesInHour;
int i = (unix_sec/kMinutesInHour);
tm->tm_min = i % kMinutesInHour; //nn
i /= kMinutesInHour;
tm->tm_hour = (i + time_zone) % kHoursInDay; // hh
tm->tm_mday = (i + time_zone) / kHoursInDay;
int a = tm->tm_mday + kDaysFromUnixTime;
int b = (a*4 + 3)/kMagicUnkonwnFirst;
int c = (-b*kMagicUnkonwnFirst)/4 + a;
int d =((c*4 + 3) / kMagicUnkonwnSec);
int e = -d * kMagicUnkonwnSec;
e = e/4 + c;
int m = (5*e + 2)/kDaysFromYear;
tm->tm_mday = -(kDaysFromYear * m + 2)/5 + e + 1;
tm->tm_mon = (-m/10)*12 + m + 2;
tm->tm_year = b*100 + d - 6700 + (m/10);
return 0;
}
static int32 g_main_thread_pid = getpid();
int32 GetMainThreadPid() {
return g_main_thread_pid;
......
......@@ -170,6 +170,9 @@ int64 UsecToCycles(int64 usec);
typedef double WallTime;
WallTime WallTime_Now();
int32_t GetCurTimeZone();
int32_t FastSecondToDate(const time_t& unix_sec, struct tm* tm, int time_zone);
int32 GetMainThreadPid();
bool PidHasChanged();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment