Skip to content

Commit

Permalink
Promote server-time formatting to Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jpnurmi committed Mar 2, 2014
1 parent 9720711 commit bbd84a0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions include/znc/Utils.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class CUtils {


static CString CTime(time_t t, const CString& sTZ); static CString CTime(time_t t, const CString& sTZ);
static CString FormatTime(time_t t, const CString& sFormat, const CString& sTZ); static CString FormatTime(time_t t, const CString& sFormat, const CString& sTZ);
static CString FormatServerTime(const timeval& tv);
static SCString GetTimezones(); static SCString GetTimezones();


private: private:
Expand Down
14 changes: 1 addition & 13 deletions src/Buffer.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -42,19 +42,7 @@ CString CBufLine::GetLine(const CClient& Client, const MCString& msParams) const
if (Client.HasServerTime()) { if (Client.HasServerTime()) {
msThisParams["text"] = m_sText; msThisParams["text"] = m_sText;
CString sStr = CString::NamedFormat(m_sFormat, msThisParams); CString sStr = CString::NamedFormat(m_sFormat, msThisParams);
CString s_msec(m_time.tv_usec / 1000); return "@time=" + CUtils::FormatServerTime(m_time) + " " + sStr;
while (s_msec.length() < 3) {
s_msec = "0" + s_msec;
}
// TODO support leap seconds properly
// TODO support message-tags properly
struct tm stm;
memset(&stm, 0, sizeof(stm));
const time_t secs = m_time.tv_sec; // OpenBSD has tv_sec as int, so explicitly convert it to time_t to make gmtime_r() happy
gmtime_r(&secs, &stm);
char sTime[20] = {};
strftime(sTime, sizeof(sTime), "%Y-%m-%dT%H:%M:%S", &stm);
return "@time=" + CString(sTime) + "." + s_msec + "Z " + sStr;
} else { } else {
msThisParams["text"] = Client.GetUser()->AddTimestamp(m_time.tv_sec, m_sText); msThisParams["text"] = Client.GetUser()->AddTimestamp(m_time.tv_sec, m_sText);
return CString::NamedFormat(m_sFormat, msThisParams); return CString::NamedFormat(m_sFormat, msThisParams);
Expand Down
16 changes: 16 additions & 0 deletions src/Utils.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -428,6 +428,22 @@ CString CUtils::FormatTime(time_t t, const CString& sFormat, const CString& sTim
return s; return s;
} }


CString CUtils::FormatServerTime(const timeval& tv) {
CString s_msec(tv.tv_usec / 1000);
while (s_msec.length() < 3) {
s_msec = "0" + s_msec;
}
// TODO support leap seconds properly
// TODO support message-tags properly
struct tm stm;
memset(&stm, 0, sizeof(stm));
const time_t secs = tv.tv_sec; // OpenBSD has tv_sec as int, so explicitly convert it to time_t to make gmtime_r() happy
gmtime_r(&secs, &stm);
char sTime[20] = {};
strftime(sTime, sizeof(sTime), "%Y-%m-%dT%H:%M:%S", &stm);
return CString(sTime) + "." + s_msec + "Z";
}

namespace { namespace {
void FillTimezones(const CString& sPath, SCString& result, const CString& sPrefix) { void FillTimezones(const CString& sPath, SCString& result, const CString& sPrefix) {
CDir Dir; CDir Dir;
Expand Down

0 comments on commit bbd84a0

Please sign in to comment.