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-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=de9c2bf321076bc8f7fdbb1a305ec7c96eab0dd1;p=users%2Fbernd%2Fpostgres.git 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 47708e6ea9..be879c8800 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -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) {