Skip to content

Commit

Permalink
Add function to convert a time_point in a string
Browse files Browse the repository at this point in the history
Signed-off-by: ahcorde <[email protected]>
  • Loading branch information
ahcorde committed Aug 26, 2020
1 parent ef60586 commit 489a79e
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 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>
#include <iomanip>
#include <vector>
#include <tuple>
#include <utility>
Expand Down Expand Up @@ -771,6 +773,48 @@ 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<long int, std::ratio<86400>> days;

/// \brief break down durations
/// \param[in] d Duration to breaw down
/// \return A tuple based on the durations specified
template<class...Durations, class DurationIn>
std::tuple<Durations...> break_down_durations( DurationIn d ) {
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
/// \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,
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

0 comments on commit 489a79e

Please sign in to comment.