Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
like image 625
rrz0 Avatar asked Oct 15 '25 17:10

rrz0


2 Answers

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.

like image 178
einpoklum Avatar answered Oct 17 '25 07:10

einpoklum


This would give the index of the day using date.h:

using namespace date;
using namespace std::chrono;
auto now = system_clock::now();
std::cout << "The current time is " << now << " UTC\n";
auto current_day = (year_month_weekday{floor<days>(now)}.weekday() - Sunday).count();
std::cout << "current_day " << current_day << "\n";

you can also use std::time_get::get_weekday

#include <iostream>       // std::cout, std::ios
#include <sstream>        // std::istringstream
#include <ctime>          // std::tm
#include <locale>         // std::locale, std::time_get, std::use_facet

int main ()
{
  std::locale loc;        // classic "C" locale

  // get time_get facet:
  const std::time_get<char>& tmget = std::use_facet <std::time_get<char> > (loc);

  std::ios::iostate state;
  std::istringstream iss ("Friday");
  std::tm when;

  tmget.get_weekday (iss, std::time_get<char>::iter_type(), iss, state, &when);

  std::cout << "weekday: " << when.tm_wday << '\n';
  return 0;
}

or:

time_t t = time(nullptr);
tm* timePtr = localtime(&t);
uint32_t y = timePtr->tm_year + 1900;
uint32_t m = timePtr->tm_mon + 1;
uint32_t d = timePtr->tm_wday;
cout<< d<<endl;
like image 43
Oblivion Avatar answered Oct 17 '25 05:10

Oblivion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!