Use and propagate microsecond-precision timestamps to FormatTime

This enables sub-second precision timestamp formatting for logs and
clients without server-time.
This commit is contained in:
Vladimir Panteleev
2017-10-24 21:45:44 +00:00
parent 901a21e91b
commit 03c4c0b165
6 changed files with 21 additions and 10 deletions
+1
View File
@@ -110,6 +110,7 @@ class CUser {
CString AddTimestamp(const CString& sStr) const;
CString AddTimestamp(time_t tm, const CString& sStr) const;
CString AddTimestamp(timeval tv, const CString& sStr) const;
void CloneNetworks(const CUser& User);
bool Clone(const CUser& User, CString& sErrorRet,
+2 -2
View File
@@ -58,8 +58,8 @@ class CAwayJob : public CTimer {
class CAway : public CModule {
void AwayCommand(const CString& sCommand) {
CString sReason;
time_t curtime;
time(&curtime);
timeval curtime;
gettimeofday(&curtime, nullptr);
if (sCommand.Token(1) != "-quiet") {
sReason = CUtils::FormatTime(curtime, sCommand.Token(1, true),
+4 -2
View File
@@ -154,8 +154,10 @@ class CListSockets : public CModule {
CString GetCreatedTime(Csock* pSocket) {
unsigned long long iStartTime = pSocket->GetStartTime();
time_t iTime = iStartTime / 1000;
return CUtils::FormatTime(iTime, "%Y-%m-%d %H:%M:%S",
timeval tv;
tv.tv_sec = iStartTime / 1000;
tv.tv_usec = iStartTime % 1000 * 1000;
return CUtils::FormatTime(tv, "%Y-%m-%d %H:%M:%S.%L",
GetUser()->GetTimezone());
}
+2 -2
View File
@@ -271,9 +271,9 @@ void CLogMod::PutLog(const CString& sLine,
}
CString sPath;
time_t curtime;
timeval curtime;
time(&curtime);
gettimeofday(&curtime, nullptr);
// Generate file name
sPath = CUtils::FormatTime(curtime, m_sLogPath, GetUser()->GetTimezone());
if (sPath.empty()) {
+1 -1
View File
@@ -52,7 +52,7 @@ CMessage CBufLine::ToMessage(const CClient& Client,
mssThisParams["text"] = m_sText;
} else {
mssThisParams["text"] =
Client.GetUser()->AddTimestamp(Line.GetTime().tv_sec, m_sText);
Client.GetUser()->AddTimestamp(Line.GetTime(), m_sText);
}
// make a copy of params, because the following loop modifies the original
+11 -3
View File
@@ -603,17 +603,25 @@ CString& CUser::ExpandString(const CString& sStr, CString& sRet) const {
}
CString CUser::AddTimestamp(const CString& sStr) const {
time_t tm;
return AddTimestamp(time(&tm), sStr);
timeval tv;
gettimeofday(&tv, nullptr);
return AddTimestamp(tv, sStr);
}
CString CUser::AddTimestamp(time_t tm, const CString& sStr) const {
timeval tv;
tv.tv_sec = tm;
tv.tv_usec = 0;
return AddTimestamp(tv, sStr);
}
CString CUser::AddTimestamp(timeval tv, const CString& sStr) const {
CString sRet = sStr;
if (!GetTimestampFormat().empty() &&
(m_bAppendTimestamp || m_bPrependTimestamp)) {
CString sTimestamp =
CUtils::FormatTime(tm, GetTimestampFormat(), m_sTimezone);
CUtils::FormatTime(tv, GetTimestampFormat(), m_sTimezone);
if (sTimestamp.empty()) {
return sRet;
}