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

Fix time to string in armem

parent 94d65d04
No related branches found
No related tags found
1 merge request!96ArMemMemoryViewer
#include "Time.h"
#include <cmath>
#include <iomanip>
namespace armarx
......@@ -14,7 +15,8 @@ namespace armarx
if (decimals > 0)
{
int div = int(std::pow(10, 3 - decimals));
ss << "." << (time.toMicroSeconds() % 1000) / div;
ss << "." << std::setfill('0') << std::setw(decimals)
<< (time.toMicroSeconds() % 1000) / div;
}
ss << " ms";
return ss.str();
......@@ -37,7 +39,8 @@ namespace armarx
if (decimals > 0)
{
int div = int(std::pow(10, 6 - decimals));
ss << "." << (time.toMicroSeconds() % int(1e6)) / div;
ss << "." << std::setfill('0') << std::setw(decimals)
<< (time.toMicroSeconds() % int(1e6)) / div;
}
return ss.str();
......
......@@ -21,7 +21,7 @@ namespace armarx::armem
std::string toStringMicroSeconds(const Time& time);
/**
* @brief Returns `time`as e.g. "2020-11-16 17:01:54.123".
* @brief Returns `time`as e.g. "2020-11-16 17:01:54.123456".
* @param decimals How many sub-second decimals to include.
*/
std::string toDateTimeMilliSeconds(const Time& time, int decimals = 6);
......
......@@ -37,7 +37,7 @@ namespace aron = armarx::aron;
BOOST_AUTO_TEST_CASE(test_time_to_string)
{
// 1: seconds, 2: milliseconds, 3: microseconds
// 111111: seconds, 345: milliseconds, 789: microseconds
armem::Time time = armem::Time::microSeconds(111111345789);
BOOST_CHECK_EQUAL(armem::toStringMilliSeconds(time), "111111345.789 ms");
......@@ -48,10 +48,29 @@ BOOST_AUTO_TEST_CASE(test_time_to_string)
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), "1970-01-02 07:51:51.345789");
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");
// 111111: seconds, 000: milliseconds, 789: microseconds
time = armem::Time::microSeconds(111111000789);
BOOST_CHECK_EQUAL(armem::toStringMilliSeconds(time), "111111000.789 ms");
BOOST_CHECK_EQUAL(armem::toStringMicroSeconds(time), "111111000789 " "\u03BC" "s");
BOOST_CHECK_EQUAL(armem::toDateTimeMilliSeconds(time), "1970-01-02 07:51:51.000789");
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.000");
BOOST_CHECK_EQUAL(armem::toDateTimeMilliSeconds(time, 6), "1970-01-02 07:51:51.000789");
// 111111: seconds, 345: milliseconds, 000: microseconds
time = armem::Time::microSeconds(111111345000);
BOOST_CHECK_EQUAL(armem::toStringMilliSeconds(time), "111111345.000 ms");
BOOST_CHECK_EQUAL(armem::toStringMicroSeconds(time), "111111345000 " "\u03BC" "s");
BOOST_CHECK_EQUAL(armem::toDateTimeMilliSeconds(time), "1970-01-02 07:51:51.345000");
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.345000");
}
......
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