[英]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. 所以我从这个问题中理解,在构造
chrono::year
使用的整数对应于Anno Domini的原点0。
So my question is, what if I wanted to get the current chrono::year
. 所以我的问题是,如果我想获得当前的
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?
有什么比我更好的了吗?
using namespace std::chrono;
year_month_day ymd = floor<days>(system_clock::now());
const year foo = ymd.year();
Here's another way: 这是另一种方式:
auto now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
struct tm *parts = std::localtime(&now_c);
std::cout << 1900 + parts->tm_year << std::endl;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.