【问题标题】:How to get the weekday number from std::chrono::year_month_day in C++如何从 C++ 中的 std::chrono::year_month_day 获取工作日编号
【发布时间】:2021-11-26 23:38:40
【问题描述】:

在 C++ 20 中,以下代码将输出输入日期的星期几的数字 (0-6):

#include <chrono>
#include <iostream>

int
main()
{
    using namespace std;
    using namespace std::chrono;
    year_month_day dmy;
    cin >> parse("%d %m %Y", dmy);
    cout << format("%w", weekday{dmy}) << '\n';
}

如何让该数字在代码中用作数值,以便在计算中使用它?这一定很简单,但我想不通。

int total_score = weekday{dmy} * 10;

作为旁注,我确实使用了由 Howard Hinnant 在 C++ 17 中创建的日期 (http://howardhinnant.github.io/date/date.html) 库,但我相信同样的问题适用于两者。

【问题讨论】:

  • @appleapple 这看起来像是对我的回答 :)
  • @appleapple 这正是我想要的。我知道这很简单。如果您将此作为答案并在代码 sn-p 中包含相关的函数调用,我会将其标记为答案。

标签: c++ c++20 chrono weekday


【解决方案1】:

您可以使用std::chrono::weekday::c_encoding 来检索存储的值。

【讨论】:

    猜你喜欢
    • 2021-03-29
    • 1970-01-01
    • 2012-08-31
    • 2013-04-04
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 2021-08-01
    相关资源
    最近更新 更多