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

Commit 631dc39

Browse files
committed
Fix some odd behaviors when using a SQL-style simple GMT offset timezone.
Formerly, when using a SQL-spec timezone setting with a fixed GMT offset (called a "brute force" timezone in the code), the session_timezone variable was not updated to match the nominal timezone; rather, all code was expected to ignore session_timezone if HasCTZSet was true. This is of course obviously fragile, though a search of the code finds only timeofday() failing to honor the rule. A bigger problem was that DetermineTimeZoneOffset() supposed that if its pg_tz parameter was pointer-equal to session_timezone, then HasCTZSet should override the parameter. This would cause datetime input containing an explicit zone name to be treated as referencing the brute-force zone instead, if the zone name happened to match the session timezone that had prevailed before installing the brute-force zone setting (as reported in bug #8572). The same malady could affect AT TIME ZONE operators. To fix, set up session_timezone so that it matches the brute-force zone specification, which we can do using the POSIX timezone definition syntax "<abbrev>offset", and get rid of the bogus lookaside check in DetermineTimeZoneOffset(). Aside from fixing the erroneous behavior in datetime parsing and AT TIME ZONE, this will cause the timeofday() function to print its result in the user-requested time zone rather than some previously-set zone. It might also affect results in third-party extensions, if there are any that make use of session_timezone without considering HasCTZSet, but in all cases the new behavior should be saner than before. Back-patch to all supported branches.
1 parent cacbdd7 commit 631dc39

File tree

6 files changed

+89
-6
lines changed

6 files changed

+89
-6
lines changed

src/backend/commands/variable.c

+2
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ check_timezone(char **newval, void **extra, GucSource source)
327327
#else
328328
myextra.CTimeZone = -interval->time;
329329
#endif
330+
myextra.session_timezone = pg_tzset_offset(myextra.CTimeZone);
330331
myextra.HasCTZSet = true;
331332

332333
pfree(interval);
@@ -341,6 +342,7 @@ check_timezone(char **newval, void **extra, GucSource source)
341342
{
342343
/* Here we change from SQL to Unix sign convention */
343344
myextra.CTimeZone = -hours * SECS_PER_HOUR;
345+
myextra.session_timezone = pg_tzset_offset(myextra.CTimeZone);
344346
myextra.HasCTZSet = true;
345347
}
346348
else

src/backend/utils/adt/datetime.c

-6
Original file line numberDiff line numberDiff line change
@@ -1465,12 +1465,6 @@ DetermineTimeZoneOffset(struct pg_tm * tm, pg_tz *tzp)
14651465
after_isdst;
14661466
int res;
14671467

1468-
if (tzp == session_timezone && HasCTZSet)
1469-
{
1470-
tm->tm_isdst = 0; /* for lack of a better idea */
1471-
return CTimeZone;
1472-
}
1473-
14741468
/*
14751469
* First, generate the pg_time_t value corresponding to the given
14761470
* y/m/d/h/m/s taken as GMT time. If this overflows, punt and decide the

src/include/pgtime.h

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ extern pg_tz *log_timezone;
6868

6969
extern void pg_timezone_initialize(void);
7070
extern pg_tz *pg_tzset(const char *tzname);
71+
extern pg_tz *pg_tzset_offset(long gmtoffset);
7172

7273
extern pg_tzenum *pg_tzenumerate_start(void);
7374
extern pg_tz *pg_tzenumerate_next(pg_tzenum *dir);

src/test/regress/expected/horology.out

+30
Original file line numberDiff line numberDiff line change
@@ -2935,3 +2935,33 @@ DETAIL: Value must be an integer.
29352935
SELECT to_timestamp('10000000000', 'FMYYYY');
29362936
ERROR: value for "YYYY" in source string is out of range
29372937
DETAIL: Value must be in the range -2147483648 to 2147483647.
2938+
--
2939+
-- Check behavior with SQL-style fixed-GMT-offset time zone (cf bug #8572)
2940+
--
2941+
SET TIME ZONE 'America/New_York';
2942+
SET TIME ZONE '-1.5';
2943+
SHOW TIME ZONE;
2944+
TimeZone
2945+
----------------------
2946+
@ 1 hour 30 mins ago
2947+
(1 row)
2948+
2949+
SELECT '2012-12-12 12:00'::timestamptz;
2950+
timestamptz
2951+
---------------------------------
2952+
Wed Dec 12 12:00:00 2012 -01:30
2953+
(1 row)
2954+
2955+
SELECT '2012-12-12 12:00 America/New_York'::timestamptz;
2956+
timestamptz
2957+
---------------------------------
2958+
Wed Dec 12 15:30:00 2012 -01:30
2959+
(1 row)
2960+
2961+
SELECT to_char('2012-12-12 12:00'::timestamptz, 'YYYY-MM-DD HH:MI:SS TZ');
2962+
to_char
2963+
----------------------
2964+
2012-12-12 12:00:00
2965+
(1 row)
2966+
2967+
RESET TIME ZONE;

src/test/regress/sql/horology.sql

+16
Original file line numberDiff line numberDiff line change
@@ -461,3 +461,19 @@ SELECT to_timestamp('199711xy', 'YYYYMMDD');
461461

462462
-- Input that doesn't fit in an int:
463463
SELECT to_timestamp('10000000000', 'FMYYYY');
464+
465+
--
466+
-- Check behavior with SQL-style fixed-GMT-offset time zone (cf bug #8572)
467+
--
468+
469+
SET TIME ZONE 'America/New_York';
470+
SET TIME ZONE '-1.5';
471+
472+
SHOW TIME ZONE;
473+
474+
SELECT '2012-12-12 12:00'::timestamptz;
475+
SELECT '2012-12-12 12:00 America/New_York'::timestamptz;
476+
477+
SELECT to_char('2012-12-12 12:00'::timestamptz, 'YYYY-MM-DD HH:MI:SS TZ');
478+
479+
RESET TIME ZONE;

src/timezone/pgtz.c

+40
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,46 @@ pg_tzset(const char *name)
288288
return &tzp->tz;
289289
}
290290

291+
/*
292+
* Load a fixed-GMT-offset timezone.
293+
* This is used for SQL-spec SET TIME ZONE INTERVAL 'foo' cases.
294+
* It's otherwise equivalent to pg_tzset().
295+
*
296+
* The GMT offset is specified in seconds, positive values meaning west of
297+
* Greenwich (ie, POSIX not ISO sign convention). However, we use ISO
298+
* sign convention in the displayable abbreviation for the zone.
299+
*/
300+
pg_tz *
301+
pg_tzset_offset(long gmtoffset)
302+
{
303+
long absoffset = (gmtoffset < 0) ? -gmtoffset : gmtoffset;
304+
char offsetstr[64];
305+
char tzname[128];
306+
307+
snprintf(offsetstr, sizeof(offsetstr),
308+
"%02ld", absoffset / SECSPERHOUR);
309+
absoffset %= SECSPERHOUR;
310+
if (absoffset != 0)
311+
{
312+
snprintf(offsetstr + strlen(offsetstr),
313+
sizeof(offsetstr) - strlen(offsetstr),
314+
":%02ld", absoffset / SECSPERMIN);
315+
absoffset %= SECSPERMIN;
316+
if (absoffset != 0)
317+
snprintf(offsetstr + strlen(offsetstr),
318+
sizeof(offsetstr) - strlen(offsetstr),
319+
":%02ld", absoffset);
320+
}
321+
if (gmtoffset > 0)
322+
snprintf(tzname, sizeof(tzname), "<-%s>+%s",
323+
offsetstr, offsetstr);
324+
else
325+
snprintf(tzname, sizeof(tzname), "<+%s>-%s",
326+
offsetstr, offsetstr);
327+
328+
return pg_tzset(tzname);
329+
}
330+
291331

292332
/*
293333
* Initialize timezone library

0 commit comments

Comments
 (0)