New function to format a timestamp to human readable

This commit is contained in:
kervala 2016-02-20 18:10:17 +01:00
parent bfb32b811d
commit 3c74803187
2 changed files with 17 additions and 0 deletions

View file

@ -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" /// Convert a time into a string that is easily readable by an human, for example 3600 -> "1h"
std::string secondsToHumanReadable (uint32 time); 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 /// Get a bytes or time in string format and convert it in seconds or bytes
uint32 fromHumanReadable (const std::string &str); uint32 fromHumanReadable (const std::string &str);

View file

@ -506,6 +506,21 @@ string secondsToHumanReadable (uint32 time)
return toString ("%u%s", res, divTable[div]); 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) uint32 fromHumanReadable (const std::string &str)
{ {
if (str.size() == 0) if (str.size() == 0)