Add ToString for time_points
Adds ToString helper functions for aos's time_point classes that match
the format that LogReader expects for start/end_time.
Change-Id: Ied8635da70ceabb82328ea47b34f878084663bdd
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/time/time.cc b/aos/time/time.cc
index db71379..e75fe45 100644
--- a/aos/time/time.cc
+++ b/aos/time/time.cc
@@ -6,6 +6,7 @@
#include <cstring>
#include <ctime>
#include <iomanip>
+#include <sstream>
#ifdef __linux__
@@ -79,6 +80,18 @@
return stream;
}
+std::string ToString(const aos::monotonic_clock::time_point &now) {
+ std::ostringstream stream;
+ stream << now;
+ return stream.str();
+}
+
+std::string ToString(const aos::realtime_clock::time_point &now) {
+ std::ostringstream stream;
+ stream << now;
+ return stream.str();
+}
+
#ifdef __linux__
std::optional<monotonic_clock::time_point> monotonic_clock::FromString(
const std::string_view now) {
diff --git a/aos/time/time.h b/aos/time/time.h
index 1a5cbd1..8462625 100644
--- a/aos/time/time.h
+++ b/aos/time/time.h
@@ -81,6 +81,9 @@
std::ostream &operator<<(std::ostream &stream,
const aos::realtime_clock::time_point &now);
+std::string ToString(const aos::monotonic_clock::time_point &now);
+std::string ToString(const aos::realtime_clock::time_point &now);
+
namespace time {
#ifdef __linux__
diff --git a/aos/time/time_test.cc b/aos/time/time_test.cc
index e8b7153..97116b0 100644
--- a/aos/time/time_test.cc
+++ b/aos/time/time_test.cc
@@ -225,4 +225,12 @@
}
}
+// Test that ToString works for monotonic and realtime time points.
+TEST(TimeTest, ToStringTimePoints) {
+ EXPECT_EQ(ToString(realtime_clock::epoch() + std::chrono::hours(5 * 24) +
+ std::chrono::seconds(11) + std::chrono::milliseconds(5)),
+ "1970-01-06_00-00-11.005000000");
+ EXPECT_EQ(ToString(monotonic_clock::min_time), "-9223372036.854775808sec");
+}
+
} // namespace aos::time::testing