简体   繁体   English

如何使用当前年度填充chrono :: year?

[英]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.

相关问题 如何构建chrono :: year对象? - How is a chrono::year Object Constructed? 如何将 chrono::year_month_day 添加到 chrono::sys_seconds - How to add chrono::year_month_day to chrono::sys_seconds 用std :: chrono显示日,月和年? - Show day, month and year with std::chrono? 如何从年、月、日、小时、分钟、秒、毫秒中获取计时时间点? - How to get chrono time_point from year, month, day, hour, minute, second, millisecond? C++20<chrono> : 如何计算 year_month_date 之间的差异?</chrono> - C++20 <chrono>: How to calculate difference between year_month_date? 我的 C++ 程序如何访问计算机的日期和时间以仅打印和使用当年? - How can my C++ program access the computer's date and time to print and use just the current year? 使用boost :: chrono,如何从单独的年,月,日,时,分,秒分别计算自纪元UTC以来的毫秒数 - Using boost::chrono, how to calculate milliseconds since epoch UTC from separate year, month, day, hour,min,secs QDateTime:自动添加当前年份 - QDateTime: add current year automatically 如何在不使用命名空间 chrono 的情况下初始化 std::chrono::duration 常量? - How can I initialize a std::chrono::duration constant without using namespace chrono? 我如何知道一年中的13号星期五出现了多少次? - How can I find how many times Friday the 13th appears in a year?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM