date icon indicating copy to clipboard operation
date copied to clipboard

Literal representing for days/weeks/months/years

Open hewillk opened this issue 2 years ago • 1 comments

Hi Howard,

First of all, thank you for bringing such a wonderful library, and I am very happy that C++20 has finally adopted it.

I would like to know why the corresponding literal operator was not introduced when the duration aliases such as day/weeks/months/years were introduced because the duration aliases such as seconds that come with <chrono> have their corresponding literal operators.

I personally found it valuable to add a literals operator with the same name for them, which allows us to express the code more intuitively:

auto m = October; 
m += 8months;
auto y = 2004y + 300years;
auto d = 100d += 20days;
auto w = Sunday;
w += 2weeks;

So I wonder why they weren't introduced in the first place, is it because the names are too long? Or is there a specific reason? Is it worth it or is it superfluous? I'd like to know your thoughts on this.

Thanks, Hewill

hewillk avatar Jul 24 '23 03:07 hewillk

Thanks for your kind words. I'm glad you found this library useful.

I think "superfluous" is probably the best description of my thought process. For me a literal is a more concise way of reading and writing a unit such as s or y. However when the whole type is spelled out in the literal name, the literal for becomes only two characters shorter:

m += months{8};

vs

m += 8months;

so it just didn't seem worth it to add the complication of two ways to write the same thing.

I struggled more with where to place the y and d literals: with the durations years and days or the calendrical specifiers year and day. I chose the calendrical specifiers from a hunch that they would end up appearing in code more often than the durations. I.e. with date literals such as:

auto constexpr epoch = 1601y/1/1;

HowardHinnant avatar Jul 24 '23 14:07 HowardHinnant