From b2512e55ea8239306c560a34a85e148954cf0c04 Mon Sep 17 00:00:00 2001 From: psychon Date: Fri, 27 Jun 2008 09:55:55 +0000 Subject: [PATCH] Display the uptime in a more readable way This adds CString::ToTimeStr() which converts a number of seconds into a human readable time string. git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1107 726aef4b-f618-498e-8847-2d620e286838 --- String.cpp | 26 ++++++++++++++++++++++++++ String.h | 1 + znc.cpp | 2 +- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/String.cpp b/String.cpp index 7a5c5361..be8aa6de 100644 --- a/String.cpp +++ b/String.cpp @@ -887,6 +887,32 @@ CString CString::ToByteStr(unsigned long long d) { return CString(d) + " B"; } +CString CString::ToTimeStr(unsigned long s) { + const unsigned long m = 60; + const unsigned long h = m * 60; + const unsigned long d = h * 24; + const unsigned long w = d * 7; + const unsigned long y = d * 365; + CString sRet; + +#define TIMESPAN(time, str) \ + if (s >= time) { \ + sRet += CString(s / time) + str " "; \ + s = s % time; \ + } + TIMESPAN(y, "y"); + TIMESPAN(w, "w"); + TIMESPAN(d, "d"); + TIMESPAN(h, "h"); + TIMESPAN(m, "m"); + TIMESPAN(1, "s"); + + if (sRet.empty()) + return "0s"; + + return sRet.RightChomp_n(); +} + bool CString::ToBool() const { return (!Trim_n().Trim_n("0").empty() && Trim_n().CaseCmp("false") != 0); } short CString::ToShort() const { return strtoul(this->c_str(), (char**) NULL, 10); } unsigned short CString::ToUShort() const { return strtoul(this->c_str(), (char**) NULL, 10); } diff --git a/String.h b/String.h index ec957df8..e5b98d6d 100644 --- a/String.h +++ b/String.h @@ -129,6 +129,7 @@ public: static CString ToPercent(double d); static CString ToByteStr(unsigned long long d); + static CString ToTimeStr(unsigned long s); bool ToBool() const; short ToShort() const; diff --git a/znc.cpp b/znc.cpp index 6b0dd587..2834a0e5 100644 --- a/znc.cpp +++ b/znc.cpp @@ -96,7 +96,7 @@ CString CZNC::GetTag(bool bIncludeVersion) { CString CZNC::GetUptime() { time_t now = time(NULL); - return CString(now - TimeStarted()) + " seconds"; + return CString::ToTimeStr(now - TimeStarted()); } bool CZNC::OnBoot() {