From 0ab29212904dc4352646acaf416b6ab1a367d661 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Thu, 21 Aug 1997 23:57:00 +0000 Subject: [PATCH] Change time function names to be more consistent, and check for zero divides, from Michael Reifenberg. --- src/backend/utils/adt/cash.c | 5 ++- src/backend/utils/adt/dt.c | 51 ++++++++++++++++++++---------- src/backend/utils/adt/geo_ops.c | 5 ++- src/include/catalog/pg_aggregate.h | 4 ++- src/include/catalog/pg_operator.h | 13 ++++---- src/include/catalog/pg_proc.h | 14 ++++---- src/include/utils/dt.h | 13 ++++---- 7 files changed, 68 insertions(+), 37 deletions(-) diff --git a/src/backend/utils/adt/cash.c b/src/backend/utils/adt/cash.c index 93c3e57bec..3bc01c748f 100644 --- a/src/backend/utils/adt/cash.c +++ b/src/backend/utils/adt/cash.c @@ -9,7 +9,7 @@ * workings can be found in the book "Software Solutions in C" by * Dale Schumacher, Academic Press, ISBN: 0-12-632360-7. * - * $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.7 1997/04/28 16:15:13 scrappy Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.8 1997/08/21 23:56:37 momjian Exp $ */ #include @@ -371,6 +371,9 @@ cash_div(Cash *c, float8 *f) if (!PointerIsValid(result = PALLOCTYPE(Cash))) elog(WARN,"Memory allocation failed, can't divide cash",NULL); + if (*f == 0.0) + elog(WARN,"cash_div: divide by 0.0 error"); + *result = rint(*c / *f); return(result); diff --git a/src/backend/utils/adt/dt.c b/src/backend/utils/adt/dt.c index 1fbca2d82c..adf8c31e2d 100644 --- a/src/backend/utils/adt/dt.c +++ b/src/backend/utils/adt/dt.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/dt.c,v 1.32 1997/08/19 21:34:34 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/dt.c,v 1.33 1997/08/21 23:56:40 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -645,7 +645,7 @@ datetime_larger(DateTime *datetime1, DateTime *datetime2) TimeSpan * -datetime_sub(DateTime *datetime1, DateTime *datetime2) +datetime_mi(DateTime *datetime1, DateTime *datetime2) { TimeSpan *result; @@ -672,10 +672,10 @@ datetime_sub(DateTime *datetime1, DateTime *datetime2) result->month = 0; return(result); -} /* datetime_sub() */ +} /* datetime_mi() */ -/* datetime_add_span() +/* datetime_pl_span() * Add a timespan to a datetime data type. * Note that timespan has provisions for qualitative year/month * units, so try to do the right thing with them. @@ -684,7 +684,7 @@ datetime_sub(DateTime *datetime1, DateTime *datetime2) * to the last day of month. */ DateTime * -datetime_add_span(DateTime *datetime, TimeSpan *span) +datetime_pl_span(DateTime *datetime, TimeSpan *span) { DateTime *result; DateTime dt; @@ -697,7 +697,7 @@ datetime_add_span(DateTime *datetime, TimeSpan *span) result = PALLOCTYPE(DateTime); #ifdef DATEDEBUG -printf( "datetime_add_span- add %f to %d %f\n", *datetime, span->month, span->time); +printf( "datetime_pl_span- add %f to %d %f\n", *datetime, span->month, span->time); #endif if (DATETIME_NOT_FINITE(*datetime)) { @@ -721,7 +721,7 @@ printf( "datetime_add_span- add %f to %d %f\n", *datetime, span->month, span->ti if (datetime2tm( dt, &tz, tm, &fsec, &tzn) == 0) { #ifdef DATEDEBUG -printf( "datetime_add_span- date was %04d-%02d-%02d %02d:%02d:%02d\n", +printf( "datetime_pl_span- date was %04d-%02d-%02d %02d:%02d:%02d\n", tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); #endif tm->tm_mon += span->month; @@ -743,7 +743,7 @@ printf( "datetime_add_span- date was %04d-%02d-%02d %02d:%02d:%02d\n", }; #ifdef DATEDEBUG -printf( "datetime_add_span- date becomes %04d-%02d-%02d %02d:%02d:%02d\n", +printf( "datetime_pl_span- date becomes %04d-%02d-%02d %02d:%02d:%02d\n", tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); #endif if (tm2datetime( tm, fsec, &tz, &dt) != 0) @@ -758,10 +758,10 @@ printf( "datetime_add_span- date becomes %04d-%02d-%02d %02d:%02d:%02d\n", }; return(result); -} /* datetime_add_span() */ +} /* datetime_pl_span() */ DateTime * -datetime_sub_span(DateTime *datetime, TimeSpan *span) +datetime_mi_span(DateTime *datetime, TimeSpan *span) { DateTime *result; TimeSpan tspan; @@ -772,10 +772,10 @@ datetime_sub_span(DateTime *datetime, TimeSpan *span) tspan.month = -span->month; tspan.time = -span->time; - result = datetime_add_span( datetime, &tspan); + result = datetime_pl_span( datetime, &tspan); return(result); -} /* datetime_sub_span() */ +} /* datetime_mi_span() */ TimeSpan * @@ -885,7 +885,7 @@ printf( "timespan_larger- months %d %d times %f %f spans %f %f\n", TimeSpan * -timespan_add(TimeSpan *span1, TimeSpan *span2) +timespan_pl(TimeSpan *span1, TimeSpan *span2) { TimeSpan *result; @@ -898,10 +898,10 @@ timespan_add(TimeSpan *span1, TimeSpan *span2) result->time = JROUND(span1->time + span2->time); return(result); -} /* timespan_add() */ +} /* timespan_pl() */ TimeSpan * -timespan_sub(TimeSpan *span1, TimeSpan *span2) +timespan_mi(TimeSpan *span1, TimeSpan *span2) { TimeSpan *result; @@ -914,8 +914,27 @@ timespan_sub(TimeSpan *span1, TimeSpan *span2) result->time = JROUND(span1->time - span2->time); return(result); -} /* timespan_sub() */ +} /* timespan_mi() */ +TimeSpan * +timespan_div(TimeSpan *span1, float8 *arg2) +{ + TimeSpan *result; + + if ((!PointerIsValid(span1)) || (!PointerIsValid(arg2))) + return NULL; + + if (!PointerIsValid(result = PALLOCTYPE(TimeSpan))) + elog(WARN,"Memory allocation failed, can't subtract timespans",NULL); + + if (*arg2 == 0.0) + elog(WARN,"timespan_div: divide by 0.0 error"); + + result->month = rint(span1->month / *arg2); + result->time = JROUND(span1->time / *arg2); + + return(result); +} /* timespan_div() */ /* datetime_age() * Calculate time difference while retaining year/month fields. diff --git a/src/backend/utils/adt/geo_ops.c b/src/backend/utils/adt/geo_ops.c index 0c06e45bef..12fea90436 100644 --- a/src/backend/utils/adt/geo_ops.c +++ b/src/backend/utils/adt/geo_ops.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/geo_ops.c,v 1.15 1997/08/19 21:34:39 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/geo_ops.c,v 1.16 1997/08/21 23:56:41 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -2555,6 +2555,9 @@ point_div(Point *p1, Point *p2) div = (p2->x*p2->x) + (p2->y*p2->y); + if (div == 0.0) + elog(WARN,"point_div: divide by 0.0 error"); + result->x = ((p1->x*p2->x) + (p1->y*p2->y)) / div; result->y = ((p2->x*p1->y) - (p2->y*p1->x)) / div; diff --git a/src/include/catalog/pg_aggregate.h b/src/include/catalog/pg_aggregate.h index a4625ce6f9..f951e1e5d8 100644 --- a/src/include/catalog/pg_aggregate.h +++ b/src/include/catalog/pg_aggregate.h @@ -7,7 +7,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: pg_aggregate.h,v 1.7 1997/04/27 19:22:36 thomas Exp $ + * $Id: pg_aggregate.h,v 1.8 1997/08/21 23:56:45 momjian Exp $ * * NOTES * the genbki.sh script reads this file and generates .bki @@ -92,12 +92,14 @@ DATA(insert OID = 0 ( avg PGUID int2pl int2inc int2div 21 21 21 DATA(insert OID = 0 ( avg PGUID float4pl float4inc float4div 700 700 700 700 _null_ 0.0 )); DATA(insert OID = 0 ( avg PGUID float8pl float8inc float8div 701 701 701 701 _null_ 0.0 )); DATA(insert OID = 0 ( avg PGUID cash_pl float8inc cash_div 790 790 701 790 _null_ 0.0 )); +DATA(insert OID = 0 ( avg PGUID timespan_pl float8inc timespan_div 1186 1186 701 1186 _null_ 0.0 )); DATA(insert OID = 0 ( sum PGUID int4pl - - 23 23 0 23 _null_ _null_ )); DATA(insert OID = 0 ( sum PGUID int2pl - - 21 21 0 21 _null_ _null_ )); DATA(insert OID = 0 ( sum PGUID float4pl - - 700 700 0 700 _null_ _null_ )); DATA(insert OID = 0 ( sum PGUID float8pl - - 701 701 0 701 _null_ _null_ )); DATA(insert OID = 0 ( sum PGUID cash_pl - - 790 790 0 790 _null_ _null_ )); +DATA(insert OID = 0 ( sum PGUID timespan_pl - - 1186 1186 0 1186 _null_ _null_ )); DATA(insert OID = 0 ( max PGUID int4larger - - 23 23 0 23 _null_ _null_ )); DATA(insert OID = 0 ( max PGUID int2larger - - 21 21 0 21 _null_ _null_ )); diff --git a/src/include/catalog/pg_operator.h b/src/include/catalog/pg_operator.h index 6bab9310b4..6e7f928401 100644 --- a/src/include/catalog/pg_operator.h +++ b/src/include/catalog/pg_operator.h @@ -7,7 +7,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: pg_operator.h,v 1.12 1997/07/29 16:17:50 thomas Exp $ + * $Id: pg_operator.h,v 1.13 1997/08/21 23:56:48 momjian Exp $ * * NOTES * the genbki.sh script reads this file and generates .bki @@ -487,9 +487,9 @@ DATA(insert OID = 1323 ( "<=" PGUID 0 b t f 1184 1184 16 1324 1324 0 0 da DATA(insert OID = 1324 ( ">" PGUID 0 b t f 1184 1184 16 1323 1323 0 0 datetime_gt intltsel intltjoinsel )); DATA(insert OID = 1325 ( ">=" PGUID 0 b t f 1184 1184 16 1322 1322 0 0 datetime_ge intltsel intltjoinsel )); -DATA(insert OID = 1327 ( "+" PGUID 0 b t f 1184 1186 1184 1327 0 0 0 datetime_add_span - - )); -DATA(insert OID = 1328 ( "-" PGUID 0 b t f 1184 1184 1186 0 0 0 0 datetime_sub - - )); -DATA(insert OID = 1329 ( "-" PGUID 0 b t f 1184 1186 1184 0 0 0 0 datetime_sub_span - - )); +DATA(insert OID = 1327 ( "+" PGUID 0 b t f 1184 1186 1184 1327 0 0 0 datetime_pl_span - - )); +DATA(insert OID = 1328 ( "-" PGUID 0 b t f 1184 1184 1186 0 0 0 0 datetime_mi - - )); +DATA(insert OID = 1329 ( "-" PGUID 0 b t f 1184 1186 1184 0 0 0 0 datetime_mi_span - - )); /* timespan operators */ DATA(insert OID = 1330 ( "=" PGUID 0 b t f 1186 1186 16 1330 1331 1332 1332 timespan_eq eqsel eqjoinsel )); @@ -500,8 +500,8 @@ DATA(insert OID = 1334 ( ">" PGUID 0 b t f 1186 1186 16 1333 1333 0 0 ti DATA(insert OID = 1335 ( ">=" PGUID 0 b t f 1186 1186 16 1332 1332 0 0 timespan_ge intltsel intltjoinsel )); DATA(insert OID = 1336 ( "-" PGUID 0 l t f 0 1186 1186 0 0 0 0 timespan_um 0 0 )); -DATA(insert OID = 1337 ( "+" PGUID 0 b t f 1186 1186 1186 1337 0 0 0 timespan_add - - )); -DATA(insert OID = 1338 ( "-" PGUID 0 b t f 1186 1186 1186 0 0 0 0 timespan_sub - - )); +DATA(insert OID = 1337 ( "+" PGUID 0 b t f 1186 1186 1186 1337 0 0 0 timespan_pl - - )); +DATA(insert OID = 1338 ( "-" PGUID 0 b t f 1186 1186 1186 0 0 0 0 timespan_mi - - )); /* float48 operators */ DATA(insert OID = 1116 ( "+" PGUID 0 b t f 700 701 701 1116 0 0 0 float48pl - - )); @@ -628,6 +628,7 @@ DATA(insert OID = 1559 ( "##" PGUID 0 b t f 600 603 600 0 0 0 0 close DATA(insert OID = 1566 ( "##" PGUID 0 b t f 601 628 600 0 0 0 0 close_sl - - )); DATA(insert OID = 1567 ( "##" PGUID 0 b t f 601 603 600 0 0 0 0 close_sb - - )); DATA(insert OID = 1568 ( "##" PGUID 0 b t f 628 603 600 0 0 0 0 close_lb - - )); +DATA(insert OID = 1585 ( "/" PGUID 0 b t f 1186 1186 1186 0 0 0 0 timespan_div - - )); /* * function prototypes diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h index 52e12cfc8f..13052cba1f 100644 --- a/src/include/catalog/pg_proc.h +++ b/src/include/catalog/pg_proc.h @@ -6,7 +6,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: pg_proc.h,v 1.24 1997/07/29 16:17:52 thomas Exp $ + * $Id: pg_proc.h,v 1.25 1997/08/21 23:56:51 momjian Exp $ * * NOTES * The script catalog/genbki.sh reads this file and generates .bki @@ -828,8 +828,8 @@ DATA(insert OID = 1165 ( timespan_le PGUID 11 f t f 2 f 16 "1186 1186" 100 DATA(insert OID = 1166 ( timespan_ge PGUID 11 f t f 2 f 16 "1186 1186" 100 0 0 100 foo bar )); DATA(insert OID = 1167 ( timespan_gt PGUID 11 f t f 2 f 16 "1186 1186" 100 0 0 100 foo bar )); DATA(insert OID = 1168 ( timespan_um PGUID 11 f t f 1 f 1186 "1186" 100 0 0 100 foo bar )); -DATA(insert OID = 1169 ( timespan_add PGUID 11 f t f 2 f 1186 "1186 1186" 100 0 0 100 foo bar )); -DATA(insert OID = 1170 ( timespan_sub PGUID 11 f t f 2 f 1186 "1186 1186" 100 0 0 100 foo bar )); +DATA(insert OID = 1169 ( timespan_pl PGUID 11 f t f 2 f 1186 "1186 1186" 100 0 0 100 foo bar )); +DATA(insert OID = 1170 ( timespan_mi PGUID 11 f t f 2 f 1186 "1186 1186" 100 0 0 100 foo bar )); DATA(insert OID = 1171 ( datetime_part PGUID 11 f t f 2 f 701 "25 1184" 100 0 0 100 foo bar )); DATA(insert OID = 1172 ( timespan_part PGUID 11 f t f 2 f 701 "25 1186" 100 0 0 100 foo bar )); @@ -842,9 +842,9 @@ DATA(insert OID = 1178 ( datetime_date PGUID 11 f t f 1 f 1082 "1184" 100 DATA(insert OID = 1179 ( abstime_date PGUID 11 f t f 1 f 1082 "702" 100 0 0 100 foo bar )); DATA(insert OID = 1180 ( datetime_abstime PGUID 11 f t f 1 f 702 "1184" 100 0 0 100 foo bar )); -DATA(insert OID = 1188 ( datetime_sub PGUID 11 f t f 2 f 1186 "1184 1184" 100 0 0 100 foo bar )); -DATA(insert OID = 1189 ( datetime_add_span PGUID 11 f t f 2 f 1184 "1184 1186" 100 0 0 100 foo bar )); -DATA(insert OID = 1190 ( datetime_sub_span PGUID 11 f t f 2 f 1184 "1184 1186" 100 0 0 100 foo bar )); +DATA(insert OID = 1188 ( datetime_mi PGUID 11 f t f 2 f 1186 "1184 1184" 100 0 0 100 foo bar )); +DATA(insert OID = 1189 ( datetime_pl_span PGUID 11 f t f 2 f 1184 "1184 1186" 100 0 0 100 foo bar )); +DATA(insert OID = 1190 ( datetime_mi_span PGUID 11 f t f 2 f 1184 "1184 1186" 100 0 0 100 foo bar )); DATA(insert OID = 1191 ( text_datetime PGUID 11 f t f 1 f 1184 "25" 100 0 0 100 foo bar )); DATA(insert OID = 1192 ( datetime_text PGUID 11 f t f 1 f 25 "1184" 100 0 0 100 foo bar )); DATA(insert OID = 1193 ( timespan_text PGUID 11 f t f 1 f 1186 "25" 100 0 0 100 foo bar )); @@ -1088,6 +1088,8 @@ DATA(insert OID = 882 ( rtrim PGUID 14 f t f 1 f 25 "25" 100 0 0 100 " DATA(insert OID = 883 ( substr PGUID 14 f t f 2 f 25 "25 23" 100 0 0 100 "select substr($1, $2, 10000)" - )); DATA(insert OID = 884 ( btrim PGUID 11 f t f 2 f 25 "25 25" 100 0 0 100 foo bar )); DATA(insert OID = 885 ( btrim PGUID 14 f t f 1 f 25 "25" 100 0 0 100 "select btrim($1, \' \')" - )); +/* XXX Help, where should I go to */ +DATA(insert OID = 1586 ( timespan_div PGUID 11 f t f 2 f 1186 "1186 701" 100 0 0 100 foo bar )); /* SEQUENCEs nextval & currval functions */ DATA(insert OID = 1317 ( nextval PGUID 11 f t f 1 f 23 "25" 100 0 0 100 foo bar )); diff --git a/src/include/utils/dt.h b/src/include/utils/dt.h index da98e780fa..b9e678687e 100644 --- a/src/include/utils/dt.h +++ b/src/include/utils/dt.h @@ -8,7 +8,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: dt.h,v 1.16 1997/08/19 21:40:30 momjian Exp $ + * $Id: dt.h,v 1.17 1997/08/21 23:57:00 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -283,12 +283,13 @@ extern float64 timespan_part(text *units, TimeSpan *timespan); extern text *datetime_zone(text *zone, DateTime *datetime); extern TimeSpan *timespan_um(TimeSpan *span); -extern TimeSpan *timespan_add(TimeSpan *span1, TimeSpan *span2); -extern TimeSpan *timespan_sub(TimeSpan *span1, TimeSpan *span2); +extern TimeSpan *timespan_pl(TimeSpan *span1, TimeSpan *span2); +extern TimeSpan *timespan_mi(TimeSpan *span1, TimeSpan *span2); +extern TimeSpan *timespan_div(TimeSpan *span1, float8 *arg2); -extern TimeSpan *datetime_sub(DateTime *dt1, DateTime *dt2); -extern DateTime *datetime_add_span(DateTime *dt, TimeSpan *span); -extern DateTime *datetime_sub_span(DateTime *dt, TimeSpan *span); +extern TimeSpan *datetime_mi(DateTime *dt1, DateTime *dt2); +extern DateTime *datetime_pl_span(DateTime *dt, TimeSpan *span); +extern DateTime *datetime_mi_span(DateTime *dt, TimeSpan *span); extern TimeSpan *datetime_age(DateTime *dt1, DateTime *dt2); extern void GetCurrentTime(struct tm *tm); -- 2.40.0