| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 
 | # include <time.h>
# include <stdlib.h>
# include "Date.h"
 
using namespace std;
//**************************************************     CONSTRUCTION    ********************************************************
 
Date::Date(int d, int m, int y) {
   mDay   = d;
   mMonth = m;
   mYear = y;
};
Date::Date(const Date &d) {
   mDay   = d.mDay;
   mMonth = d.mMonth;
   mYear = d.mYear;
};
Date::Date() {
   mDay   = 1;
   mMonth = 1;
   mYear = 2000;
};
Date::~Date(){
};
 
 
//************************************************** GETTERS AND SETTERS ********************************************************
 
int Date::getDay () const {
return mDay ;
};
int Date::getMonth () const{
return mMonth ;
};
int Date::getYear () const {
return mYear ;
};
int Date::getIsValid () const {
return *mIsValid ;
};
int Date::getIsLeapYear () const {
return *mIsLeapYear ;
};
 
 
void Date::setDay   (const int d){
    mDay = d;
    *mIsValid=-1;
};
void Date::setMonth (const int m){
    mMonth = m;
    *mIsValid=-1;
};
void Date::setYear  (const int y){
    mYear = y;
    *mIsValid=-1;
    *mIsLeapYear=-1;
};
void Date::setIsValid  (const int v){
    *mIsValid=v;
};
void Date::setIsLeapYear  (const int v){
    *mIsLeapYear=v;
};
 
 
//**************************************************      OPERATORS     ********************************************************
 
Date Date::operator =(const Date &d) {
mDay =d.mDay;
mMonth =d.mMonth;
mYear =d.mYear;
return *this ;
};
Date Date::operator ++(int){ // postfix operator
   Date d = *this;
   *this = NextDate(d);
   return d;
};
Date Date::operator ++(){ // prefix operator
   *this = NextDate(*this);
   return *this;
};
Date Date::operator --(int){ // postfix operator, return current value
   Date d = *this;
   *this = PreviousDate(*this);
   return d;
};
Date Date::operator --(){ // prefix operator, return new value
   *this = PreviousDate(*this);
   return *this;
};
bool operator == (const Date& d1,const Date& d2){
    if (!d1.IsValid()) { return false; };
    if (!d2.IsValid()) { return false; };
    if( (d1.getDay()==d2.getDay())
	&& (d1.getMonth()==d2.getMonth())
	&& (d1.getYear()==d2.getYear())) {
	return true;
    };
    return false;
};
bool operator !=(const Date& d1, const Date& d2){
    return !(d1==d2);
};
bool operator < (const Date& d1, const Date& d2){
    if (!d1.IsValid()) { return false; }; // not  meaningful, return anything
    if (!d2.IsValid()) { return false; }; // should really be an exception, but ?
    if (d1.getYear()<d2.getYear())      { return true;}
    else if (d1.getYear()>d2.getYear()) { return false;}
    else {                            // same year
	if (d1.getMonth()<d2.getMonth())      { return true;}
	else if (d1.getMonth()>d2.getMonth()) { return false;}
	else { // same  month
	    if ( d1.getDay()<d2.getDay()) { return true;}
	    else { return false; }
	};
    };
    return false;
};
bool operator > (const Date& d1, const Date& d2) {
    if (d1==d2) { return false;};  // this is strict inequality
    if (d1<d2) { return false; };
    return true;
};
bool operator <=(const Date& d1, const Date& d2){
  if (d1==d2) { return true; }
  return (d1<d2);
};
bool operator >=(const Date& d1, const Date& d2) {
    if (d1==d2) { return true;};
    return (d1>d2);
};
Date operator + (const Date& d, const int& days){ // IMPROVE!
    if (!d.IsValid()){return Date();};
    if (days<0) {return (d-(-days));};
    Date d1=d;
    for (int day=1;day<=days;++day){ d1=NextDate(d1); };
    return d1;
};
Date operator += (Date& d, const int& days){
    d = (d+days);
    return d;
};
Date operator - (const Date& d, const int& days) {
    if (!d.IsValid()) { return Date();};
    if (days<0)     { return (d+(-days));};
    Date d1=d;
    for (int day=0; day<days; ++day){ d1=PreviousDate(d1); }; // IMPROVE!
    return d1;
};
Date operator -= (Date& d, const int& days){
    return (d-days);
};
 
//**************************************************       METHODS      ********************************************************
bool Date::IsLeapYear () const {
    if (*mIsLeapYear==-1) {
        if (mYear%4 != 0){
            *mIsValid=0;
            return false;
        }
        else if (mYear%100 != 0){
            *mIsValid=1;
            return true;
        }
        else if (mYear%400 != 0){
            *mIsValid=0;
            return false;
        }
        else {
            *mIsValid=1;
            return true;
        }
    }
    if (*mIsLeapYear==0) return false;
    else return true;
};
bool Date::IsValid() const {
    if (*mIsValid==-1) {
        if (mMonth>12 || mMonth<1) {
            *mIsValid=0;
            return false;
        }
        if (mDay>31 || mDay<1){
            *mIsValid=0;
            return false;
        }
        if ((mDay==31 && ( mMonth==2 || mMonth==4 || mMonth==6 ||mMonth==9 || mMonth==11) ) ) {
            *mIsValid=0;
            return false;
        }
        if (mDay==30 && mMonth==2){
            *mIsValid=0;
            return false;
        }
        if ((mDay==29 && mMonth==2) && (IsLeapYear()==false)){
            *mIsValid=0;
            return false;
        }
        *mIsValid=1;
        return true;
    }
    if (*mIsValid==0) return false;
    else return true;
};
int IntDate(const Date& d) {
    if (d.IsValid()) { return d.getYear() * 10000 + d.getMonth() * 100 + d.getDay();  };
    return -1;
};
Date NextDate(const Date& d){
    Date ndat;
    if (!d.IsValid()) { return ndat; };
    ndat=Date((d.getDay()+1),d.getMonth(),d.getYear());  if (ndat.IsValid()) return ndat;
    ndat=Date(1,(d.getMonth()+1),d.getYear());      if (ndat.IsValid()) return ndat;
    ndat = Date(1,1,(d.getYear()+1));          return ndat;
};
Date PreviousDate(const Date& d){
    Date ndat;
    if (!d.IsValid()) { return ndat; };
    ndat = Date((d.getDay()-1),d.getMonth(),d.getYear());  if (ndat.IsValid()) return ndat;
    ndat = Date(31,(d.getMonth()-1),d.getYear());     if (ndat.IsValid()) return ndat;
    ndat = Date(30,(d.getMonth()-1),d.getYear());     if (ndat.IsValid()) return ndat;
    ndat = Date(29,(d.getMonth()-1),d.getYear());     if (ndat.IsValid()) return ndat;
    ndat = Date(28,(d.getMonth()-1),d.getYear());     if (ndat.IsValid()) return ndat;
    ndat = Date(31,12,(d.getYear()-1));          return ndat;
}; | 
Partager