|
7 | 7 | *
|
8 | 8 | *
|
9 | 9 | * IDENTIFICATION
|
10 |
| - * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/datetimes.c,v 1.8 1997/01/26 15:31:12 scrappy Exp $ |
| 10 | + * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/datetimes.c,v 1.9 1997/03/02 02:05:33 momjian Exp $ |
11 | 11 | *
|
12 | 12 | *-------------------------------------------------------------------------
|
13 | 13 | */
|
@@ -240,32 +240,54 @@ date_smaller(int4 dateVal1, int4 dateVal2)
|
240 | 240 | int32
|
241 | 241 | date_mi(int4dateVal1,int4dateVal2)
|
242 | 242 | {
|
| 243 | +int4dv1,dv2; |
243 | 244 | DateADT*date1,*date2;
|
244 | 245 | int32days=0;
|
245 | 246 | inti;
|
246 | 247 |
|
247 |
| -date1= (DateADT*)&dateVal1; |
248 |
| -date2= (DateADT*)&dateVal2; |
| 248 | +/* This circumlocution allows us to assume that date1 is always |
| 249 | + before date2. */ |
| 250 | +dv1=date_smaller (dateVal1,dateVal2); |
| 251 | +dv2=date_larger (dateVal1,dateVal2); |
| 252 | +date1= (DateADT*)&dv1; |
| 253 | +date2= (DateADT*)&dv2; |
249 | 254 |
|
250 | 255 | /* Sum number of days in each full year between date1 and date2. */
|
251 | 256 | for (i=date1->year+1;i<date2->year;++i)
|
252 | 257 | days+=isleap (i) ?366 :365;
|
253 | 258 |
|
254 |
| -/* Add in number of days in each full month from date1 to end of |
255 |
| - year. */ |
256 |
| -for (i=date1->month+1;i <=12;++i) |
257 |
| -days+=day_tab[isleap (date1->year)][i-1]; |
258 |
| - |
259 |
| -/* Add in number of days in each full month from start of year to |
260 |
| - date2. */ |
261 |
| -for (i=1;i<date2->month;++i) |
262 |
| -days+=day_tab[isleap (date2->year)][i-1]; |
| 259 | +if (days) |
| 260 | + { |
| 261 | +/* We need to wrap around the year. Add in number of days in each |
| 262 | + full month from date1 to end of year. */ |
| 263 | +for (i=date1->month+1;i <=12;++i) |
| 264 | +days+=day_tab[isleap (date1->year)][i-1]; |
| 265 | + |
| 266 | +/* Add in number of days in each full month from start of year to |
| 267 | + date2. */ |
| 268 | +for (i=1;i<date2->month;++i) |
| 269 | +days+=day_tab[isleap (date2->year)][i-1]; |
| 270 | + } |
| 271 | +else |
| 272 | + { |
| 273 | +/* Add in number of days in each full month from date1 to date2. */ |
| 274 | +for (i=date1->month+1;i<date2->month;++i) |
| 275 | +days+=day_tab[isleap (date1->year)][i-1]; |
| 276 | + } |
263 | 277 |
|
264 |
| -/* Add in number of days left in month for date1. */ |
265 |
| -days+=day_tab[isleap (date1->year)][date1->month-1]-date1->day; |
| 278 | +if (days||date1->month!=date2->month) |
| 279 | + { |
| 280 | +/* Add in number of days left in month for date1. */ |
| 281 | +days+=day_tab[isleap (date1->year)][date1->month-1]-date1->day; |
266 | 282 |
|
267 |
| -/* Add in day of month of date2. */ |
268 |
| -days+=date2->day; |
| 283 | +/* Add in day of month of date2. */ |
| 284 | +days+=date2->day; |
| 285 | + } |
| 286 | +else |
| 287 | + { |
| 288 | +/* Everything's in the same month, so just subtract the days! */ |
| 289 | +days=date2->day-date1->day; |
| 290 | + } |
269 | 291 |
|
270 | 292 | return (days);
|
271 | 293 | }
|
|