Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is std::chrono::duration based on seconds

Tags:

c++

chrono

I'm learning <chrono> library, and considering the std::chrono::duration class, is there any specific reason to base it on seconds? For example a variable to store seconds would be

chrono::duration<int> two_seconds(2);

and all other time spans require relating them to seconds, like

chrono::duration<int, ratio<60>> two_minutes(2);
chrono::duration<int, ratio<1, 1000>> two_milliseconds(2);
chrono::duration<int, ratio<60 * 60 * 24>> two_days(2); 

Are there any reasons to base duration on seconds and not on minutes, hours, etc.?

like image 634
Wojtek Avatar asked Jan 09 '15 11:01

Wojtek


People also ask

What is std :: Chrono :: duration?

Class template std::chrono::duration represents a time interval. It consists of a count of ticks of type Rep and a tick period, where the tick period is a compile-time rational fraction representing the time in seconds from one tick to the next. The only data stored in a duration is a tick count of type Rep .


1 Answers

Seconds are chosen because it's the basic time unit in both the SI system and in science as a whole.
Even Americans use seconds and not something like microfortnights.

why this basic time span isn't another template parameter for duration class

It is, as you can provide typedefs for ratios, and some are included in the standard.

#include <chrono>

std::chrono::duration<int, minutes> two_minutes(2);            // Standard
std::chrono::duration<int, milliseconds> two_milliseconds(2);  // Standard

If you need more, they're trivial to add:

typedef std::ratio<60 * 60 * 24> days;
typedef std::ratio<756, 625> microfortnights;

std::chrono::duration<int, days> two_days(2); 
std::chrono::duration<int, microfortnights> two_weeks(1000000); 
like image 51
molbdnilo Avatar answered Oct 09 '22 21:10

molbdnilo