From: Tom Lane Date: Wed, 1 Dec 2004 19:57:56 +0000 (+0000) Subject: Fix timestamptz_age() to do calculation in local timezone not GMT, per bug 1332. X-Git-Tag: REL7_4_7~25 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d237a12e493f6e1b562579bf8361040ac1ba2470;p=postgresql Fix timestamptz_age() to do calculation in local timezone not GMT, per bug 1332. --- diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index dd5395c0ef..35486b275b 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v 1.96.2.1 2004/05/31 18:32:23 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v 1.96.2.2 2004/12/01 19:57:56 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -17,7 +17,6 @@ #include #include -#include #include #include /* for finite() on Solaris */ @@ -2161,25 +2160,25 @@ timestamp_age(PG_FUNCTION_ARGS) tm->tm_year = -tm->tm_year; } - if (tm->tm_sec < 0) + while (tm->tm_sec < 0) { tm->tm_sec += 60; tm->tm_min--; } - if (tm->tm_min < 0) + while (tm->tm_min < 0) { tm->tm_min += 60; tm->tm_hour--; } - if (tm->tm_hour < 0) + while (tm->tm_hour < 0) { tm->tm_hour += 24; tm->tm_mday--; } - if (tm->tm_mday < 0) + while (tm->tm_mday < 0) { if (dt1 < dt2) { @@ -2193,7 +2192,7 @@ timestamp_age(PG_FUNCTION_ARGS) } } - if (tm->tm_mon < 0) + while (tm->tm_mon < 0) { tm->tm_mon += 12; tm->tm_year--; @@ -2246,11 +2245,14 @@ timestamptz_age(PG_FUNCTION_ARGS) *tm1 = &tt1; struct tm tt2, *tm2 = &tt2; + int tz1; + int tz2; + char *tzn; result = (Interval *) palloc(sizeof(Interval)); - if ((timestamp2tm(dt1, NULL, tm1, &fsec1, NULL) == 0) - && (timestamp2tm(dt2, NULL, tm2, &fsec2, NULL) == 0)) + if ((timestamp2tm(dt1, &tz1, tm1, &fsec1, &tzn) == 0) + && (timestamp2tm(dt2, &tz2, tm2, &fsec2, &tzn) == 0)) { fsec = (fsec1 - fsec2); tm->tm_sec = (tm1->tm_sec - tm2->tm_sec); @@ -2272,25 +2274,25 @@ timestamptz_age(PG_FUNCTION_ARGS) tm->tm_year = -tm->tm_year; } - if (tm->tm_sec < 0) + while (tm->tm_sec < 0) { tm->tm_sec += 60; tm->tm_min--; } - if (tm->tm_min < 0) + while (tm->tm_min < 0) { tm->tm_min += 60; tm->tm_hour--; } - if (tm->tm_hour < 0) + while (tm->tm_hour < 0) { tm->tm_hour += 24; tm->tm_mday--; } - if (tm->tm_mday < 0) + while (tm->tm_mday < 0) { if (dt1 < dt2) { @@ -2304,12 +2306,16 @@ timestamptz_age(PG_FUNCTION_ARGS) } } - if (tm->tm_mon < 0) + while (tm->tm_mon < 0) { tm->tm_mon += 12; tm->tm_year--; } + /* + * Note: we deliberately ignore any difference between tz1 and tz2. + */ + /* recover sign if necessary... */ if (dt1 < dt2) {