How to check if a date is in the future with C

5 Answers

0 votes
#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(&current_tm, &now); // Windows thread-safe version
#else
    localtime_r(&now, &current_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(&current_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.

*/

 



answered 7 hours ago by avibootz
0 votes
#include <stdio.h>
#include <time.h>
#include <stdbool.h>

/**
 * Checks if a date is in the future using an integer scalar format (YYYYMMDD).
 * This completely avoids timezone shifts and DST issues inherent in mktime.
 */
bool is_date_in_future_scalar(int year, int month, int day) {
    // 1. Convert target date into a flat integer (e.g., 20350615)
    int target_date_int = (year * 10000) + (month * 100) + day;

    // 2. Get current system time and break it down via local time
    time_t now = time(NULL);
    if (now == (time_t)-1) {
        return false;
    }

    struct tm current_tm;
#if defined(_MSC_VER) || defined(__MINGW32__)
    localtime_s(&current_tm, &now);
#else
    localtime_r(&now, &current_tm);
#endif

    // 3. Extract current year, month, and day into the same integer format
    int current_year = current_tm.tm_year + 1900;
    int current_month = current_tm.tm_mon + 1;
    int current_day = current_tm.tm_mday;

    int current_date_int = (current_year * 10000) + (current_month * 100) + current_day;

    // 4. Direct integer comparison
    return target_date_int > current_date_int;
}

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_scalar(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.

*/

 



answered 7 hours ago by avibootz
0 votes
#include <stdio.h>
#include <time.h>
#include <stdbool.h>

/*
 * Returns true if the specified date/time is in the future.
 */
bool is_future_date(time_t date) {
    return date > time(NULL);
}

int main(void)
{
    /* Current time */
    time_t now = time(NULL);

    /* Create a date 24 hours in the future */
    time_t future_date = now + (24 * 60 * 60);

    if (is_future_date(future_date)) {
        printf("The date is in the future.\n");
    }
    else {
        printf("The date is not in the future.\n");
    }

    return 0;
}


/*
run:

The date is in the future.

*/

 



answered 5 hours ago by avibootz
0 votes
#include <stdio.h>
#include <time.h>
#include <stdbool.h>

/*
 * Returns true if the specified date/time is in the future.
 */
bool is_future_date(time_t date) {
    return date > time(NULL);
}

int main(void)
{
    struct tm date = {0};

    /* 31 December 2030 */
    date.tm_year = 2030 - 1900; /* Years since 1900 */
    date.tm_mon  = 11;          /* December = 11 */
    date.tm_mday = 31;

    time_t target_date = mktime(&date);

    if (target_date == (time_t)-1) {
        printf("Invalid date.\n");
        return 1;
    }

    if (is_future_date(target_date)) {
        printf("The date is in the future.\n");
    }
    else {
        printf("The date is not in the future.\n");
    }

    return 0;
}



/*
run:

The date is in the future.

*/

 



answered 5 hours ago by avibootz
0 votes
#include <stdio.h>
#include <time.h>
#include <stdbool.h>

/*
 * Returns true if the specified date is after today's date.
 * Time-of-day is ignored.
 */
bool is_future_date_only(time_t date)
{
    time_t now = time(NULL);

    struct tm target = *localtime(&date);
    struct tm today  = *localtime(&now);

    /* Remove time components */
    target.tm_hour = target.tm_min = target.tm_sec = 0;
    today.tm_hour  = today.tm_min  = today.tm_sec  = 0;

    return mktime(&target) > mktime(&today);
}

int main(void)
{
    time_t now = time(NULL);

    /* Tomorrow */
    time_t tomorrow = now + (24 * 60 * 60);

    if (is_future_date_only(tomorrow)) {
        printf("The date is in the future.\n");
    }
    else {
        printf("The date is not in the future.\n");
    }

    return 0;
}


/*
run:

The date is in the future.

*/

 



answered 5 hours ago by avibootz
...