Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8abc490

Browse files
committed
Features added:
* Wrote max(date) and min(date) aggregates * Wrote operator "-" for date; date - date yields number of days difference * Wrote operator+(date,int) and operator-(date,int); the int is the number of days. Each operator returns a new date. By: Tom Tromey <tromey@creche.cygnus.com>
1 parent eedc75b commit 8abc490

File tree

4 files changed

+112
-5
lines changed

4 files changed

+112
-5
lines changed

src/backend/utils/adt/datetimes.c

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* 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 $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -224,6 +224,104 @@ date_cmp(int4 dateVal1, int4 dateVal2)
224224
return 0;
225225
}
226226

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+
227325
/*****************************************************************************
228326
* Time ADT
229327
*****************************************************************************/

src/include/catalog/pg_aggregate.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: pg_aggregate.h,v 1.2 1996/10/31 09:47:04 scrappy Exp $
10+
* $Id: pg_aggregate.h,v 1.3 1996/11/14 21:39:07 scrappy Exp $
1111
*
1212
* NOTES
1313
* the genbki.sh script reads this file and generates .bki
@@ -101,11 +101,13 @@ DATA(insert OID = 0 ( max PGUID int4larger - - 23 23 0 23 _null_ _null_
101101
DATA(insert OID = 0 ( max PGUID int2larger - - 21 21 0 21 _null_ _null_ ));
102102
DATA(insert OID = 0 ( max PGUID float4larger - - 700 700 0 700 _null_ _null_ ));
103103
DATA(insert OID = 0 ( max PGUID float8larger - - 701 701 0 701 _null_ _null_ ));
104+
DATA(insert OID = 0 ( max PGUID date_larger - - 1082 1082 0 1082 _null_ _null_ ));
104105

105106
DATA(insert OID = 0 ( min PGUID int4smaller - - 23 23 0 23 _null_ _null_ ));
106107
DATA(insert OID = 0 ( min PGUID int2smaller - - 21 21 0 21 _null_ _null_ ));
107108
DATA(insert OID = 0 ( min PGUID float4smaller - - 700 700 0 700 _null_ _null_ ));
108109
DATA(insert OID = 0 ( min PGUID float8smaller - - 701 701 0 701 _null_ _null_ ));
110+
DATA(insert OID = 0 ( min PGUID date_smaller - - 1082 1082 0 1082 _null_ _null_ ));
109111

110112
DATA(insert OID = 0 ( count PGUID - int4inc - 0 0 23 23 _null_ 0 ));
111113

src/include/catalog/pg_operator.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: pg_operator.h,v 1.3 1996/11/13 20:50:58 scrappy Exp $
10+
* $Id: pg_operator.h,v 1.4 1996/11/14 21:39:11 scrappy Exp $
1111
*
1212
* NOTES
1313
* the genbki.sh script reads this file and generates .bki
@@ -389,6 +389,9 @@ DATA(insert OID = 1095 ( "<" PGUID 0 b t f 1082 1082 16 1097 1098 0 0
389389
DATA(insert OID = 1096 ( "<=" PGUID 0 b t f 1082 1082 16 1098 1097 0 0 date_le intltsel intltjoinsel ));
390390
DATA(insert OID = 1097 ( ">" PGUID 0 b t f 1082 1082 16 1095 1096 0 0 date_gt intltsel intltjoinsel ));
391391
DATA(insert OID = 1098 ( ">=" PGUID 0 b t f 1082 1082 16 1096 1065 0 0 date_ge intltsel intltjoinsel ));
392+
DATA(insert OID = 1099 ( "-" PGUID 0 b t f 1082 1082 23 0 0 0 0 date_mi - - ));
393+
DATA(insert OID = 1100 ( "+" PGUID 0 b t f 1082 23 1082 0 0 0 0 date_pli - - ));
394+
DATA(insert OID = 1101 ( "-" PGUID 0 b t f 1082 23 1082 0 0 0 0 date_mii - - ));
392395

393396
DATA(insert OID = 1108 ( "=" PGUID 0 b t t 1083 1083 16 1108 1109 1110 1110 time_eq eqsel eqjoinsel ));
394397
DATA(insert OID = 1109 ( "<>" PGUID 0 b t f 1083 1083 16 1109 1108 0 0 time_ne neqsel neqjoinsel ));

src/include/catalog/pg_proc.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: pg_proc.h,v 1.6 1996/11/13 20:51:01 scrappy Exp $
9+
* $Id: pg_proc.h,v 1.7 1996/11/14 21:39:14 scrappy Exp $
1010
*
1111
* NOTES
1212
* The script catalog/genbki.sh reads this file and generates .bki
@@ -727,7 +727,11 @@ DATA(insert OID = 1089 ( date_gt PGUID 11 f t f 2 f 16 "1082 1082" 100
727727
DATA(insert OID = 1090 ( date_ge PGUID 11 f t f 2 f 16 "1082 1082" 100 0 0 100 foo bar ));
728728
DATA(insert OID = 1091 ( date_ne PGUID 11 f t f 2 f 16 "1082 1082" 100 0 0 100 foo bar ));
729729
DATA(insert OID = 1092 ( date_cmp PGUID 11 f t f 2 f 23 "1082 1082" 100 0 0 100 foo bar ));
730-
730+
DATA(insert OID = 1093 ( date_larger PGUID 11 f t f 2 f 1082 "1082 1082" 100 0 0 100 foo bar ));
731+
DATA(insert OID = 1094 ( date_smaller PGUID 11 f t f 2 f 1082 "1082 1082" 100 0 0 100 foo bar ));
732+
DATA(insert OID = 1095 ( date_mi PGUID 11 f t f 2 f 23 "1082 1082" 100 0 0 100 foo bar ));
733+
DATA(insert OID = 1096 ( date_pli PGUID 11 f t f 2 f 1082 "1082 23" 100 0 0 100 foo bar ));
734+
DATA(insert OID = 1097 ( date_mii PGUID 11 f t f 2 f 1082 "1082 23" 100 0 0 100 foo bar ));
731735
DATA(insert OID = 1099 ( time_in PGUID 11 f t f 1 f 1083 "0" 100 0 0 100 foo bar ));
732736

733737
/* OIDS 1100 - 1199 */

0 commit comments

Comments
 (0)