#include <stdio.h>
#include <time.h>
#include <stdbool.h>
/**
* Checks if a given date is strictly in the future compared to today.
* Uses standard <time.h> and mktime().
*/
bool is_date_in_future(int year, int month, int day) {
// 1. Set up target date in struct tm
struct tm target_tm = {0};
target_tm.tm_year = year - 1900; // Years since 1900
target_tm.tm_mon = month - 1; // Months are 0-indexed (0 = January, 11 = December)
target_tm.tm_mday = day; // Day of the month (1-31)
target_tm.tm_isdst = -1; // Let mktime determine Daylight Saving Time rules
// Convert target struct tm to time_t
time_t target_time = mktime(&target_tm);
if (target_time == (time_t)-1) {
fprintf(stderr, "Error: Invalid calendar date provided.\n");
return false;
}
// 2. Get current system time
time_t now = time(NULL);
if (now == (time_t)-1) {
fprintf(stderr, "Error: Failed to obtain system time.\n");
return false;
}
// 3. Normalize current time to midnight (00:00:00) for a clean day comparison
struct tm current_tm;
#if defined(_MSC_VER) || defined(__MINGW32__)
localtime_s(¤t_tm, &now); // Windows thread-safe version
#else
localtime_r(&now, ¤t_tm); // POSIX thread-safe version
#endif
current_tm.tm_hour = 0;
current_tm.tm_min = 0;
current_tm.tm_sec = 0;
current_tm.tm_isdst = -1;
time_t today_midnight = mktime(¤t_tm);
// 4. Compare timestamps
return target_time > today_midnight;
}
int main(void) {
int test_year = 2035;
int test_month = 6;
int test_day = 15;
printf("Checking date: %d-%02d-%02d\n", test_year, test_month, test_day);
if (is_date_in_future(test_year, test_month, test_day)) {
printf("Result: The date is in the FUTURE.\n");
} else {
printf("Result: The date is NOT in the future.\n");
}
return 0;
}
/*
run:
Checking date: 2035-06-15
Result: The date is in the FUTURE.
*/