From 1b9e74d6c00033b2103f70162c96e9bf5ce6f2b7 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 21 Aug 2015 15:14:27 +0300 Subject: Add function for convert time diff to string. --- src/actions/actions.cpp | 54 ++--------------------------------------- src/utils/stringutils.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++ src/utils/stringutils.h | 2 ++ 3 files changed, 66 insertions(+), 52 deletions(-) (limited to 'src') diff --git a/src/actions/actions.cpp b/src/actions/actions.cpp index ee5a2481e..329202612 100644 --- a/src/actions/actions.cpp +++ b/src/actions/actions.cpp @@ -1162,59 +1162,9 @@ impHandler0(uptime) } else { - std::string str; - int timeDiff = cur_time - start_time; - - const int weeks = timeDiff / 60 / 60 / 24 / 7; - if (weeks > 0) - { - // TRANSLATORS: uptime command - str = strprintf(ngettext(N_("%d week"), N_("%d weeks"), - weeks), weeks); - timeDiff -= weeks * 60 * 60 * 24 * 7; - } - - const int days = timeDiff / 60 / 60 / 24; - if (days > 0) - { - if (!str.empty()) - str.append(", "); - // TRANSLATORS: uptime command - str.append(strprintf(ngettext(N_("%d day"), N_("%d days"), - days), days)); - timeDiff -= days * 60 * 60 * 24; - } - const int hours = timeDiff / 60 / 60; - if (hours > 0) - { - if (!str.empty()) - str.append(", "); - // TRANSLATORS: uptime command - str.append(strprintf(ngettext(N_("%d hour"), N_("%d hours"), - hours), hours)); - timeDiff -= hours * 60 * 60; - } - const int min = timeDiff / 60; - if (min > 0) - { - if (!str.empty()) - str.append(", "); - // TRANSLATORS: uptime command - str.append(strprintf(ngettext(N_("%d minute"), N_("%d minutes"), - min), min)); - timeDiff -= min * 60; - } - - if (timeDiff > 0) - { - if (!str.empty()) - str.append(", "); - // TRANSLATORS: uptime command - str.append(strprintf(ngettext(N_("%d second"), N_("%d seconds"), - timeDiff), timeDiff)); - } // TRANSLATORS: uptime command - debugChatTab->chatLog(strprintf(_("Client uptime: %s"), str.c_str()), + debugChatTab->chatLog(strprintf(_("Client uptime: %s"), + timeDiffToString(cur_time - start_time).c_str()), ChatMsgType::BY_SERVER); } return true; diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index 3e067fe5c..864f509fc 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -29,6 +29,8 @@ #include #endif +#include "utils/gettext.h" + #include "debug.h" static size_t UTF8_MAX_SIZE = 10; @@ -877,3 +879,63 @@ std::string timeToStr(const int time) else return "unknown"; } + +std::string timeDiffToString(int timeDiff) +{ + std::string str; + + const int weeks = timeDiff / 60 / 60 / 24 / 7; + if (weeks > 0) + { + // TRANSLATORS: uptime command + str = strprintf(ngettext(N_("%d week"), N_("%d weeks"), + weeks), weeks); + timeDiff -= weeks * 60 * 60 * 24 * 7; + } + + const int days = timeDiff / 60 / 60 / 24; + if (days > 0) + { + if (!str.empty()) + str.append(", "); + // TRANSLATORS: uptime command + str.append(strprintf(ngettext(N_("%d day"), N_("%d days"), + days), days)); + timeDiff -= days * 60 * 60 * 24; + } + const int hours = timeDiff / 60 / 60; + if (hours > 0) + { + if (!str.empty()) + str.append(", "); + // TRANSLATORS: uptime command + str.append(strprintf(ngettext(N_("%d hour"), N_("%d hours"), + hours), hours)); + timeDiff -= hours * 60 * 60; + } + const int min = timeDiff / 60; + if (min > 0) + { + if (!str.empty()) + str.append(", "); + // TRANSLATORS: uptime command + str.append(strprintf(ngettext(N_("%d minute"), N_("%d minutes"), + min), min)); + timeDiff -= min * 60; + } + + if (timeDiff > 0) + { + if (!str.empty()) + str.append(", "); + // TRANSLATORS: uptime command + str.append(strprintf(ngettext(N_("%d second"), N_("%d seconds"), + timeDiff), timeDiff)); + } + if (str.empty()) + { + str.append(strprintf(ngettext(N_("%d second"), N_("%d seconds"), + 0), 0)); + } + return str; +} diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h index 67144ea65..73e4b7697 100644 --- a/src/utils/stringutils.h +++ b/src/utils/stringutils.h @@ -252,4 +252,6 @@ std::string removeToken(std::string &str, const std::string &token); std::string timeToStr(const int time); +std::string timeDiffToString(int timeDiff); + #endif // UTILS_STRINGUTILS_H -- cgit v1.2.3-60-g2f50