Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting duration value as double

Let's say, I have following duration value:

auto duration=12h+15min+99s+99ms;

I want to know how many hours that is (as double value).

When I do auto hours=std::chrono::duration_cast<std::chrono::hours>(duration), I get hours.count() which is int. What is the right way to get the value for the whole duration expressed as double?

like image 533
Arsen Zahray Avatar asked Dec 04 '25 03:12

Arsen Zahray


1 Answers

using namespace std::chrono;

// Create a double-based hours duration unit
using dhours = duration<double, hours::period>;

// Assign your integral-based duration to it
dhours h = 12h+15min+99s+99ms;

// Get the value
cout << h.count();

Or alternatively, simply divide your integral based duration by one double-based hours:

cout << (12h+15min+99s+99ms)/1.0h << '\n';
like image 140
Howard Hinnant Avatar answered Dec 05 '25 18:12

Howard Hinnant