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

5 Answers

0 votes
#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.

*/

 



answered 7 hours ago by avibootz
0 votes
#include <iostream>
#include <ctime>

/**
 * Checks if a given date is strictly in the future.
 * Uses legacy <ctime> and std::mktime (compatible with C++11/14/17).
 */
bool isDateInFutureLegacy(int year, int month, int day) {
    // 1. Set up the target date struct tm
    std::tm target_tm = {};
    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 (seconds since epoch)
    std::time_t target_time = std::mktime(&target_tm);
    if (target_time == -1) {
        std::cerr << "Error: Invalid calendar date provided.\n";
        return false;
    }

    // 2. Get current system time
    std::time_t now = std::time(nullptr);
    
    // 3. Normalize current time to midnight (00:00:00) for a fair day-only comparison
    std::tm* current_tm = std::localtime(&now);
    current_tm->tm_hour = 0;
    current_tm->tm_min = 0;
    current_tm->tm_sec = 0;
    current_tm->tm_isdst = -1;
    
    std::time_t today_midnight = std::mktime(current_tm);

    // 4. Compare timestamps
    return target_time > today_midnight;
}

int main() {
    int test_year = 2035;
    int test_month = 6;
    int test_day = 15;

    std::cout << "[Legacy] Checking date: " << test_year << "-" << test_month << "-" << test_day << "\n";

    if (isDateInFutureLegacy(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.\n";
    }
}


/*
run:

Checking date: 2035-6-15
Result: The date is in the FUTURE.

*/

 



answered 7 hours ago by avibootz
0 votes
#include <chrono>
#include <iostream>

// Returns true if the specified date/time is in the future
bool isFutureDate(const std::chrono::system_clock::time_point& date) {
    return date > std::chrono::system_clock::now();
}

int main()
{
    using namespace std::chrono;

    // Example: 24 hours in the future
    auto futureDate = system_clock::now() + hours(24);

    if (isFutureDate(futureDate)) {
        std::cout << "The date is in the future.\n";
    }
    else {
        std::cout << "The date is not in the future.\n";
    }
}


/*
run:

The date is in the future.

*/

 



answered 6 hours ago by avibootz
0 votes
#include <chrono>
#include <iostream>

// Returns true if the date is after today's date
bool isFutureDate(const std::chrono::year_month_day& date)
{
    auto today = std::chrono::floor<std::chrono::days>(
        std::chrono::system_clock::now());

    std::chrono::year_month_day currentDate{today};

    return date > currentDate;
}

int main()
{
    using namespace std::chrono;

    year_month_day tomorrow{
        floor<days>(system_clock::now()) + days{1}
    };

    if (isFutureDate(tomorrow)) {
        std::cout << "The date is in the future.\n";
    }
    else {
        std::cout << "The date is not in the future.\n";
    }
}


/*
run:

The date is in the future.

*/

 



answered 6 hours ago by avibootz
0 votes
#include <chrono>
#include <iostream>

bool isFutureDate(const std::chrono::year_month_day& date)
{
    auto today = std::chrono::year_month_day{
        std::chrono::floor<std::chrono::days>(
            std::chrono::system_clock::now())
    };

    return date > today;
}

int main()
{
    std::chrono::year_month_day date{
        std::chrono::year{2030},
        std::chrono::month{12},
        std::chrono::day{31}
    };

    std::cout << std::boolalpha
              << isFutureDate(date)
              << '\n';
}



/*
run:

true

*/

 



answered 5 hours ago by avibootz
...