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';
}
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);
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';
}
edit: Now let's do a bit more, and thanks @HowardHinnant for the informative comments.
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';
You said you wanted to get the weekday as a number. Well, we can do that:
auto weekday_index = weekday.c_encoding();
the type will be unsigned
, and the value will be in the range 0...6
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) };
date::weekday weekday {now_local_in_days};
auto weekday_index = weekday.c_encoding();
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) };
#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) };
date::weekday weekday {now_local_in_days};
auto weekday_index = weekday.c_encoding();
std::cout << weekday_index << '\n';
}
And if today is Tuesday, the resulting output should be 2
.