summaryrefslogtreecommitdiff
path: root/src/utils/stringutils.cpp
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2015-08-21 15:14:27 +0300
committerAndrei Karas <akaras@inbox.ru>2015-08-21 15:14:27 +0300
commit1b9e74d6c00033b2103f70162c96e9bf5ce6f2b7 (patch)
tree3d1949898f6f72fe4af62797928b4b8be242ca22 /src/utils/stringutils.cpp
parentaac315f8a199fccb5843bee95c59157626e6cece (diff)
downloadplus-1b9e74d6c00033b2103f70162c96e9bf5ce6f2b7.tar.gz
plus-1b9e74d6c00033b2103f70162c96e9bf5ce6f2b7.tar.bz2
plus-1b9e74d6c00033b2103f70162c96e9bf5ce6f2b7.tar.xz
plus-1b9e74d6c00033b2103f70162c96e9bf5ce6f2b7.zip
Add function for convert time diff to string.
Diffstat (limited to 'src/utils/stringutils.cpp')
-rw-r--r--src/utils/stringutils.cpp62
1 files changed, 62 insertions, 0 deletions
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 <sys/time.h>
#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;
+}