]>
code.delx.au - gnu-emacs/blob - test/manual/etags/cp-src/functions.cpp
2 #pragma ident "@(#)functions.cpp 1.0 98/11/12 (c) Rupak Rathore"
4 // Constructor default argument initialises to today's values
5 void Date::setDate ( int d
, int m
, int y
){
12 if ( d
== 0 && m
== 0 && y
== 0 ) // explicity called or default constructor hence leave it.
14 if ( d
< 0 && m
< 0 && d
< 0 ) // Special instruction to intialise to today's value
17 ptm
=localtime ( &t
) ;
22 date
->tm_mon
= m
- 1; // Months are counted from January
23 if ( y
> 1900 ) // Complete year specified so take into account
27 date
->tm_sec
=date
->tm_min
=date
->tm_hour
=0;
31 // Addition operation ::: Warning ::: A combination of addition and substraction does not give a proper result
32 void Date::plus ( int days
, int month
, int year
){
35 date
->tm_mday
+= days
;
36 date
->tm_mon
+= month
;
37 date
->tm_year
+= year
;
41 //Substraction operation ::: Warning ::: A combination of addition and substraction does not give a proper result
42 void Date::minus ( int days
, int month
, int year
){
45 date
->tm_mday
-= days
;
46 date
->tm_mon
-= month
;
47 date
->tm_year
-= year
;
52 void Date::shift ( void ){//Shift this date to previous working days (useful for benchmarks)
55 while(isHoliday(*this)||isweekend()){
62 Date
& Date::operator = ( Date d
){
64 setDate ( d
.date
->tm_mday
, d
.date
->tm_mon
+ 1, d
.date
->tm_year
);
69 Date
& Date::operator += ( int days
){
71 date
->tm_mday
+= days
;
77 // Substract number of days
78 Date
& Date::operator -= ( int days
){
80 date
->tm_mday
-= days
;
87 Date
& Date::operator ++ ( void ){
96 Date
& Date::operator -- ( void ){
104 int Date::operator - ( Date d
){
106 if (( ! set() ) || (! d
.set()))
108 l
=(mktime(date
)-mktime(d
.date
))/(3600*24);
112 int Date::operator < ( Date d
) {
113 return ( unidate() < d
.unidate() );
116 int Date::operator > ( Date d
) {
117 return ( unidate() > d
.unidate() );
120 int Date::operator == ( Date d
) {
121 return ( unidate() == d
.unidate() );
124 ostream
& operator << ( ostream
&c
, Date d
) {
128 c
<< d
.date
->tm_mday
<< ":" << d
.date
->tm_mon
+ 1 << ":" << d
.date
->tm_year
+ 1900 ;
132 // Modified to read date in yyyymmdd format.
133 istream
& operator >> ( istream
&i
, Date
& dd
){
145 istream& operator >> ( istream &i, Date &dd ) {
148 cout << "Enter the date ( dd-mm-yyyy ) : ";
150 d = ( input[0] - '0' ) * 10 + ( input[1] - '0' );
151 m = ( input[3] - '0' ) * 10 + ( input[4] - '0' );
152 y = ( input[6] - '0' ) * 1000 + ( input[7] - '0' ) * 100 + ( input[8] - '0' ) * 10 + ( input[9] - '0' );
153 dd.setDate ( d, m, y );
158 // Check whether given year is leap or not
159 bool isLeap ( int year
){
160 return ( (year
%100==0) ? (year
%400==0) : (year
%4==0) );
163 bool isHoliday ( Date d
){
165 ld
= ( d
.year()*100 + d
.month() )*100 + d
.day();
166 for ( int i
=0; i
<no_of_vacations
;i
++)
167 if ( ld
== vacation
[i
] )
172 // Sort the given array in ascending order
173 void asort(int *a
, int num
){
175 for ( k
=1; k
<num
; k
++ ){
177 for ( i
=k
; i
<num
; i
++ )
178 if ( a
[mini
] > a
[i
] ) {
186 void ReadVacation ( char *filename
) {
188 ifstream
vacfile(filename
);
189 if ( ! vacfile
.good() )
190 d_error("ReadVacation","Unable to find the vacation and holidays file");
191 // cerr << filename ;
192 d_silent("ReadVacation","vacation file successfully opened.");
194 while ( !vacfile
.eof() )
195 vacfile
>> vacation
[no_of_vacations
++];
197 d_silent("ReadVacation","Finished Reading file");
201 void Debug ( int lineno
, int level
, char* func
, char* mesg
) // error_level, function, message
203 if ( debug_level
<= level
)
204 cerr
<< PROGNAME
<< ": " << func
<< ": " << lineno
<< ": " << debug_string
[level
] << ": " << mesg
<< endl
;
205 if ( level
== ERROR
){
206 cerr
<< PROGNAME
<< ": Exiting because of fatal error." <<endl
;
211 int WorkingDays(Date a
, Date b
){
214 if ( (! a
.set()) || (! b
.set()) )
216 days
=b
-a
+1; // Inclusive
218 for ( int i
=0;i
<days
;i
++){
219 if((!isHoliday(tmp
))&&(!tmp
.isweekend()))
226 Date
StartDay(Date a
,int days
){//Function to calculate the apropriate start day to finish in days working days
233 if((!isHoliday(tmp
))&&(!tmp
.isweekend()))