std::chrono::year_month_day_last::operator sys_days, std::chrono::year_month_day_last::operator local_days
来自 cppreference.com
< cpp | chrono | year month day last
constexpr operator std::chrono::sys_days() const noexcept; |
(1) | (自 C++20 起) |
constexpr explicit operator std::chrono::local_days() const noexcept; |
(2) | (自 C++20 起) |
将 *this 转换为一个 std::chrono::time_point,它代表与该 year_month_day_last
相同的日期。这等同于从 year()
、month()
和 day()
组成一个 year_month_day
,并将该 year_month_day
转换为目标类型。
1) 等同于 std::chrono::sys_days(year()/month()/day()).
2) 等同于 std::chrono::local_days(year()/month()/day()).
[编辑] 示例
运行此代码
#include <array> #include <chrono> #include <iostream> #include <string_view> using namespace std::chrono; using namespace std::literals; int main() { constexpr std::chrono::year y{2023y}; constexpr std::array quarters{"1st"sv, "2nd"sv, "3rd"sv, "4th"sv}; constexpr auto mq{12 / 4}; // months per quarter std::cout << "In year " << static_cast<int>(y) << '\n'; for (auto q = 1; q < 5; ++q) { const auto ls = y / std::chrono::month(q * mq) / Sunday[last]; const auto ld = y / std::chrono::month(q * mq) / last; // subtract last Sunday from last day for day of week const auto index = (sys_days(ld) - sys_days(ls)).count(); std::cout << "The " << quarters[q - 1] << " quarter ends on a " << std::chrono::weekday(index) << '\n'; } }
输出
In year 2023 The 1st quarter ends on a Fri The 2nd quarter ends on a Fri The 3rd quarter ends on a Sat The 4th quarter ends on a Sun
[编辑] 另请参阅
转换为 std::chrono::time_point ( std::chrono::year_month_day 的公共成员函数) |