Merge branch 'master' of github.com:znc/znc

This commit is contained in:
Alexey Sokolov
2017-12-13 08:30:27 +00:00
9 changed files with 134 additions and 10 deletions
+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;
}
+58
View File
@@ -498,6 +498,64 @@ CString CUtils::FormatTime(time_t t, const CString& sFormat,
return s;
}
CString CUtils::FormatTime(const timeval& tv, const CString& sFormat,
const CString& sTimezone) {
// Parse additional format specifiers before passing them to
// strftime, since the way strftime treats unknown format
// specifiers is undefined.
CString sFormat2;
// Make sure %% is parsed correctly, i.e. %%f is passed through to
// strftime to become %f, and not 123.
bool bInFormat = false;
int iDigits;
CString::size_type uLastCopied = 0, uFormatStart;
for (CString::size_type i = 0; i < sFormat.length(); i++) {
if (!bInFormat) {
if (sFormat[i] == '%') {
uFormatStart = i;
bInFormat = true;
iDigits = 3;
}
} else {
switch (sFormat[i]) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
iDigits = sFormat[i] - '0';
break;
case 'f': {
int iVal = tv.tv_usec;
int iDigitDelta = iDigits - 6; // tv_user is in 10^-6 seconds
for (; iDigitDelta > 0; iDigitDelta--)
iVal *= 10;
for (; iDigitDelta < 0; iDigitDelta++)
iVal /= 10;
sFormat2 += sFormat.substr(uLastCopied,
uFormatStart - uLastCopied);
CString sVal = CString(iVal);
sFormat2 += CString(iDigits - sVal.length(), '0');
sFormat2 += sVal;
uLastCopied = i + 1;
bInFormat = false;
break;
}
default:
bInFormat = false;
}
}
}
if (uLastCopied) {
sFormat2 += sFormat.substr(uLastCopied);
return FormatTime(tv.tv_sec, sFormat2, sTimezone);
} else {
// If there are no extended format specifiers, avoid doing any
// memory allocations entirely.
return FormatTime(tv.tv_sec, sFormat, sTimezone);
}
}
CString CUtils::FormatServerTime(const timeval& tv) {
CString s_msec(tv.tv_usec / 1000);
while (s_msec.length() < 3) {