As reported also by Wikipedia, in 1990 Michael Keith and Tom Craver published an expression to minimise the number of keystrokes needed to enter a self-contained function for converting a Gregorian date into a numerical day of the week.
The expression does preserve neither y nor d, and returns a zero-based index representing the day, starting with Sunday, i.e. if the day is Monday the expression returns 1.
A code example which uses the expression follows:
int d = 15 ; //Day 1-31
int m = 5 ; //Month 1-12`
int y = 2013 ; //Year 2013`
int weekday = (d += m < 3 ? y-- : y - 2, 23*m/9 + d + 4 + y/4- y/100 + y/400)%7;
The expression uses the comma operator, as discussed in this answer.
Enjoy! ;-)
Answer from Thats it. on Stack OverflowAs reported also by Wikipedia, in 1990 Michael Keith and Tom Craver published an expression to minimise the number of keystrokes needed to enter a self-contained function for converting a Gregorian date into a numerical day of the week.
The expression does preserve neither y nor d, and returns a zero-based index representing the day, starting with Sunday, i.e. if the day is Monday the expression returns 1.
A code example which uses the expression follows:
int d = 15 ; //Day 1-31
int m = 5 ; //Month 1-12`
int y = 2013 ; //Year 2013`
int weekday = (d += m < 3 ? y-- : y - 2, 23*m/9 + d + 4 + y/4- y/100 + y/400)%7;
The expression uses the comma operator, as discussed in this answer.
Enjoy! ;-)
A one-liner is unlikely, but the strptime function can be used to parse your date format and the struct tm argument can be queried for its tm_wday member on systems that modify those fields automatically (e.g. some glibc implementations).
int get_weekday(char * str) {
struct tm tm;
memset((void *) &tm, 0, sizeof(tm));
if (strptime(str, "%d-%m-%Y", &tm) != NULL) {
time_t t = mktime(&tm);
if (t >= 0) {
return localtime(&t)->tm_wday; // Sunday=0, Monday=1, etc.
}
}
return -1;
}
Or you could encode these rules to do some arithmetic in a really long single line:
- 1 Jan 1900 was a Monday.
- Thirty days has September, April, June and November; all the rest have thirty-one, saving February alone, which has twenty-eight, rain or shine, and on leap years, twenty-nine.
- A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
EDIT: note that this solution only works for dates after the UNIX epoch (1970-01-01T00:00:00Z).
As reported also by Wikipedia, in 1990 Michael Keith and Tom Craver published an expression to minimise the number of keystrokes needed to enter a self-contained function for converting a Gregorian date into a numerical day of the week.
The expression does preserve neither y nor d, and returns a zero-based index representing the day, starting with Sunday, i.e. if the day is Monday the expression returns 1.
A code example which uses the expression follows:
int d = 15 ; //Day 1-31
int m = 5 ; //Month 1-12`
int y = 2013 ; //Year 2013`
int weekday = (d += m < 3 ? y-- : y - 2, 23*m/9 + d + 4 + y/4- y/100 + y/400)%7;
The expression uses the comma operator, as discussed in this answer.
Enjoy! ;-)
Answer from Thats it. on Stack OverflowFirst: Do not write your own function, if there already are standardized functions that can handle the same problem. Point is that you might easily make a mistake (and I can already see one in the first line of your weekday() function as it is now), whereas implementations of standardized functions have been tested thoroughly and you can be confident that they deliver the result you are expected to get.
That being said, here is a possible approach using std::localtime and std::mktime:
#include <ctime>
#include <iostream>
int main()
{
std::tm time_in = { 0, 0, 0, // second, minute, hour
9, 10, 2016 - 1900 }; // 1-based day, 0-based month, year since 1900
std::time_t time_temp = std::mktime(&time_in);
//Note: Return value of localtime is not threadsafe, because it might be
// (and will be) reused in subsequent calls to std::localtime!
const std::tm * time_out = std::localtime(&time_temp);
//Sunday == 0, Monday == 1, and so on ...
std::cout << "Today is this day of the week: " << time_out->tm_wday << "\n";
std::cout << "(Sunday is 0, Monday is 1, and so on...)\n";
return 0;
}
New answer for old question because the tools they are a changing...
The C++20 spec says the following will have identical functionality to the intention of the code in the question:
#include <chrono>
#include <format>
#include <iostream>
int
main()
{
using namespace std;
using namespace std::chrono;
year_month_day dmy;
cin >> parse("%d %m %Y", dmy);
cout << format("{:%A}", weekday{dmy}) << '\n';
}
One can experiment today with this syntax by using this free, open-source date/time library, except that the date objects are in namespace date instead of namespace std::chrono, and the syntax of the format string is slightly altered.
#include "date/date.h"
#include <iostream>
int
main()
{
using namespace std;
using namespace date;
year_month_day dmy;
cin >> parse("%d %m %Y", dmy);
cout << format("%A", weekday{dmy}) << '\n';
}