struct tm

本文探讨了在C++中实现日期类时遇到的问题,特别是使用struct tm进行日期加减运算时年份显示异常的原因及解决方法。

 今天我在做一项有关日期的作业时,使用到了struct tm 也因此发现了一些问题。
以下为详例:


class Date
{
public:
    Date() {}
    Date(int n_year, int n_month, int n_day)
    {
        if (!DateFormIsCorrect(n_year, n_month, n_day))
        {
            cout << "Construction failed, the date format is incorrect!\n";
            return;
        }
        mn_year = n_year;
        mn_month = n_month;
        mn_day = n_day;
    }
    friend ostream& operator << (ostream &out, Date &date);
    Date operator + (int n_day);
    Date operator - (int n_day);
    int operator - (Date &date);
private:
    bool DateFormIsCorrect(int n_year, int n_month, int n_day);
    int mn_year, mn_month, mn_day;
};

程序要求定义一个Date类,比如:

Date operator + (int n_day);

表示返回加上 n_day 天后的日期,struct tm后的原始实现如下:

Date Date::operator + (int n_addDay)
{
    time_t t(0);
    struct tm *ptm = localtime(&t);
    ptm->tm_year = mn_year;//注意这里
    ptm->tm_mon = mn_month;
    ptm->tm_mday = mn_day;
    t = mktime(ptm);
    t += n_addDay * 24 * 3600;
    ptm = localtime(&t);

    Date Date_new;
    Date_new.mn_year = ptm->tm_year;//注意这里
    Date_new.mn_month = ptm->tm_mon;
    Date_new.mn_day = ptm->tm_mday;
    return Date_new;
}

 程序运行后发现年份输出会有问题,比如如果执行以下内容

Date date_a(1997, 2, 23);
cout << date_a;
cout << "过了5天后是:" << (date_a + 5);

 输出结果:70年2月28日
 可以发现年份是有问题的,这是因为 struct tm 中的成员定义:

struct tm {
  int tm_sec; /* 秒–取值区间为[0,59] */
  int tm_min; /* 分 - 取值区间为[0,59] */
  int tm_hour; /* 时 - 取值区间为[0,23] */
  int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
  int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
  int tm_year; /* 年份,其值从1900开始 */
  int tm_wday; /* 星期–取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
  int tm_yday; /* 从每年的1月1日开始的天数–取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
  int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
  };

我们发现,tm_year所代表的并不是年份而是从1900年开始过了几年。
也就是说,tm_year = 年份 - 1900

另外还有两个要注意的
1. 月份从0开始的,0表示一月,即month = 1+p->tm_mon
2. 星期也是从0开始的, 0表示星期日,1表示星期一

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值