From ee1d69503b1ff785ee7ac051b67e2daa77d8c641 Mon Sep 17 00:00:00 2001 From: kervala Date: Sat, 20 Feb 2016 18:10:17 +0100 Subject: [PATCH] New function to format a timestamp to human readable --- code/nel/include/nel/misc/common.h | 2 ++ code/nel/src/misc/common.cpp | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/code/nel/include/nel/misc/common.h b/code/nel/include/nel/misc/common.h index 23b827a0b..dd1c80fb3 100644 --- a/code/nel/include/nel/misc/common.h +++ b/code/nel/include/nel/misc/common.h @@ -353,6 +353,8 @@ uint32 humanReadableToBytes (const std::string &str); /// Convert a time into a string that is easily readable by an human, for example 3600 -> "1h" std::string secondsToHumanReadable (uint32 time); +/// Convert a UNIX timestamp to a formatted date in ISO format +std::string timestampToHumanReadable(uint32 timestamp); /// Get a bytes or time in string format and convert it in seconds or bytes uint32 fromHumanReadable (const std::string &str); diff --git a/code/nel/src/misc/common.cpp b/code/nel/src/misc/common.cpp index c1a824b23..c986234d7 100644 --- a/code/nel/src/misc/common.cpp +++ b/code/nel/src/misc/common.cpp @@ -506,6 +506,21 @@ string secondsToHumanReadable (uint32 time) return toString ("%u%s", res, divTable[div]); } +std::string timestampToHumanReadable(uint32 timestamp) +{ + char buffer[30]; + time_t dtime = timestamp; + tm *tms = localtime(&dtime); + + if (tms) + { + strftime(buffer, 30, "%Y-%m-%d %H:%M:%S", tms); + return std::string(buffer); + } + + return ""; +} + uint32 fromHumanReadable (const std::string &str) { if (str.size() == 0)