How do I get the current day of week in C++ using date.h?

Rrz*_*rz0 2 c++ date dayofweek c++-chrono c++14

I am using C++ 14 and am trying to get the current day of week. After some reading I am using date.h by Howard E. Hinnant.

However I am struggling to get a day of the week (encoded as 0 thru 6).

Something like this prints out Thu:

 int main(void)
 {
     date::sys_days t;
     weekday wd{t};
     cout << wd << '\n';
 }
Run Code Online (Sandbox Code Playgroud)

Many answers on this site regarding getting the current day of the week use chrono.

How can I print the current weekday as a range from 0-6 according to when the program is run, using date.h?

For example, if I run the program today (Tuesday) I would expect a value of 2.

Any suggestions would be appreciated.


Just to be clear about what I am trying to achieve, something similar in Java:

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Europe/London"));
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
Run Code Online (Sandbox Code Playgroud)

ein*_*ica 5

The problem with your code is that t is not being initialized to anything interesting. You just need to set it to "now". There's a slight hitch, though: now() returns a different kind of time point, that can't automagically be converted into a date::sys_days value.

So, here's the minimal change to make your program work:

#include "date.h"
#include <iostream>
#include <cstdint>

int main()
{
     auto now = std::chrono::system_clock::now();
     date::sys_days t { std::chrono::time_point_cast<date::days>(now) };
     date::weekday wd{t};
     std::cout << wd << '\n';
}
Run Code Online (Sandbox Code Playgroud)

edit: Now let's do a bit more, and thanks @HowardHinnant for the informative comments.

Use more meaningful names

Replace the code for main() with

auto now = std::chrono::system_clock::now();
date::sys_days now_in_days { std::chrono::time_point_cast<date::days>(now) };
date::weekday weekday {now_in_days};
std::cout << weekday << '\n';
Run Code Online (Sandbox Code Playgroud)

Get the weekday as a number

You said you wanted to get the weekday as a number. Well, we can do that:

auto weekday_index = weekday.c_encoding();
Run Code Online (Sandbox Code Playgroud)

the type will be unsigned, and the value will be in the range 0...6

Get the weekday in your own timezone

Your code and mine, so far, is UTC. Which is good as a default, but may give you something surprising as the weekday. We can use zoned_time and write:

auto now = std::chrono::system_clock::now();
auto now_local = zoned_time{current_zone(), now}.get_local_time();
date::sys_days now_local_in_days { std::chrono::time_point_cast<date::days>(now_local) };
auto weekday_index = now_local_in_days.c_encoding();
Run Code Online (Sandbox Code Playgroud)

Gotcha for dates before the epoch (1970)

This is super annoying, but time_point_cast() may not always do what you want! Howard says that for days before 1970 we need to use floor() instead:

date::sys_days now_local_in_days { std::chrono::floor<date::days>(now_local) };
Run Code Online (Sandbox Code Playgroud)

Final program

#include "date.h"
#include <iostream>
#include <cstdint>

int main()
{
    auto now = std::chrono::system_clock::now();
    auto now_local = zoned_time{current_zone(), now}.get_local_time();
    date::sys_days now_local_in_days { std::chrono::floor<date::days>(now_local) };
    auto weekday_index = now_local_in_days.c_encoding();
    std::cout << weekday_index << '\n';
}
Run Code Online (Sandbox Code Playgroud)

And if today is Tuesday, the resulting output should be 2.

  • 更多信息和注意事项: 1) 您可以使用 wd.c_encoding() 将 `weekday` `wd` 转换为 [0, 6] 范围内的 `unsigned`。2) 上述公式返回 UTC 中的当前“工作日”。要获取本地时间中的当前“工作日”,请使用“zoned_time{current_zone(), now}.get_local_time()”将“now”转换为“local_time”,然后截断为“days”精度并转换为“weekday”。3) `time_point_cast` 将为您提供 1970 年之后所有时间的正确答案。为了确保所有时间点的正确答案,请使用“floor&lt;days&gt;”而不是“time_point_cast&lt;days&gt;”。 (2认同)