你可以转换
system_clock::now()
std::chrono::year_month_day
std::chrono::sys_days
. 实际上,这可能看起来像
#include <chrono>
bool IsTodayChristmas() {
using namespace std::chrono;
constexpr month_day Christmas = {December / 25};
auto Now = year_month_day{floor<days>(system_clock::now())};
// either
return Now == Christmas / Now.year();
// or
return Now.month() / Now.day() == Christmas;
}
正如Howard Hinnant指出的,这将决定UTC的圣诞节。你更有可能在圣诞节后在当地时区:要做到这一点,我们必须首先转变
Now
进入我们当地的时区:(注
std::chrono::current_zone
(据我所知,libstdc++或libc++尚未提供。)
bool IsTodayChristmas() {
using namespace std::chrono;
constexpr month_day Christmas = {December / 25};
auto Now_local = current_zone()->to_local(system_clock::now());
auto Today = year_month_day{floor<days>(Now_local)};
return Today == Christmas / Today.year();
}