7

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.?

5
  • 6
    Seconds are the physical base for time duration values. May be this is the reason? Commented Jan 9, 2015 at 11:25
  • 5
    If it had been any other unit, would you have asked the corresponding question? Commented Jan 9, 2015 at 11:29
  • 1
    It must be based on something, since it is stored as an integral value and a ratio (of integrals). What else would you suggest as the base unit? Seconds is the most natural, as it is the SI unit for time. Commented Jan 9, 2015 at 11:36
  • If it was to be passed as a template parameter, how would these types be defined? I guess by using a ratio to some base unit. And this is exactly what chrono::duration does by itself. You can also use milliseconds as your "base" if you use the type std::chrono::milliseconds (which is typedefed to what you already posted in your second line of code) Commented Jan 9, 2015 at 11:38
  • "any reason ... not on minutes, hours, etc.?" Thanks to leap seconds, minutes and hours don't have a consistent duration. Commented Apr 23, 2017 at 21:38

1 Answer 1

48

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); 
Sign up to request clarification or add additional context in comments.

6 Comments

"Even Americans use seconds and not something like microfortnights." +1 for that :)
The standard already includes things like std::chrono::milliseconds. See the section labelled "Helper Types" on this page.
@Steve Oops, I should have checked and not taken OP's word for it.
It is never too early to start preparing for the first of April... using microfortnights = duration<long, std::ratio<756,625>>; ;-)
@HowardHinnant there is Duff's rule that says that pi seconds is a nanocentury
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.