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

Commit 1e6457d

Browse files
committed
Fix timestamptz_age() to do calculation in local timezone not GMT, per bug 1332.
1 parent 4828445 commit 1e6457d

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/backend/utils/adt/timestamp.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
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
*/
@@ -17,7 +17,6 @@
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)
23522351
tm->tm_year = -tm->tm_year;
23532352
}
23542353

2355-
if (tm->tm_sec < 0)
2354+
while (tm->tm_sec < 0)
23562355
{
23572356
tm->tm_sec += 60;
23582357
tm->tm_min--;
23592358
}
23602359

2361-
if (tm->tm_min < 0)
2360+
while (tm->tm_min < 0)
23622361
{
23632362
tm->tm_min += 60;
23642363
tm->tm_hour--;
23652364
}
23662365

2367-
if (tm->tm_hour < 0)
2366+
while (tm->tm_hour < 0)
23682367
{
23692368
tm->tm_hour += 24;
23702369
tm->tm_mday--;
23712370
}
23722371

2373-
if (tm->tm_mday < 0)
2372+
while (tm->tm_mday < 0)
23742373
{
23752374
if (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
{
23892388
tm->tm_mon += 12;
23902389
tm->tm_year--;
@@ -2437,11 +2436,14 @@ timestamptz_age(PG_FUNCTION_ARGS)
24372436
*tm1 = &tt1;
24382437
struct pg_tm tt2,
24392438
*tm2 = &tt2;
2439+
int tz1;
2440+
int tz2;
2441+
char *tzn;
24402442

24412443
result = (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
{
24462448
fsec = (fsec1 - fsec2);
24472449
tm->tm_sec = (tm1->tm_sec - tm2->tm_sec);
@@ -2463,25 +2465,25 @@ timestamptz_age(PG_FUNCTION_ARGS)
24632465
tm->tm_year = -tm->tm_year;
24642466
}
24652467

2466-
if (tm->tm_sec < 0)
2468+
while (tm->tm_sec < 0)
24672469
{
24682470
tm->tm_sec += 60;
24692471
tm->tm_min--;
24702472
}
24712473

2472-
if (tm->tm_min < 0)
2474+
while (tm->tm_min < 0)
24732475
{
24742476
tm->tm_min += 60;
24752477
tm->tm_hour--;
24762478
}
24772479

2478-
if (tm->tm_hour < 0)
2480+
while (tm->tm_hour < 0)
24792481
{
24802482
tm->tm_hour += 24;
24812483
tm->tm_mday--;
24822484
}
24832485

2484-
if (tm->tm_mday < 0)
2486+
while (tm->tm_mday < 0)
24852487
{
24862488
if (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
{
25002502
tm->tm_mon += 12;
25012503
tm->tm_year--;
25022504
}
25032505

2506+
/*
2507+
* Note: we deliberately ignore any difference between tz1 and tz2.
2508+
*/
2509+
25042510
/* recover sign if necessary... */
25052511
if (dt1 < dt2)
25062512
{

0 commit comments

Comments
 (0)