-
Notifications
You must be signed in to change notification settings - Fork 67
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
Changes from 6 commits
ef60586
489a79e
b7ccbcd
770b512
8f13ba3
a9cd19f
b2b1451
0efc2c1
ec4845f
e5061a4
fef7f9a
f11f678
36eb876
ab0e075
8067150
76b3555
87d71f8
f9ef912
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ | |
#include <limits> | ||
#include <string> | ||
#include <iostream> | ||
#include <sstream> | ||
#include <iomanip> | ||
#include <vector> | ||
#include <tuple> | ||
#include <utility> | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small nit: could condense with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is failing in OSX https://build.osrfoundation.org/job/ignition_math-ci-pr_any-homebrew-amd64/666/consoleFull#1943795879aa60f765-a60a-427d-900c-dc128ab22287 even if you use your suggestion. Any thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked into this a bit: Seems like OSX stores |
||
return result; | ||
} | ||
|
||
/// \brief Convert a std::chrono::steady_clock::duration to a seconds and | ||
/// nanoseconds pair. | ||
/// \param[in] _dur The duration to convert. | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done :) f9ef912