Skip to content

Commit 172ba45

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 4bf70c0 commit 172ba45

File tree

6 files changed

+90
-6
lines changed

6 files changed

+90
-6
lines changed

src/backend/commands/variable.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ check_timezone(char **newval, void **extra, GucSource source)
342342
#else
343343
myextra.CTimeZone = -interval->time;
344344
#endif
345+
myextra.session_timezone = pg_tzset_offset(myextra.CTimeZone);
345346
myextra.HasCTZSet = true;
346347

347348
pfree(interval);
@@ -356,6 +357,7 @@ check_timezone(char **newval, void **extra, GucSource source)
356357
{
357358
/* Here we change from SQL to Unix sign convention */
358359
myextra.CTimeZone = -hours * SECS_PER_HOUR;
360+
myextra.session_timezone = pg_tzset_offset(myextra.CTimeZone);
359361
myextra.HasCTZSet = true;
360362
}
361363
else

src/backend/utils/adt/datetime.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,12 +1447,6 @@ DetermineTimeZoneOffset(struct pg_tm * tm, pg_tz *tzp)
14471447
after_isdst;
14481448
int res;
14491449

1450-
if (tzp == session_timezone && HasCTZSet)
1451-
{
1452-
tm->tm_isdst = 0; /* for lack of a better idea */
1453-
return CTimeZone;
1454-
}
1455-
14561450
/*
14571451
* First, generate the pg_time_t value corresponding to the given
14581452
* y/m/d/h/m/s taken as GMT time. If this overflows, punt and decide the

src/include/pgtime.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ extern size_t pg_strftime(char *s, size_t max, const char *format,
5555
extern void pg_timezone_pre_initialize(void);
5656
extern void pg_timezone_initialize(void);
5757
extern pg_tz *pg_tzset(const char *tzname);
58+
extern pg_tz *pg_tzset_offset(long gmtoffset);
59+
5860
extern bool tz_acceptable(pg_tz *tz);
5961
extern bool pg_get_timezone_offset(const pg_tz *tz, long int *gmtoff);
6062
extern const char *pg_get_timezone_name(pg_tz *tz);

src/test/regress/expected/horology.out

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2899,3 +2899,33 @@ DETAIL: Value must be an integer.
28992899
SELECT to_timestamp('10000000000', 'FMYYYY');
29002900
ERROR: value for "YYYY" in source string is out of range
29012901
DETAIL: Value must be in the range -2147483648 to 2147483647.
2902+
--
2903+
-- Check behavior with SQL-style fixed-GMT-offset time zone (cf bug #8572)
2904+
--
2905+
SET TIME ZONE 'America/New_York';
2906+
SET TIME ZONE '-1.5';
2907+
SHOW TIME ZONE;
2908+
TimeZone
2909+
----------------------
2910+
@ 1 hour 30 mins ago
2911+
(1 row)
2912+
2913+
SELECT '2012-12-12 12:00'::timestamptz;
2914+
timestamptz
2915+
---------------------------------
2916+
Wed Dec 12 12:00:00 2012 -01:30
2917+
(1 row)
2918+
2919+
SELECT '2012-12-12 12:00 America/New_York'::timestamptz;
2920+
timestamptz
2921+
---------------------------------
2922+
Wed Dec 12 15:30:00 2012 -01:30
2923+
(1 row)
2924+
2925+
SELECT to_char('2012-12-12 12:00'::timestamptz, 'YYYY-MM-DD HH:MI:SS TZ');
2926+
to_char
2927+
----------------------
2928+
2012-12-12 12:00:00
2929+
(1 row)
2930+
2931+
RESET TIME ZONE;

src/test/regress/sql/horology.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,19 @@ SELECT to_timestamp('199711xy', 'YYYYMMDD');
455455

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

src/timezone/pgtz.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,46 @@ pg_tzset(const char *name)
13251325
return &tzp->tz;
13261326
}
13271327

1328+
/*
1329+
* Load a fixed-GMT-offset timezone.
1330+
* This is used for SQL-spec SET TIME ZONE INTERVAL 'foo' cases.
1331+
* It's otherwise equivalent to pg_tzset().
1332+
*
1333+
* The GMT offset is specified in seconds, positive values meaning west of
1334+
* Greenwich (ie, POSIX not ISO sign convention). However, we use ISO
1335+
* sign convention in the displayable abbreviation for the zone.
1336+
*/
1337+
pg_tz *
1338+
pg_tzset_offset(long gmtoffset)
1339+
{
1340+
long absoffset = (gmtoffset < 0) ? -gmtoffset : gmtoffset;
1341+
char offsetstr[64];
1342+
char tzname[128];
1343+
1344+
snprintf(offsetstr, sizeof(offsetstr),
1345+
"%02ld", absoffset / SECSPERHOUR);
1346+
absoffset %= SECSPERHOUR;
1347+
if (absoffset != 0)
1348+
{
1349+
snprintf(offsetstr + strlen(offsetstr),
1350+
sizeof(offsetstr) - strlen(offsetstr),
1351+
":%02ld", absoffset / SECSPERMIN);
1352+
absoffset %= SECSPERMIN;
1353+
if (absoffset != 0)
1354+
snprintf(offsetstr + strlen(offsetstr),
1355+
sizeof(offsetstr) - strlen(offsetstr),
1356+
":%02ld", absoffset);
1357+
}
1358+
if (gmtoffset > 0)
1359+
snprintf(tzname, sizeof(tzname), "<-%s>+%s",
1360+
offsetstr, offsetstr);
1361+
else
1362+
snprintf(tzname, sizeof(tzname), "<+%s>-%s",
1363+
offsetstr, offsetstr);
1364+
1365+
return pg_tzset(tzname);
1366+
}
1367+
13281368

13291369
/*
13301370
* Check whether timezone is acceptable.

0 commit comments

Comments
 (0)