Fix to_string(time_t) and write a unit test for it

This commit is contained in:
louiz’
2016-08-31 01:17:45 +02:00
parent 9727f4cf00
commit 2a4905df01
2 changed files with 15 additions and 4 deletions
+5 -4
View File
@@ -4,9 +4,10 @@ namespace utils
{
std::string to_string(const std::time_t& timestamp)
{
constexpr std::size_t stamp_size = 20;
char date_buf[stamp_size];
std::strftime(date_buf, stamp_size, "%FT%TZ", std::gmtime(&timestamp));
return {std::begin(date_buf), std::end(date_buf)};
constexpr std::size_t stamp_size = 21;
char date_buf[stamp_size];
if (std::strftime(date_buf, stamp_size, "%FT%TZ", std::gmtime(&timestamp)) != stamp_size - 1)
return "";
return {std::begin(date_buf), std::end(date_buf) - 1};
}
}