Skip to content

Commit

Permalink
Added a method to get GMT offset from LogMessageTime
Browse files Browse the repository at this point in the history
  • Loading branch information
vijaysattigeri committed Nov 18, 2021
1 parent 8b87221 commit 1fb5429
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/glog/logging.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ typedef unsigned __int64 uint64;
struct LogMessageTime {
explicit LogMessageTime (const struct::tm& time_struct_,
const time_t& timestamp_, const int32_t& usecs_):
time_struct(time_struct_), ts(timestamp_), usecs(usecs_) {}
time_struct(time_struct_), ts(timestamp_), usecs(usecs_) {
CalcGmtOffset();
}

const time_t& timestamp() const { return ts; }
const int& sec() const { return time_struct.tm_sec; }
Expand All @@ -152,11 +154,15 @@ struct LogMessageTime {
const int& dayOfWeek() const { return time_struct.tm_wday; }
const int& dayInYear() const { return time_struct.tm_yday; }
const int& dst() const { return time_struct.tm_isdst; }
const long int& gmtoff() const { return gmtoffset; }

private:
const struct::tm& time_struct;
const time_t& ts;
const int32_t& usecs;
long int gmtoffset;

void CalcGmtOffset();
};

struct LogMessageInfo {
Expand Down Expand Up @@ -1552,6 +1558,9 @@ public:
// Must be called without the log_mutex held. (L < log_mutex)
static int64 num_messages(int severity);

// Return the time info. as reference
void GetTimeInfo(struct::tm& time_struct, time_t& ts, int32_t& usecs) const;

struct LogMessageData;

private:
Expand Down
26 changes: 26 additions & 0 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,14 @@ void LogMessage::Init(const char* file,
}
}

void LogMessage::GetTimeInfo(struct::tm& time_struct,
time_t& ts,
int32_t& usecs) const {
time_struct = data_->tm_time_;
ts = data_->timestamp_;
usecs = data_->usecs_;
}

LogMessage::~LogMessage() {
Flush();
#ifdef GLOG_THREAD_LOCAL_STORAGE
Expand Down Expand Up @@ -2566,3 +2574,21 @@ void DisableLogCleaner() {
}

_END_GOOGLE_NAMESPACE_

#ifdef GLOG_CUSTOM_PREFIX_SUPPORT
void LogMessageTime::CalcGmtOffset() {
std::tm gmt_struct;
int isDst = 0;
if ( FLAGS_log_utc_time ) {
localtime_r(&ts, &gmt_struct);
isDst = gmt_struct.tm_isdst;
gmt_struct = time_struct;
} else {
isDst = time_struct.tm_isdst;
gmtime_r(&ts, &gmt_struct);
}

time_t gmt_sec = mktime(&gmt_struct);
gmtoffset = static_cast<long int>(ts - gmt_sec + (isDst ? 3600 : 0) ) ;
}
#endif
18 changes: 18 additions & 0 deletions src/logging_custom_prefix_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1379,3 +1379,21 @@ TEST(UserDefinedClass, logging) {
// We must be able to compile this.
CHECK_EQ(u, u);
}

TEST(LogMsgTime, gmtoff) {
/*
* Unit test for GMT offset API
* TODO: To properly test this API, we need a platform independent way to set time-zone.
* */
google::LogMessage log_obj(__FILE__, __LINE__);

struct::tm time_struct;
time_t ts = static_cast<time_t>(-1);
int32_t usecs = -1;
log_obj.GetTimeInfo(time_struct, ts, usecs);

LogMessageTime time_obj(time_struct, ts, usecs);
long int nGmtOff = time_obj.gmtoff();
// GMT offset ranges from UTC-12:00 to UTC+14:00
EXPECT_TRUE( (nGmtOff >= -43200) && (nGmtOff <= 50400) );
}

0 comments on commit 1fb5429

Please sign in to comment.