Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added secNsecToDuration to helpers functions #158

Merged
merged 3 commits into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions include/ignition/math/Helpers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,6 @@ namespace ignition

/// \brief Convert a std::chrono::steady_clock::time_point to a seconds and
/// nanoseconds pair.
// and on macOS, microsecond precision.
chapulina marked this conversation as resolved.
Show resolved Hide resolved
/// \param[in] _time The time point to convert.
/// \return A pair where the first element is the number of seconds and
/// the second is the number of nanoseconds.
Expand All @@ -751,7 +750,6 @@ namespace ignition

/// \brief Convert seconds and nanoseconds to
/// std::chrono::steady_clock::time_point.
// and on macOS, microsecond precision.
/// \param[in] _sec The seconds to convert.
/// \param[in] _nanosec The nanoseconds to convert.
/// \return A std::chrono::steady_clock::time_point based on the number of
Expand All @@ -767,9 +765,26 @@ namespace ignition
return result;
}

/// \brief Convert seconds and nanoseconds to
/// std::chrono::steady_clock::duration.
/// \param[in] _sec The seconds to convert.
/// \param[in] _nanosec The nanoseconds to convert.
/// \return A std::chrono::steady_clock::duration based on the number of
/// seconds and the number of nanoseconds.
inline std::chrono::steady_clock::duration secNsecToDuration(
const uint64_t &_sec, const uint64_t &_nanosec)
{
auto duration = std::chrono::seconds(_sec) + std::chrono::nanoseconds(
_nanosec);
std::chrono::steady_clock::duration result =
std::chrono::steady_clock::duration::zero();
using std::chrono::duration_cast;
result += duration_cast<std::chrono::steady_clock::duration>(duration);
return result;
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
}

/// \brief Convert a std::chrono::steady_clock::duration to a seconds and
/// nanoseconds pair.
// and on macOS, microsecond precision.
/// \param[in] _dur The duration to convert.
/// \return A pair where the first element is the number of seconds and
/// the second is the number of nanoseconds.
Expand Down
22 changes: 22 additions & 0 deletions src/Helpers_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,28 @@ TEST(HelpersTest, timePointToString)
EXPECT_STREQ(s.c_str(), std::string("00 00:01:23.125").c_str());
}


/////////////////////////////////////////////////
TEST(HelpersTest, secNsecToDuration)
{
using std::chrono::duration_cast;
using std::chrono::nanoseconds;
using std::chrono::steady_clock;
ahcorde marked this conversation as resolved.
Show resolved Hide resolved

std::chrono::steady_clock::duration point =
std::chrono::steady_clock::duration::zero();
point += std::chrono::hours(24);

std::chrono::steady_clock::duration s =
math::secNsecToDuration(24*60*60, 0);
EXPECT_EQ(s, point);

point = std::chrono::steady_clock::duration::zero();
point += std::chrono::nanoseconds(1000);
s = math::secNsecToDuration(0, 1000);
EXPECT_EQ(s, point);
}

/////////////////////////////////////////////////
TEST(HelpersTest, stringToTimePoint)
{
Expand Down