首页 > 代码库 > 一个Date类

一个Date类

写一个Date

a) 数据成员:year/month/day

b) 提供两个构造函数 

c) 实现功能setDate设置日期

d) 把时间设定为今天 setToday();

e) toString()  打印格式:2009/7/31

f) toFormatString:对于月份采用英文 September 19 2014

g) isLeapYear() 判断是否是闰年

h) calDayOfYear() 计算该日期是当年的第几天(注意闰年)

i) 写一个全局的函数int differenceDate(const Date &d1, const Date &d2);比较两个日期的差异(d1 - d2, 可以写为static函数

j) 写一个static函数today(),返回代表今天的Date对象。

 

 头文件:

 1 #ifndef DATE_H_ 2 #define DATE_H_  3  4 #include <string> 5  6 class Date 7 { 8     public: 9         Date();10         Date(int year, int month, int day);11         12         void assertValid() const;13         bool isValid() const;14         bool isLeapYear() const;15         int calDaysOfYear() const;16         int calLeftDaysOfYear() const;17         int compareDate(const Date &other) const;18 19         void setDate(int year, int month, int day);20         void setToday();21 22         std::string toString() const;23         std::string toFormatString() const;24 25 26         int getYear() const27         { return year_; }28         int getMonth() const29         { return month_; }30         int getDay() const31         { return day_; }32 33         static int differenceDate(const Date &t1, const Date &t2);34 35     private:36         int year_;37         int month_;38         int day_;39 40         static const int kDaysOfMonth[2][12];41         static const std::string kNameOfMonth[12];42 };43 44 #endif  /*DATE_H_*/
View Code

 实现:

  1 #include "Date.h"  2 #include <stdio.h>  3 #include <stdlib.h>  4 #include <time.h>  5 using namespace std;  6   7   8 const int Date::kDaysOfMonth[2][12] =   9 { 10     {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 11     {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} 12 }; 13  14 const string Date::kNameOfMonth[12] =  15 { 16     "January", "February", "March", "April", "May", "June", "July", 17     "August", "September", "October", "November", "December" 18 }; 19  20  21 Date::Date() 22     :year_(0), 23      month_(0), 24      day_(0) 25 { 26  27 } 28  29 Date::Date(int year, 30            int month, 31            int day) 32     :year_(year), 33      month_(month), 34      day_(day) 35 { 36 } 37  38 bool Date::isLeapYear() const 39 { 40     return (year_%4 == 0 && year_%100 != 0) || year_%400 == 0; 41 } 42  43 bool Date::isValid() const 44 { 45     return year_ > 0  46            && month_ > 0 47            && month_ <= 12 48            && day_ > 0 49            && day_ <= kDaysOfMonth[isLeapYear()][month_-1]; 50 } 51  52 void Date::assertValid() const 53 { 54     if(!isValid()) 55         abort(); 56 } 57  58 int Date::calDaysOfYear() const 59 { 60     assertValid(); 61     int leap = isLeapYear(); 62     int result = 0; 63     for(int i = 1; i < month_; ++i) 64     { 65         result += kDaysOfMonth[leap][i-1]; 66     } 67     result += day_; 68  69     return result; 70 } 71  72 int Date::calLeftDaysOfYear() const 73 { 74     bool leap = isLeapYear(); 75     int days = calDaysOfYear(); 76     if(leap == true) 77         return 366 - days; 78     else 79         return 365 - days; 80 } 81  82 int Date::compareDate(const Date &other) const 83 { 84     assertValid(); 85     other.assertValid(); 86  87     int res = year_ - other.year_; 88     if(res == 0) 89         res = month_ - other.month_; 90     if(res == 0) 91         res = day_ - other.day_; 92  93     return res; 94 } 95  96  97 void Date::setDate(int year, int month, int day) 98 { 99     year_ = year;100     month_ = month;101     day_ = day;102 }103 104 void Date::setToday()105 {106     time_t t = time(NULL);107     struct tm *pt = localtime(&t);108     if(pt == NULL)109     {110         perror("localtime");111         exit(EXIT_FAILURE);112     }113 114     year_ = pt->tm_year + 1900;115     month_ = pt->tm_mon + 1;116     day_ = pt->tm_mday;117 }118 119 string Date::toString() const120 {121     assertValid();122     //2009 7/31123     char text[100] = {0};124     snprintf(text, sizeof text, "%04d %02d %02d", year_, month_, day_);125     return string(text);126 }127 128 string Date::toFormatString() const129 {130     assertValid();131     //22 September 2014132     char text[1024] = {0};133     snprintf(text, sizeof text, "%d %s %04d", day_, kNameOfMonth[month_-1].c_str(), year_);134     return string(text);135 }136 137 138 int Date::differenceDate(const Date &d1, const Date &d2)139 {140     Date t1, t2;  //t1 < t2141     bool minus = false;142     int days = 0;143 144     int diff = d1.compareDate(d2);145     if(diff == 0)146         return 0;147     else if(diff < 0) //d1 < d2148     {149         t1 = d1;150         t2 = d2;151         minus = true;152     }153     else if(diff > 0)154     {155         t1 = d2;156         t2 = d1;157         minus = false;158     }159 160 161     //只需比较t1与t2162     //这里保证t1 < t2163     //只需计算出天数的差距164 165     if(t1.year_ == t2.year_) //同一年166         days = t2.calDaysOfYear() - t1.calDaysOfYear();167     else168     {169         days += t1.calLeftDaysOfYear(); //t1剩余的天数170         for(int i = t1.year_+1; i <= t2.year_-1 ; ++i)171         {172             bool leap = (i%4 == 0 && i%100 != 0) || i%400 == 0;173             days += (leap == true ? 366 : 365); 174         }175         days += t2.calDaysOfYear();   //t2的天数176     }177 178     if(minus == true)179         return -1 * days;180     else181         return days;182 }
View Code

 

 

 

 

 

 

 

 

 

 

一个Date类