如何用当前年份填充chrono :: year? [英] How can I Populate a chrono::year With the Current Year?

查看:61
本文介绍了如何用当前年份填充chrono :: year?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我从这个问题中了解到,用于构造 chrono :: year 对应于 Anno Domini 起源为0。

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 怎么办。有功能吗?我显然可以做到:

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?

推荐答案

using namespace std::chrono;
year_month_day ymd = floor<days>(system_clock::now());
const year foo = ymd.year();

这篇关于如何用当前年份填充chrono :: year?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆