Skip to content

Commit

Permalink
[iron] Bugfix for wrong timestamps in ros2 bag info (backport #1745) (#…
Browse files Browse the repository at this point in the history
…1753)

* Bugfix for wrong timestamps in ros2 bag info (#1745)

* Bugfix for wrong timestamps in ros2 bag info

- Correctly calculate fractional part for seconds by subtracting
`nanoseconds_from_seconds` from `nanoseconds`.

Signed-off-by: Michael Orlov <[email protected]>

* Adjust expectations in the "ros2 bag info" integration tests

Signed-off-by: Michael Orlov <[email protected]>

* Add leading zeros to the fractional seconds in the format_duration(..)

Signed-off-by: Michael Orlov <[email protected]>

* Adjust expectations in info end-to-end tests by adding leading zero

The real file duration is: 70633730 nanoseconds.
i.e., regex mask shall be "0\\.0706.*s" to match 070633730 nanoseconds.

Signed-off-by: Michael Orlov <[email protected]>

---------

Signed-off-by: Michael Orlov <[email protected]>
(cherry picked from commit da28c9d)

# Conflicts:
#	rosbag2_tests/test/rosbag2_tests/test_rosbag2_info_end_to_end.cpp

* Address merge conflicts

Signed-off-by: Michael Orlov <[email protected]>

---------

Signed-off-by: Michael Orlov <[email protected]>
Co-authored-by: Michael Orlov <[email protected]>
  • Loading branch information
mergify[bot] and MichaelOrlov authored Jul 19, 2024
1 parent 2dbad1d commit adffc7e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
15 changes: 9 additions & 6 deletions rosbag2_py/src/rosbag2_py/format_bag_metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,27 @@ std::unordered_map<std::string, std::string> format_duration(
std::chrono::high_resolution_clock::duration duration)
{
std::unordered_map<std::string, std::string> formatted_duration;
auto m_seconds = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(m_seconds);
std::string fractional_seconds = std::to_string(m_seconds.count() % 1000);
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(duration);
auto nanoseconds_from_seconds = std::chrono::duration_cast<std::chrono::nanoseconds>(seconds);
std::stringstream fractional_seconds_ss;
fractional_seconds_ss << std::setw(9) << std::setfill('0') <<
(nanoseconds - nanoseconds_from_seconds).count();
std::time_t std_time_point = seconds.count();
tm time;
#ifdef _WIN32
localtime_s(&time, &std_time_point);
#else
localtime_r(&std_time_point, &time);
#endif

std::stringstream formatted_date;
std::stringstream formatted_time;
formatted_date << std::put_time(&time, "%b %e %Y");
formatted_time << std::put_time(&time, "%H:%M:%S") << "." << fractional_seconds;
formatted_time << std::put_time(&time, "%H:%M:%S") << "." << fractional_seconds_ss.str();
formatted_duration["date"] = formatted_date.str();
formatted_duration["time"] = formatted_time.str();
formatted_duration["time_in_sec"] = std::to_string(seconds.count()) + "." + fractional_seconds;
formatted_duration["time_in_sec"] = std::to_string(seconds.count()) + "." +
fractional_seconds_ss.str();

return formatted_duration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ TEST_P(InfoEndToEndTestFixture, info_end_to_end_test) {
"\nFiles: " + expected_file +
"\nBag size: .*B"
"\nStorage id: " + expected_storage +
"\nDuration: 0\\.151s"
"\nStart: Apr .+ 2020 .*:.*:36.763 \\(1586406456\\.763\\)"
"\nEnd: Apr .+ 2020 .*:.*:36.914 \\(1586406456\\.914\\)"
"\nDuration: 0.151137181s"
"\nStart: Apr .+ 2020 .*:.*:36.763032325 \\(1586406456.763032325\\)"
"\nEnd: Apr .+ 2020 .*:.*:36.914169506 \\(1586406456.914169506\\)"
"\nMessages: 7"
"\nTopic information: "));
EXPECT_THAT(
Expand Down

0 comments on commit adffc7e

Please sign in to comment.