Professional Documents
Culture Documents
1 Setting up.
Compile date.cc, put into library libdate.a, make library available.
Put date.h on header le path.
2 Dening dates.
Examples of date denitions
date d;
date d(19930624)
date d(24,6,1993) Note the day, month, year sequence.
3 Operations on dates.
Let d, d1, d2 be dates.
The following operations are dened:
Iteration: d++, ++d, d, d .
Logical comparisions >, >=, <, <=, ! =, ==.
Some aritmetric operations are dened, which has a meaning that may seem strange at rst, but they
are natural for usage purposes.
Adding/Subtracting x number of days from a data:
d=d1+7 gives d the date 7 days after date d1.
d=d1-7 gives d the date 7 days before date d1.
Finding the number of days between 2 dates. i=d1-d2. Here i is an integer that is the number of days
between d1 and d2. i will be negative if d1 < d2.
Example:
date d1(30,6,1993); date d2(19930610);
i = d1-d2
Gives i the value 20, the number of days between 10 jun 93 and 30 jun 93.
4 date.h
Header le that denes the class and its procedures
// le: date.h
1
// author: Bernt A Oedegaard.
#ifndef DATE H
#dene DATE H
#include <iostream>
#include <fstream>
#include <string>
using namespace std; 10
class date {
protected:
int year ;
int month ;
int day ;
public:
date();
ostream& operator << ( ostream& os, const date& d); // output operator
long long date( const date&); // calculations 50
date next date( const date&);
date previous date(const date&);
2
inline void date::set day ( const int& day) { date::day = day; };
inline void date::set month( const int& month) { date::month = month; };
inline void date::set year (const int& year) { date::year = year; };
70
#endif
5 date.cc
Implementation
day = d;
month = m;
if (y<100) {
if (y<25){ // the simplest possible x for Y2K, choose 2K if year < 20 (unusual
// that dates before 1930 is referred to
year = 2000+y;
} 20
else {
year = 1900+y;
};
} // assume XX is shorthand for 19XX
else { year = y;};
};
year = date/10000;
if (year < 100) { year = 1900+year ;}; /* assume XX is shorthand for 19XX 30
(if >2000 presumably uses all four) */
month = (date%10000)/100;
day = date%100;
}
return false;
if ( day ==30 && month ==2) return false;
if ( year <2000){
if ((day ==29 && month ==2) && !((year −1900)%4==0)) return false;
};
if ( year >2000){ 50
if ((day ==29 && month ==2) && !((year −2000)%4==0)) return false;
};
return true;
3
};
return d; 60
}
date ndat;
return os;
}
return d1;
}
4
d = (d+days);
return d; 120
};
return (d−days);
}
if (no months>0) {
if (d.month()+nmonths<=12) { 150
ndat=date((d.day()),d.month()+nmonths,d.year());
ndat=date(d.day() −1,(d.month()+nmonths),d.year());
if (ndat.valid()) return ndat;
ndat=date(d.day() −2,(d.month()+nmonths),d.year());
if (ndat.valid()) return ndat;
ndat=date(d.day() −3,(d.month()+nmonths),d.year());
if (ndat.valid()) return ndat;
}
else { 160
ndat=date((d.day()),d.month()+nmonths −12,d.year()+1);
if (ndat.valid()) return ndat;
ndat=date((d.day() −1),d.month()+nmonths−12,d.year()+1);
if (ndat.valid()) return ndat;
ndat=date((d.day() −2),d.month()+nmonths−12,d.year()+1);
if (ndat.valid()) return ndat;
ndat=date((d.day() −3),d.month()+nmonths−12,d.year()+1);
if (ndat.valid()) return ndat;
};
} 170
else {
if (d.month()+nmonths>=1) {
ndat=date((d.day()),d.month()+nmonths,d.year());
ndat=date(d.day() −1,(d.month()+nmonths),d.year());
if (ndat.valid()) return ndat;
ndat=date(d.day() −2,(d.month()+nmonths),d.year());
if (ndat.valid()) return ndat;
ndat=date(d.day() −3,(d.month()+nmonths),d.year());
if (ndat.valid()) return ndat; 180
}
else {
ndat=date((d.day()),d.month()+nmonths+12,d.year() −1);
5
if (ndat.valid()) return ndat;
ndat=date((d.day() −1),d.month()+nmonths+12,d.year()−1);
if (ndat.valid()) return ndat;
ndat=date((d.day() −2),d.month()+nmonths+12,d.year()−1);
if (ndat.valid()) return ndat;
ndat=date((d.day() −3),d.month()+nmonths+12,d.year()−1);
if (ndat.valid()) return ndat; 190
};
};
cerr << " Error in Add month " << endl;
};
200
6 Testing
To see how the date class is used, here are some examples
// tst date.cc
// author: Bernt A Oedegaard
void test date simple(){
cout << " Comparing " << d1 << "and" << d2 << endl;
cout << " is" << d1 <<"equal to " << d2 ;
if (d1==d2){ cout << "YES"; } else { cout << "NO"; };
cout << endl; 20
cout << " is" << d1 <<"different from" << d2 ;
}; 30
date d1(930725);
date d2(930810);
6
cout << "DONE TESTING DATE COMPARISONS " << endl;
};
40
date d(1,1,1990);
cout << " next date to " << d << " is " << next date(d) << endl;
cout << " add 25 days to " << d << " gives " << d+25 << endl;
cout << " add 125 days to " << d << " gives " << d+125 << endl;
cout << " previous date to " << d << " is " << previous date(d) << endl;
cout << " subtract 25 days from " << d << " gives " << d−25 << endl; 50
cout << " subtract 125 days from " << d << " gives " << d−125 << endl;
cout << " next month relative to " << d << " is " << next month(d) << endl;
cout << " end of month relative to " << d << " is " << end of month(d) << endl;
cout << " add 6 months relative to " << d << " is " << add months(d,6) << endl;
cout << "DONE TESTING DATE CHANGING " << endl;
};
date d(1,1,2001);
tst print();
}; 70