88 *
99 *
1010 * IDENTIFICATION
11- * $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.115 2004/11/20 22:12:44 tgl Exp $
11+ * $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.116 2004/12/01 19:57:49 tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
1717
1818#include <ctype.h>
1919#include <math.h>
20- #include <errno.h>
2120#include <float.h>
2221#include <limits.h>
2322
@@ -2352,25 +2351,25 @@ timestamp_age(PG_FUNCTION_ARGS)
23522351tm -> tm_year = - tm -> tm_year ;
23532352}
23542353
2355- if (tm -> tm_sec < 0 )
2354+ while (tm -> tm_sec < 0 )
23562355{
23572356tm -> tm_sec += 60 ;
23582357tm -> tm_min -- ;
23592358}
23602359
2361- if (tm -> tm_min < 0 )
2360+ while (tm -> tm_min < 0 )
23622361{
23632362tm -> tm_min += 60 ;
23642363tm -> tm_hour -- ;
23652364}
23662365
2367- if (tm -> tm_hour < 0 )
2366+ while (tm -> tm_hour < 0 )
23682367{
23692368tm -> tm_hour += 24 ;
23702369tm -> tm_mday -- ;
23712370}
23722371
2373- if (tm -> tm_mday < 0 )
2372+ while (tm -> tm_mday < 0 )
23742373{
23752374if (dt1 < dt2 )
23762375{
@@ -2384,7 +2383,7 @@ timestamp_age(PG_FUNCTION_ARGS)
23842383}
23852384}
23862385
2387- if (tm -> tm_mon < 0 )
2386+ while (tm -> tm_mon < 0 )
23882387{
23892388tm -> tm_mon += 12 ;
23902389tm -> tm_year -- ;
@@ -2437,11 +2436,14 @@ timestamptz_age(PG_FUNCTION_ARGS)
24372436* tm1 = & tt1 ;
24382437struct pg_tm tt2 ,
24392438* tm2 = & tt2 ;
2439+ int tz1 ;
2440+ int tz2 ;
2441+ char * tzn ;
24402442
24412443result = (Interval * )palloc (sizeof (Interval ));
24422444
2443- if ((timestamp2tm (dt1 ,NULL ,tm1 ,& fsec1 ,NULL )== 0 )
2444- && (timestamp2tm (dt2 ,NULL ,tm2 ,& fsec2 ,NULL )== 0 ))
2445+ if ((timestamp2tm (dt1 ,& tz1 ,tm1 ,& fsec1 ,& tzn )== 0 )
2446+ && (timestamp2tm (dt2 ,& tz2 ,tm2 ,& fsec2 ,& tzn )== 0 ))
24452447{
24462448fsec = (fsec1 - fsec2 );
24472449tm -> tm_sec = (tm1 -> tm_sec - tm2 -> tm_sec );
@@ -2463,25 +2465,25 @@ timestamptz_age(PG_FUNCTION_ARGS)
24632465tm -> tm_year = - tm -> tm_year ;
24642466}
24652467
2466- if (tm -> tm_sec < 0 )
2468+ while (tm -> tm_sec < 0 )
24672469{
24682470tm -> tm_sec += 60 ;
24692471tm -> tm_min -- ;
24702472}
24712473
2472- if (tm -> tm_min < 0 )
2474+ while (tm -> tm_min < 0 )
24732475{
24742476tm -> tm_min += 60 ;
24752477tm -> tm_hour -- ;
24762478}
24772479
2478- if (tm -> tm_hour < 0 )
2480+ while (tm -> tm_hour < 0 )
24792481{
24802482tm -> tm_hour += 24 ;
24812483tm -> tm_mday -- ;
24822484}
24832485
2484- if (tm -> tm_mday < 0 )
2486+ while (tm -> tm_mday < 0 )
24852487{
24862488if (dt1 < dt2 )
24872489{
@@ -2495,12 +2497,16 @@ timestamptz_age(PG_FUNCTION_ARGS)
24952497}
24962498}
24972499
2498- if (tm -> tm_mon < 0 )
2500+ while (tm -> tm_mon < 0 )
24992501{
25002502tm -> tm_mon += 12 ;
25012503tm -> tm_year -- ;
25022504}
25032505
2506+ /*
2507+ * Note: we deliberately ignore any difference between tz1 and tz2.
2508+ */
2509+
25042510/* recover sign if necessary... */
25052511if (dt1 < dt2 )
25062512{