2

Am holding duration's in a year_month_day. Is there a simple way to add the year_month_day duration to a time_point like sys_seconds?

sys_seconds date1 = {};
sys_seconds dat2 = {};
year_month_day calDuration;
date1 = date2 + calDuration;    //error: no match for ‘operator+’ 
myk
  • 708
  • 2
  • 8
  • 20
  • 2
    I don't think that `year_month_day` is a duration. It is a point in time, isn't it? `sys_seconds` is a `time_point`. It just doesn't make sense to add two time points. Rethink your problem. – j6t Dec 18 '20 at 13:42

1 Answers1

3

This is chrono catching logic bugs for you at compile-time. Adding date2 + calDuration is akin to adding tomorrow + today. It just doesn't make sense. And that's why it is a compile-time error.

What you may mean is that you have durations years, months and days. This is not the same as the similarly named types year, month and day. The plural forms are chrono::durations, just like minutes and nanoseconds. days is 24h. And years and months are the average length of those units in the civil calendar.

Conversely, the singular forms year, month and day are the calendrical components that give a name to a day in the civil calendar, e.g. 2020y, December, and 18d. And it is these singular forms that make up a year_month_day, e.g. 2020y/December/18d.

See this SO answer for a deep-dive on the difference between month and months.

There are multiple ways to add the units years and months to a time_point. See this SO answer for a deep-dive on that topic.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • Ah - ok - your right I didn't notice that we have both std::chrono::month and std::chrono::months etc. as completely different things. Guess I'll define a years_months_days then to hold my calendar duration's. – myk Dec 19 '20 at 02:41
  • Sounds good. And in that case, I'm guessing what you want to do is calendrical arithmetic, as opposed to chronological arithmetic for the `years` and `months` units. See https://stackoverflow.com/a/43018120/576911 for more details about this distinction and how to handle it. – Howard Hinnant Dec 19 '20 at 13:51