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 functions to convert between time_point and secNsec #150

Merged
merged 18 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
84 changes: 84 additions & 0 deletions include/ignition/math/Helpers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <limits>
#include <string>
#include <iostream>
#include <sstream>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're in here, can we alphabetize includes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done :) f9ef912

#include <iomanip>
#include <vector>
#include <tuple>
#include <utility>
Expand Down Expand Up @@ -727,6 +729,43 @@ namespace ignition
}
}

/// \brief Convert a std::chrono::steady_clock::time_point to a seconds and
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
/// nanoseconds pair.
/// \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.
inline std::pair<int64_t, int64_t> timePointToSecNsec(
const std::chrono::system_clock::time_point &_time)
mjcarroll marked this conversation as resolved.
Show resolved Hide resolved
{
auto now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
_time.time_since_epoch());
auto now_s = std::chrono::duration_cast<std::chrono::seconds>(
_time.time_since_epoch());
int64_t seconds = std::chrono::duration_cast<std::chrono::seconds>(
_time.time_since_epoch()).count();
scpeters marked this conversation as resolved.
Show resolved Hide resolved
int64_t nanoseconds = std::chrono::duration_cast
<std::chrono::nanoseconds>(now_ns - now_s).count();
return {seconds, nanoseconds};
}

/// \brief Convert to a seconds and nanoseconds to
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
/// std::chrono::steady_clock::time_point.
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
/// \param[in] _sec The seconds to convert.
/// \param[in] _nanosec The nanoseconds to convert.
/// \return A std::chrono::system_clock::time_poin based on the number of
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
/// seconds and
/// the number of nanoseconds.
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
inline std::chrono::system_clock::time_point secNsecToTimePoint(
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
const uint64_t &_sec, const uint64_t &_nanosec)
{
auto duration = std::chrono::seconds(_sec) + std::chrono::nanoseconds(
_nanosec);
std::chrono::system_clock::time_point result =
std::chrono::system_clock::from_time_t(0);
result = result + duration;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit: could condense with result += duration, I'll leave it up to you though, same with all below additions like this made in the tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for making suggestions that don't actually work; I thought it would be cleaner. I'll take a look with my mac later today

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into this a bit: Seems like OSX stores chrono to only a microsecond precision, whereas Ubuntu goes to nanosecond precision. It seems like all platforms have different precisions. See eddelbuettel/nanotime#74 and https://stackoverflow.com/questions/55120594/different-behaviour-of-system-clock-on-windows-and-linux

return result;
}

/// \brief Convert a std::chrono::steady_clock::duration to a seconds and
/// nanoseconds pair.
/// \param[in] _dur The duration to convert.
Expand All @@ -740,6 +779,51 @@ namespace ignition
return {s.count(), ns.count()};
}

// TODO(anyone): Replace this with std::chrono::days.
/// This will exist in C++-20
typedef std::chrono::duration<uint64_t, std::ratio<86400>> days;

/// \brief break down durations
/// \param[in] d Duration to breaw down
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
/// \return A tuple based on the durations specified
template<class...Durations, class DurationIn>
std::tuple<Durations...> break_down_durations(DurationIn d) {
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
std::tuple<Durations...> retval;
using discard = int[];
(void)discard{0, (void((
(std::get<Durations>(retval) =
std::chrono::duration_cast<Durations>(d)),
(d -= std::chrono::duration_cast<DurationIn>(
std::get<Durations>(retval))))), 0)...};
return retval;
}

/// \brief Convert a std::chrono::steady_clock::duration to a string
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
/// \param[in] _point The std::chrono::system_clock::time_point to convert.
/// \return A string formatted with the time_point
inline std::string timePointToString(
const std::chrono::system_clock::time_point &_point)
{
auto duration = _point - std::chrono::system_clock::from_time_t(0);
auto clean_duration = break_down_durations<days,
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
std::chrono::hours,
std::chrono::minutes,
std::chrono::seconds,
std::chrono::milliseconds>(
duration);
std::ostringstream output_string;
output_string << std::get<0>(clean_duration).count() << " "
<< std::setw(2) << std::setfill('0')
<< std::get<1>(clean_duration).count() << ":"
<< std::setw(2) << std::setfill('0')
<< std::get<2>(clean_duration).count() << ":"
<< std::setw(2) << std::setfill('0')
<< std::setprecision(3)
<< std::get<3>(clean_duration).count() +
std::get<4>(clean_duration).count()/1000.0;
return output_string.str();
}

// Degrade precision on Windows, which cannot handle 'long double'
// values properly. See the implementation of Unpair.
// 32 bit ARM processors also define 'long double' to be the same
Expand Down
45 changes: 45 additions & 0 deletions src/Helpers_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,51 @@ TEST(HelpersTest, Pair)
}
}

/////////////////////////////////////////////////
TEST(HelpersTest, timePointToSecNsec)
{
std::pair<int64_t, int64_t> parts = math::timePointToSecNsec(
std::chrono::system_clock::from_time_t(0));
EXPECT_EQ(parts.first, 0);
EXPECT_EQ(parts.second, 0);

// Jan 2 01:00:00 1970
std::tm timeinfo = std::tm();
timeinfo.tm_sec = 0;
timeinfo.tm_min = 0;
timeinfo.tm_hour = 1;
timeinfo.tm_mon = 0;
timeinfo.tm_year = 70;
timeinfo.tm_mday = 2;
std::time_t tt = std::mktime(&timeinfo);

parts = math::timePointToSecNsec(
std::chrono::system_clock::from_time_t(tt));

EXPECT_EQ(parts.first, 24*60*60);
EXPECT_EQ(parts.second, 0);
}

/////////////////////////////////////////////////
TEST(HelpersTest, secNsecToTimePoint)
{
std::chrono::system_clock::time_point s = math::secNsecToTimePoint(0, 0);
EXPECT_EQ(s, std::chrono::system_clock::from_time_t(0));

// Jan 2 01:00:00 1970
std::tm timeinfo = std::tm();
timeinfo.tm_sec = 0;
timeinfo.tm_min = 0;
timeinfo.tm_hour = 1;
timeinfo.tm_mon = 0;
timeinfo.tm_year = 70;
timeinfo.tm_mday = 2;
std::time_t tt = std::mktime(&timeinfo);

s = math::secNsecToTimePoint(24*60*60, 0);
EXPECT_EQ(s, std::chrono::system_clock::from_time_t(tt));
}

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