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.
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';
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();
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) };
date::weekday weekday {now_local_in_days};
auto weekday_index = weekday.c_encoding();
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) };
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) };
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
.