2

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.

MarkR
  • 215
  • 3
  • 10
  • 2
    https://en.cppreference.com/w/cpp/chrono/weekday/encoding – apple apple Oct 06 '21 at 19:37
  • 1
    @appleapple That looks like an answer to me :) – cigien Oct 06 '21 at 19:38
  • @appleapple This is exactly what I was looking for. I knew it would be simple. If you give this as an answer and you include the relevant function call in a code snippet, I will mark it as the answer. – MarkR Oct 06 '21 at 19:44

1 Answers1

3

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

apple apple
  • 10,292
  • 2
  • 16
  • 36