Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I Populate a chrono::year With the Current Year?

So I understand from this question that the integer used in the construction of a chrono::year corresponds to the Anno Domini origin of 0.

So my question is, what if I wanted to get the current chrono::year. Is there a function for that? I can obviously do:

const auto time = std::time(nullptr);
const auto current_date = *std::gmtime(&time);
const chrono::year foo{ current_date.tm_year + 1900 };

But that seems like a pretty convoluted process. Is there anything better available to me?

like image 850
Jonathan Mee Avatar asked Mar 06 '23 09:03

Jonathan Mee


1 Answers

using namespace std::chrono;
year_month_day ymd = floor<days>(system_clock::now());
const year foo = ymd.year();
like image 133
T.C. Avatar answered Mar 16 '23 04:03

T.C.