You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++设置截止日期实现功能限制:字符串日期比较失效问题

C++截止日期控制功能修复方案

问题描述

需要实现截止日期控制:当当前日期超过指定日期时,提名功能无法运行。但现有代码直接进入else分支,无法正常执行提名逻辑。

错误原因

代码中直接用ctime(&ct)返回的字符串与enddate常量比较,存在核心逻辑错误:

  • ctime()返回的字符串格式为**"星期 月份 日 时:分:秒 年份"**(例如"Wed Dec 28 14:30:00 2022"),与enddate的"YYYY-MM-DD"格式完全不匹配。
  • 字符串比较是按ASCII码逐字符对比,两种格式的首字符就存在差异,导致currenttime < enddate永远不成立,直接触发else分支。

修正方案

方案1:基于日期数值比较(推荐)

将当前时间转换为tm结构体,提取年、月、日的数值,与截止日期的数值直接比较,逻辑更可靠。

修正后的完整代码:

#include<iostream>
#include<iomanip>
#include<string>
#include<stdlib.h>
#include<time.h>

// 定义截止日期的年、月、日
#define END_YEAR 2022
#define END_MONTH 12
#define END_DAY 30

using namespace std;

int nominationMenu() {
    time_t ct = time(0);
    tm* current_tm = localtime(&ct); // 转换为本地时间结构体

    // 提取当前日期的年、月、日(注意tm结构体的偏移量)
    int current_year = current_tm->tm_year + 1900;
    int current_month = current_tm->tm_mon + 1;
    int current_day = current_tm->tm_mday;

    // 判断当前日期是否在截止日期之前(含当天)
    bool is_nomination_available = false;
    if (current_year < END_YEAR) {
        is_nomination_available = true;
    } else if (current_year == END_YEAR) {
        if (current_month < END_MONTH) {
            is_nomination_available = true;
        } else if (current_month == END_MONTH) {
            if (current_day <= END_DAY) {
                is_nomination_available = true;
            }
        }
    }

    string candid_mykad, candidID;
    string program;
    int candidstudyyear;

    if (is_nomination_available) {
        cout << setw(49) << setfill('-') << "-" << endl;
        cout << setw(50) << setfill(' ') << left << "\n                NOMINATION SESSION" << endl;
        cout << setw(50) << setfill('-') << "\n-" << endl;
        cout << "PLease keep in mind that the voting session will only begin after the 26th of December." << endl;
        cout << "One candidate per student.\n" << endl;
        system("pause");
        cout << "Please enter candidate's information:" << endl;
        cout << "\nMyKad (without dashes '-') \t:" << endl;
        cin >> candid_mykad;
        cout << "Student ID (full alphanumeric e.g. 22WMR12345) \t: " << endl;
        cin >> candidID;
        cout << "Program \t:" << endl;
        cin.ignore(1000, '\n');
        getline(cin, program);
        cout << "Year of study \t:" << endl;
        cin >> candidstudyyear;

        cout << "\nYou have nominated a candidate." << endl;
    }
    else {
        cout << "Nomination session has ended. Please proceed to the voting session." << endl;
    }

    return 0;
}

方案2:格式化字符串后比较

将当前时间格式化为YYYY-MM-DD的字符串,与enddate常量格式统一后再比较:

#include<iostream>
#include<iomanip>
#include<string>
#include<stdlib.h>
#include<time.h>
#define enddate "2022-12-30"
using namespace std;

int nominationMenu() {
    time_t ct = time(0);
    char current_date_buf[11];
    // 将当前时间格式化为YYYY-MM-DD字符串
    strftime(current_date_buf, sizeof(current_date_buf), "%Y-%m-%d", localtime(&ct));
    string current_date(current_date_buf);

    string candid_mykad, candidID;
    string program;
    int candidstudyyear;

    if (current_date <= enddate) {
        cout << setw(49) << setfill('-') << "-" << endl;
        cout << setw(50) << setfill(' ') << left << "\n                NOMINATION SESSION" << endl;
        cout << setw(50) << setfill('-') << "\n-" << endl;
        cout << "PLease keep in mind that the voting session will only begin after the 26th of December." << endl;
        cout << "One candidate per student.\n" << endl;
        system("pause");
        cout << "Please enter candidate's information:" << endl;
        cout << "\nMyKad (without dashes '-') \t:" << endl;
        cin >> candid_mykad;
        cout << "Student ID (full alphanumeric e.g. 22WMR12345) \t: " << endl;
        cin >> candidID;
        cout << "Program \t:" << endl;
        cin.ignore(1000, '\n');
        getline(cin, program);
        cout << "Year of study \t:" << endl;
        cin >> candidstudyyear;

        cout << "\nYou have nominated a candidate." << endl;
    }
    else {
        cout << "Nomination session has ended. Please proceed to the voting session." << endl;
    }

    return 0;
}

注意事项

  • 多线程环境下,建议使用线程安全的时间转换函数:Windows用localtime_s,Linux用localtime_r,避免localtime的线程安全问题。
  • 方案2中必须确保strftime的格式字符串与enddate的格式完全一致,否则字符串比较会失效。
  • 数值比较的方式更不易出错,推荐作为首选方案。

内容的提问来源于stack exchange,提问作者Jayren

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 15:50:15