#include <iostream>
#include <ctime>
#include <string>
/*
This program computes the day of the week for January 1st of a given year.
Approach:
---------
1. Fill a std::tm structure with the desired date: January 1st of the input year.
2. Call std::mktime, which normalizes the structure and computes the calendar fields.
3. Read tm_wday (0 = Sunday, 1 = Monday, ..., 6 = Saturday).
4. Convert tm_wday to a human-readable string.
This uses the standard library's built-in date/time functions,
avoiding manual calendar arithmetic while remaining portable and efficient.
*/
// Convert tm_wday (0..6) to a weekday name
std::string weekday_to_string(int wday) {
switch (wday) {
case 0: return "Sunday";
case 1: return "Monday";
case 2: return "Tuesday";
case 3: return "Wednesday";
case 4: return "Thursday";
case 5: return "Friday";
case 6: return "Saturday";
default: return "Unknown";
}
}
// Compute weekday of January 1st for a given year
std::string jan1_weekday(int year) {
std::tm date{};
// Years since 1900
date.tm_year = year - 1900;
// January (0-based)
date.tm_mon = 0;
// Day of month
date.tm_mday = 1;
// Let mktime fill in the rest (tm_wday, etc.)
std::mktime(&date);
return weekday_to_string(date.tm_wday);
}
int main() {
int year = 2026;
std::string result = jan1_weekday(year);
std::cout << "January 1st, " << year << " falls on a " << result << ".\n";
}
/*
run:
January 1st, 2026 falls on a Thursday.
*/