|
7 | 7 | *
|
8 | 8 | *
|
9 | 9 | * IDENTIFICATION
|
10 |
| - * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/datetimes.c,v 1.6 1996/11/10 03:03:10 momjian Exp $ |
| 10 | + * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/datetimes.c,v 1.7 1996/11/14 21:38:58 scrappy Exp $ |
11 | 11 | *
|
12 | 12 | *-------------------------------------------------------------------------
|
13 | 13 | */
|
@@ -224,6 +224,104 @@ date_cmp(int4 dateVal1, int4 dateVal2)
|
224 | 224 | return 0;
|
225 | 225 | }
|
226 | 226 |
|
| 227 | +int4 |
| 228 | +date_larger(int4 dateVal1, int4 dateVal2) |
| 229 | +{ |
| 230 | + return (date_gt (dateVal1, dateVal2) ? dateVal1 : dateVal2); |
| 231 | +} |
| 232 | + |
| 233 | +int4 |
| 234 | +date_smaller(int4 dateVal1, int4 dateVal2) |
| 235 | +{ |
| 236 | + return (date_lt (dateVal1, dateVal2) ? dateVal1 : dateVal2); |
| 237 | +} |
| 238 | + |
| 239 | +/* Compute difference between two dates in days. */ |
| 240 | +int32 |
| 241 | +date_mi(int4 dateVal1, int4 dateVal2) |
| 242 | +{ |
| 243 | + DateADT *date1, *date2; |
| 244 | + int32 days = 0; |
| 245 | + int i; |
| 246 | + |
| 247 | + date1 = (DateADT *) &dateVal1; |
| 248 | + date2 = (DateADT *) &dateVal2; |
| 249 | + |
| 250 | + /* Sum number of days in each full year between date1 and date2. */ |
| 251 | + for (i = date1->year + 1; i < date2->year; ++i) |
| 252 | + days += isleap (i) ? 366 : 365; |
| 253 | + |
| 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]; |
| 263 | + |
| 264 | + /* Add in number of days left in month for date1. */ |
| 265 | + days += day_tab[isleap (date1->year)][date1->month - 1] - date1->day; |
| 266 | + |
| 267 | + /* Add in day of month of date2. */ |
| 268 | + days += date2->day; |
| 269 | + |
| 270 | + return (days); |
| 271 | +} |
| 272 | + |
| 273 | +/* Add a number of days to a date, giving a new date. |
| 274 | + Must handle both positive and negative numbers of days. */ |
| 275 | +int4 |
| 276 | +date_pli(int4 dateVal, int32 days) |
| 277 | +{ |
| 278 | + DateADT *date1 = (DateADT *) &dateVal; |
| 279 | + /* Use separate day variable because date1->day is a narrow type. */ |
| 280 | + int32 day = date1->day + days; |
| 281 | + |
| 282 | + if (days > 0) |
| 283 | + { |
| 284 | + /* Loop as long as day has wrapped around end of month. */ |
| 285 | + while (day > day_tab[isleap (date1->year)][date1->month - 1]) |
| 286 | + { |
| 287 | + day -= day_tab[isleap (date1->year)][date1->month - 1]; |
| 288 | + if (++date1->month > 12) |
| 289 | + { |
| 290 | + /* Month wrapped around. */ |
| 291 | + date1->month = 1; |
| 292 | + ++date1->year; |
| 293 | + } |
| 294 | + } |
| 295 | + } |
| 296 | + else |
| 297 | + { |
| 298 | + /* Loop as long as day has wrapped around beginning of month. */ |
| 299 | + while (day < 1) |
| 300 | + { |
| 301 | + /* Decrement month first, because a negative day number |
| 302 | + should be held as relative to the previous month's end. */ |
| 303 | + if (--date1->month < 1) |
| 304 | + { |
| 305 | + /* Month wrapped around. */ |
| 306 | + date1->month = 12; |
| 307 | + --date1->year; |
| 308 | + } |
| 309 | + |
| 310 | + day += day_tab[isleap (date1->year)][date1->month - 1]; |
| 311 | + } |
| 312 | + } |
| 313 | + date1->day = day; |
| 314 | + |
| 315 | + return (dateVal); |
| 316 | +} |
| 317 | + |
| 318 | +/* Subtract a number of days from a date, giving a new date. */ |
| 319 | +int4 |
| 320 | +date_mii(int4 dateVal, int32 days) |
| 321 | +{ |
| 322 | + return (date_pli (dateVal, -days)); |
| 323 | +} |
| 324 | + |
227 | 325 | /*****************************************************************************
|
228 | 326 | * Time ADT
|
229 | 327 | *****************************************************************************/
|
|
0 commit comments