This source file includes following definitions.
- setDate
- plus
- minus
- shift
- isLeap
- isHoliday
- asort
- ReadVacation
- Debug
- WorkingDays
- StartDay
1 #include "main.hpp"
2 #pragma ident "@(#)functions.cpp 1.0 98/11/12 (c) Rupak Rathore"
3
4
5 void Date::setDate ( int d , int m , int y ){
6 time_t t;
7 struct tm * ptm;
8 t = time ( NULL ) ;
9 if ( date != NULL )
10 delete date;
11 date = NULL;
12 if ( d == 0 && m == 0 && y == 0 )
13 return;
14 if ( d < 0 && m < 0 && d < 0 )
15 d=m=y=0;
16 date = new tm;
17 ptm=localtime ( &t ) ;
18 *date=(*ptm);
19 if ( d )
20 date->tm_mday = d;
21 if ( m )
22 date->tm_mon = m - 1;
23 if ( y > 1900 )
24 y -= 1900;
25 if ( y )
26 date->tm_year = y;
27 date->tm_sec=date->tm_min=date->tm_hour=0;
28 t = mktime ( date ) ;
29 }
30
31
32 void Date::plus ( int days , int month , int year ){
33 if ( ! set () )
34 return;
35 date->tm_mday += days ;
36 date->tm_mon += month ;
37 date->tm_year += year ;
38 mktime ( date );
39 }
40
41
42 void Date::minus ( int days , int month , int year ){
43 if ( ! set () )
44 return;
45 date->tm_mday -= days ;
46 date->tm_mon -= month ;
47 date->tm_year -= year ;
48 mktime ( date );
49 }
50
51
52 void Date::shift ( void ){
53 if ( ! set() )
54 return ;
55 while(isHoliday(*this)||isweekend()){
56 date->tm_mday -= 1 ;
57 mktime ( date );
58 }
59 }
60
61
62 Date & Date::operator = ( Date d ){
63 if ( d.set() )
64 setDate ( d.date->tm_mday, d.date->tm_mon + 1, d.date->tm_year );
65 return(*this);
66 }
67
68
69 Date & Date::operator += ( int days ){
70 if ( set () ){
71 date->tm_mday += days ;
72 mktime ( date );
73 }
74 return(*this);
75 }
76
77
78 Date & Date::operator -= ( int days ){
79 if ( set () ){
80 date->tm_mday -= days ;
81 mktime ( date );
82 }
83 return(*this);
84 }
85
86
87 Date & Date::operator ++ ( void ){
88 if ( set () ){
89 date->tm_mday += 1 ;
90 mktime ( date );
91 }
92 return(*this);
93 }
94
95
96 Date & Date::operator -- ( void ){
97 if ( set () ){
98 date->tm_mday -= 1 ;
99 mktime ( date );
100 }
101 return(*this);
102 }
103
104 int Date::operator - ( Date d ){
105 long l;
106 if (( ! set() ) || (! d.set()))
107 return(0);
108 l=(mktime(date)-mktime(d.date))/(3600*24);
109 return((int)l);
110 }
111
112 int Date::operator < ( Date d ) {
113 return ( unidate() < d.unidate() );
114 }
115
116 int Date::operator > ( Date d ) {
117 return ( unidate() > d.unidate() );
118 }
119
120 int Date::operator == ( Date d ) {
121 return ( unidate() == d.unidate() );
122 }
123
124 ostream& operator << ( ostream &c, Date d ) {
125 if ( ! d.set() )
126 c << "Null";
127 else
128 c << d.date->tm_mday << ":" << d.date->tm_mon + 1 << ":" << d.date->tm_year + 1900 ;
129 return ( c );
130 }
131
132
133 istream& operator >> ( istream &i, Date & dd ){
134 int d,m,y,tmp;
135 i >> tmp;
136 d=tmp%100;
137 tmp/=100;
138 m=tmp%100;
139 tmp/=100;
140 y=tmp;
141 dd.setDate(d,m,y);
142 return(i);
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 bool isLeap ( int year ){
160 return ( (year%100==0) ? (year%400==0) : (year%4==0) );
161 }
162
163 bool isHoliday ( Date d ){
164 long int ld;
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] )
168 return(true);
169 return(false);
170 }
171
172
173 void asort(int *a, int num){
174 int i,k,mini,tmp;
175 for ( k=1; k<num; k++ ){
176 mini=k-1;
177 for ( i=k; i<num; i++ )
178 if ( a[mini] > a[i] ) {
179 tmp=a[i];
180 a[i]=a[mini];
181 a[mini]=tmp;
182 }
183 }
184 }
185
186 void ReadVacation ( char *filename ) {
187
188 ifstream vacfile(filename);
189 if ( ! vacfile.good() )
190 d_error("ReadVacation","Unable to find the vacation and holidays file");
191
192 d_silent("ReadVacation","vacation file successfully opened.");
193 no_of_vacations = 0;
194 while ( !vacfile.eof() )
195 vacfile >> vacation[no_of_vacations++];
196 --no_of_vacations;
197 d_silent("ReadVacation","Finished Reading file");
198 vacfile.close();
199 }
200
201 void Debug ( int lineno, int level, char* func , char* mesg )
202 {
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 ;
207 exit(2);
208 }
209 }
210
211 int WorkingDays(Date a, Date b){
212 Date tmp;
213 int wdays=0,days=0;
214 if ( (! a.set()) || (! b.set()) )
215 return(0);
216 days=b-a+1;
217 tmp=a;
218 for ( int i=0;i<days;i++){
219 if((!isHoliday(tmp))&&(!tmp.isweekend()))
220 wdays++;
221 tmp++;
222 }
223 return(wdays);
224 }
225
226 Date StartDay(Date a,int days){
227 Date tmp;
228 int wdays=0;
229 if ( ! a.set() )
230 return (a);
231 tmp=a;
232 while(wdays<days){
233 if((!isHoliday(tmp))&&(!tmp.isweekend()))
234 wdays++;
235 tmp--;
236 }
237 tmp++;
238 return(tmp);
239 }