struct tm
来源:互联网 发布:vb中的sum是什么意思 编辑:程序博客网 时间:2024/05/17 03:01
今天我在做一项有关日期的作业时,使用到了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表示星期一
阅读全文
0 0
- struct tm
- struct tm 的应用
- struct tm 和 time_t
- struct tm 和 time_t
- struct tm 和 time_t
- struct tm 时间获取
- struct tm 和 time_t
- struct tm转time_t
- struct tm 的应用
- struct tm 与 time_t
- struct tm 的介绍
- struct tm *now测试
- struct tm *gmtime_r(time_t *timep, struct tm *result)
- 获取struct tm中的时间
- struct tm 和 time_t(zz)
- 【转】struct tm 和 time_t
- struct tm && ctime localtime mktime
- struct tm和time_t总结
- 使用git提交代码到github及git常用指令
- Struts2的优缺点总结
- uva 10340 All in All
- FFmpeg 3 跨平台视音频编解码入门基础
- java学习笔记
- struct tm
- 微信公众号开发(2)--开始开发
- LINUX设备驱动之触摸屏驱动
- android内存优化
- ForkJoin多核并行框架总结
- 【JDK】Java 8的元空间(metaspace)
- Storm应用系列之——最基本的例子
- Activity启动过程全解析
- XML的基础知识