#include <iostream>
#include <chrono>
/**
* Checks if a given date is strictly in the future compared to today.
* Uses C++20 std::chrono::year_month_day.
*/
bool isDateInFuture(int year, unsigned int month, unsigned int day) {
// 1. Construct the target date using C++20 chrono components
std::chrono::year_month_day target_ymd{
std::chrono::year{year},
std::chrono::month{month},
std::chrono::day{day}
};
// 2. Validate the date (e.g., checks if Feb 30 or invalid months are passed)
if (!target_ymd.ok()) {
std::cerr << "Error: Invalid calendar date provided.\n";
return false;
}
// 3. Convert year_month_day to sys_days (days since epoch 1970-01-01)
std::chrono::sys_days target_sys_days{target_ymd};
// 4. Get today's system clock time and floor it to whole days (stripping out hours/minutes/seconds)
auto now = std::chrono::system_clock::now();
auto today_sys_days = std::chrono::floor<std::chrono::days>(now);
// 5. Compare the target date days with today's date days
return target_sys_days > today_sys_days;
}
int main() {
// Example test values
int test_year = 2035;
unsigned int test_month = 6;
unsigned int test_day = 15;
std::cout << "Checking date: " << test_year << "-" << test_month << "-" << test_day << "\n";
if (isDateInFuture(test_year, test_month, test_day)) {
std::cout << "Result: The date is in the FUTURE.\n";
} else {
std::cout << "Result: The date is NOT in the future (it is today or in the past).\n";
}
}
/*
run:
Checking date: 2035-6-15
Result: The date is in the FUTURE.
*/