.h文件
/***
* bate:1.1.1
* use:Date class
* aythor: haohdq
*
***/
#pragma once
#ifndef _LIBCPP_IOSTREAM
#include <iostream>
#endif
#ifndef _LIBCPP_STRING
#include <string>
#endif
#ifndef _TIME_H_
#include <time.h>
#endif
#ifndef _DATE_H_
#define _DATE_H_
class Date{
public:
Date(int y,int m,int d)
:y(y),m(m),d(d){
set(y,m,d);
}
explicit Date(const int yyy=19900101){
int yy=yyy;
y=yy/10000;
yy%=10000;
m=yy/100;
d=yy%100;
}
Date(const std::string h){
int tmp=0;
for(int i=0;i<h.length();i++){
while(h[i]>='0'&&h[i]<='9'&&i<h.length()){
tmp+=h[i]-'0';
tmp*=10;
i++;
}
}
tmp/=10;
int yy=tmp;
int y1=yy/10000;yy%=10000;
int m1=yy/100;
int d1=yy%100;
set(y1,m1,d1);
}
int y_day()const{
Date a=*this;
a.set_m(1);
a.set_d(1);
return *this-a+1;
}
int ny_day()const{
return 365+is_p_year()-y_day();
}
int get_year()const{
return y;
}
int get_month()const{
return m;
}
int get_day()const{
return d;
}
int week_num()const{//求星期幾
int yy=y,mm=m;
if(mm==1||mm==2)mm+=12;
int c=yy/100;
yy%=100;
return (yy+yy/4+c/4-2*c+26*(mm+1)/10+d-1)%7;
}
bool is_p_year()const{
return y%400?(y%100?(y%4?0:1):0):1;
}
bool is_p_year(const int yy)const{
return yy%400?(yy%100?(yy%4?0:1):0):1;
}
void set(int yy,int mm,int dd){
if(yy<=0)yy=1990;
else y=yy;
if(mm<=0||mm>12)m=1;
else m=mm;
if(dd<=0||dd>DayforMonth(y,m))d=1;
else d=dd;
}
void set_y(int yy){
set(yy,m,d);
}
void set_m(int mm){
set(y,mm,d);
}
void set_d(int dd){
set(y,m,dd);
}
void set_now(){
time_t curtime=time(0);
tm tim =*localtime(&curtime);
set(tim.tm_year+1900,tim.tm_mon+1,tim.tm_mday);
}
Date& operator += (const int c);
Date& operator -= (const int c);
friend Date operator + (const int v,const Date a);
friend Date operator + (const Date a,const int v);
Date operator ++(int);
Date& operator ++();
friend Date operator - (const Date a,const int v);
friend int operator - (const Date a,const Date b);
Date operator --(int);
Date& operator --();
friend bool operator > (const Date a,const Date b);
friend bool operator >=(const Date a,const Date b);
friend bool operator < (const Date a,const Date b);
friend bool operator <=(const Date a,const Date b);
friend bool operator ==(const Date a,const Date b);
friend bool operator !=(const Date a,const Date b);
friend std::ostream & operator <<(std::ostream &os,const Date &a);
friend std::istream & operator >>(std::istream &is,Date &a);
private:
int y,m,d;
int DayforMonth(int y,int m)const;
int ToInt(int yy,int mm)const;
void r_set();
};
#endif
<pre name="code" class="cpp">/***
* bate:1.1.1
* use:Date class
* aythor: haohdq
*
***/<pre name="code" class="cpp">/***
日期類
***/
#include <iostream>
#include "date.h"
// 得到每個月的天數
int Date::DayforMonth(int y,int m)const{
int d=0;
switch(m){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
d=31;
break;
case 4:
case 6:
case 9:
case 11:
d=30;
break;
case 2:
d=28+is_p_year(y);
break;
}
return d;
}
int Date::ToInt(int yy=1,int mm=1)const{
int sum=0;
for(int i=yy;i<y;i++){
sum+=365+is_p_year(i);
}
for(int i=mm;i<m;i++){
sum+=DayforMonth(y,m);
}
sum+=d;
return sum;
}
void Date::r_set(){
while(d>DayforMonth(y,m)){//大於
d-=DayforMonth(y,m);
m++;
if(m>12){
++(y);
m=1;
}
}
while(d<1){
m--;
if(m<1){
y--;
m=12;
}
d+=DayforMonth(y,m);
}
}
////////////日期加天數
Date operator + (const int v,const Date a){
Date d=a;
if(v==0) return d;
if(v>0){
d.d+=v;
d.r_set();
return d;
}
return d-(-v);
}
Date operator +(const Date a,const int v){
return v+a;
}
Date& Date::operator +=(const int v){
if(v==0)return *this;
if(v>0){
d+=v;
r_set();
return *this;
}
else return *this-=(-v);
}
Date& Date::operator -=(const int v){
if(v==0)return *this;
if(v>0){
d-=v;
r_set();
return *this;
}
else return *this+=(-v);
}
Date& Date::operator ++(){
d++;
r_set();
return *this;
}
Date Date::operator ++(int){
Date b=*this;
d++;
r_set();
return b;
}
Date operator -(const Date a,const int v){
Date b=a;
if(v==0)return b;
if(v>0){
b.d--;
b.r_set();
return b;
}
else return b+(-v);
}
int operator-(const Date a,const Date b){
return a.ToInt()-b.ToInt();
}
Date& Date::operator --(){
d--;
r_set();
return *this;
}
Date Date::operator --(int){
Date b=*this;
d--;
r_set();
return b;
}
bool operator ==(const Date a,const Date b){
return a.ToInt()==b.ToInt();
}
bool operator !=(const Date a,const Date b){
return !(a==b);
}
bool operator >(const Date a,const Date b){
return a.ToInt()>b.ToInt();
}
bool operator >=(const Date a,const Date b){
return a>b||a==b;
}
bool operator <(const Date a,const Date b){
return !(a>=b);
}
bool operator <=(const Date a,const Date b){
return a<b||a==b;
}
std::ostream& operator <<(std::ostream &os,const Date &a){
os<<a.y<<'-'<<a.m<<'-'<<a.d;
return os;
}
std::istream& operator >>(std::istream &in,Date &a){
std::string h;
in>>h;
Date b(h);
a=b;
return in;
}