Search code examples
c++c++20c++-chronoweekday

How to get the weekday number from std::chrono::year_month_day in C++


In C++ 20, the following code will output the number (0-6) for the weekday of the input date:

#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';
}

How can I get that number to use in code as a numeric value so I could use it in a calculation? This has to be simple but I can't figure it out.

int total_score = weekday{dmy} * 10;

As a side note, I am really using the date (http://howardhinnant.github.io/date/date.html) library created by Howard Hinnant in C++ 17 but I believe the same question applies to both.


Solution

  • You can use std::chrono::weekday::c_encoding to retrieve the stored value.