~singpolyma/biboumi

2a4905df0153c47656555a21f3d57bbad6f3ffe1 — louiz’ 7 years ago 9727f4c
Fix to_string(time_t) and write a unit test for it
2 files changed, 15 insertions(+), 4 deletions(-)

M louloulibs/utils/time.cpp
M tests/utils.cpp
M louloulibs/utils/time.cpp => louloulibs/utils/time.cpp +5 -4
@@ 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};
}
}
\ No newline at end of file

M tests/utils.cpp => tests/utils.cpp +10 -0
@@ 7,6 7,7 @@
#include <utils/xdg.hpp>
#include <utils/empty_if_fixed_server.hpp>
#include <utils/get_first_non_empty.hpp>
#include <utils/time.hpp>

using namespace std::string_literals;



@@ 111,3 112,12 @@ TEST_CASE("first non-empty string")
  CHECK(get_first_non_empty("first"s) == "first"s);
  CHECK(get_first_non_empty(0, 1, 2, 3) == 1);
}

TEST_CASE("time_to_string")
{
  const std::time_t stamp = 1472480968;
  const std::string result = "2016-08-29T14:29:28Z";
  CHECK(utils::to_string(stamp) == result);
  CHECK(utils::to_string(stamp).size() == result.size());
  CHECK(utils::to_string(0) == "1970-01-01T00:00:00Z");
}