Skip to content
Snippets Groups Projects
Commit 3892f196 authored by Rainer Kartmann's avatar Rainer Kartmann
Browse files

Add and use time toString and toDateTime functions

parent e17854c4
No related branches found
No related tags found
1 merge request!90Armem/memory
...@@ -88,14 +88,9 @@ namespace armarx::armem ...@@ -88,14 +88,9 @@ namespace armarx::armem
MemoryRemoteGui::GroupBox MemoryRemoteGui::makeGroupBox(const EntitySnapshot& snapshot) const MemoryRemoteGui::GroupBox MemoryRemoteGui::makeGroupBox(const EntitySnapshot& snapshot) const
{ {
static const char* mu = "\u03BC";
(void) mu;
GroupBox group; GroupBox group;
std::stringstream ss; std::stringstream ss;
ss << "t = " << snapshot.time.toString("%Y-%m-%d %H:%M:%S") ss << "t = " << armem::toDateTimeMilliSeconds(snapshot.time);
<< "." << snapshot.time.toMicroSeconds() % 1000
// << " (" << snapshot.time.toMicroSeconds() << " " << mu << "s" << ")"
;
group.setLabel(ss.str()); group.setLabel(ss.str());
for (const auto& instance : snapshot.instances) for (const auto& instance : snapshot.instances)
......
#include "Time.h" #include "Time.h"
#include <cmath>
namespace armarx
{
std::string armem::toStringMilliSeconds(const Time& time, int decimals)
{
std::stringstream ss;
ss << time.toMilliSeconds();
if (decimals > 0)
{
int div = int(std::pow(10, 3 - decimals));
ss << "." << (time.toMicroSeconds() % 1000) / div;
}
ss << " ms";
return ss.str();
}
std::string armem::toStringMicroSeconds(const Time& time)
{
static const char* mu = "\u03BC";
std::stringstream ss;
ss << time.toMicroSeconds() << " " << mu << "s";
return ss.str();
}
std::string armem::toDateTimeMilliSeconds(const Time& time, int decimals)
{
std::stringstream ss;
ss << time.toString("%Y-%m-%d %H:%M:%S");
if (decimals > 0)
{
int div = int(std::pow(10, 6 - decimals));
ss << "." << (time.toMicroSeconds() % int(1e6)) / div;
}
return ss.str();
}
}
#pragma once
#include <IceUtil/Time.h> #include <IceUtil/Time.h>
...@@ -6,4 +8,22 @@ namespace armarx::armem ...@@ -6,4 +8,22 @@ namespace armarx::armem
using Time = IceUtil::Time; using Time = IceUtil::Time;
/**
* @brief Returns `time` as e.g. "123456789.012 ms".
* @param decimals How many sub-millisecond decimals to include.
*/
std::string toStringMilliSeconds(const Time& time, int decimals = 3);
/**
* @brief Returns `time` as e.g. "123456789012 `mu`s".
* The output string contains the actual greek letter `mu`.
*/
std::string toStringMicroSeconds(const Time& time);
/**
* @brief Returns `time`as e.g. "2020-11-16 17:01:54.123".
* @param decimals How many sub-second decimals to include.
*/
std::string toDateTimeMilliSeconds(const Time& time, int decimals = 6);
} }
...@@ -31,7 +31,7 @@ namespace armarx::armem ...@@ -31,7 +31,7 @@ namespace armarx::armem
} }
else else
{ {
throw error::MissingEntry("entity snapshot", std::to_string(time.toMilliSeconds()), throw error::MissingEntry("entity snapshot", toDateTimeMilliSeconds(time),
"entity", this->name); "entity", this->name);
} }
} }
......
...@@ -32,7 +32,7 @@ namespace armarx::armem ...@@ -32,7 +32,7 @@ namespace armarx::armem
else else
{ {
throw armem::error::MissingEntry("instance", std::to_string(index), throw armem::error::MissingEntry("instance", std::to_string(index),
"entity snapshot", std::to_string(time.toMicroSeconds())); "entity snapshot", toDateTimeMilliSeconds(time));
} }
} }
......
...@@ -35,6 +35,26 @@ namespace armem = armarx::armem; ...@@ -35,6 +35,26 @@ namespace armem = armarx::armem;
namespace aron = armarx::aron; namespace aron = armarx::aron;
BOOST_AUTO_TEST_CASE(test_time_to_string)
{
// 1: seconds, 2: milliseconds, 3: microseconds
armem::Time time = armem::Time::microSeconds(111111345789);
BOOST_CHECK_EQUAL(armem::toStringMilliSeconds(time), "111111345.789 ms");
BOOST_CHECK_EQUAL(armem::toStringMilliSeconds(time, 0), "111111345 ms");
BOOST_CHECK_EQUAL(armem::toStringMilliSeconds(time, 1), "111111345.7 ms");
BOOST_CHECK_EQUAL(armem::toStringMilliSeconds(time, 2), "111111345.78 ms");
BOOST_CHECK_EQUAL(armem::toStringMilliSeconds(time, 3), "111111345.789 ms");
BOOST_CHECK_EQUAL(armem::toStringMicroSeconds(time), "111111345789 " "\u03BC" "s");
BOOST_CHECK_EQUAL(armem::toDateTimeMilliSeconds(time), "1970-01-02 07:51:51.345");
BOOST_CHECK_EQUAL(armem::toDateTimeMilliSeconds(time, 0), "1970-01-02 07:51:51");
BOOST_CHECK_EQUAL(armem::toDateTimeMilliSeconds(time, 3), "1970-01-02 07:51:51.345");
BOOST_CHECK_EQUAL(armem::toDateTimeMilliSeconds(time, 6), "1970-01-02 07:51:51.345789");
}
BOOST_AUTO_TEST_CASE(test_segment_setup) BOOST_AUTO_TEST_CASE(test_segment_setup)
{ {
armem::InternalEntityUpdate update; armem::InternalEntityUpdate update;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment