Commit 3562ae6d authored by Dominik Charousset's avatar Dominik Charousset

Make timespan and timestamp convertible to string

parent 254e30c6
......@@ -28,6 +28,8 @@
#include "caf/error.hpp"
#include "caf/none.hpp"
#include "caf/string_view.hpp"
#include "caf/timespan.hpp"
#include "caf/timestamp.hpp"
#include "caf/meta/type_name.hpp"
#include "caf/meta/omittable.hpp"
......@@ -76,6 +78,22 @@ public:
void consume(string_view str);
void consume(timespan& x);
void consume(timestamp& x);
template <class Clock, class Duration>
void consume(std::chrono::time_point<Clock, Duration>& x) {
timestamp tmp{std::chrono::duration_cast<timespan>(x.time_since_epoch())};
consume(tmp);
}
template <class Rep, class Period>
void consume(std::chrono::duration<Rep, Period>& x) {
auto tmp = std::chrono::duration_cast<timespan>(x);
consume(tmp);
}
inline void consume(bool& x) {
result_ += x ? "true" : "false";
}
......
......@@ -67,5 +67,36 @@ void stringification_inspector::consume(string_view str) {
result_ += '"';
}
void stringification_inspector::consume(timespan& x) {
auto count = x.count();
auto res = [&](const char* suffix) {
result_ += std::to_string(count);
result_ += suffix;
};
// Check whether it's nano-, micro-, or milliseconds.
for (auto suffix : {"ns", "us", "ms"}) {
if (count % 1000 != 0)
return res(suffix);
count /= 1000;
}
// After the loop we only need to differentiate between seconds and minutes.
if (count % 60 != 0)
return res("s");
count /= 60;
return res("min");
}
void stringification_inspector::consume(timestamp& x) {
char buf[64];
auto y = std::chrono::time_point_cast<timestamp::clock::duration>(x);
auto z = timestamp::clock::to_time_t(y);
strftime(buf, sizeof(buf), "%FT%T", std::localtime(&z));
result_ += buf;
// time_t has no milliseconds, so we need to insert them manually.
auto ms = (x.time_since_epoch().count() / 1000000) % 1000;
result_ += '.';
result_ += std::to_string(ms);
}
} // namespace detail
} // namespace caf
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2018 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#define CAF_SUITE deep_to_string
#include "caf/deep_to_string.hpp"
#include "caf/test/dsl.hpp"
using namespace caf;
CAF_TEST(timespans) {
CAF_CHECK_EQUAL(deep_to_string(timespan{1}), "1ns");
CAF_CHECK_EQUAL(deep_to_string(timespan{1000}), "1us");
CAF_CHECK_EQUAL(deep_to_string(timespan{1000000}), "1ms");
CAF_CHECK_EQUAL(deep_to_string(timespan{1000000000}), "1s");
CAF_CHECK_EQUAL(deep_to_string(timespan{60000000000}), "1min");
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment