Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit631dc39

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_timezonevariable was not updated to match the nominal timezone; rather, all codewas expected to ignore session_timezone if HasCTZSet was true. This isof course obviously fragile, though a search of the code finds onlytimeofday() failing to honor the rule. A bigger problem was thatDetermineTimeZoneOffset() supposed that if its pg_tz parameter waspointer-equal to session_timezone, then HasCTZSet should override theparameter. This would cause datetime input containing an explicit zonename to be treated as referencing the brute-force zone instead, if thezone name happened to match the session timezone that had prevailedbefore 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 zonespecification, which we can do using the POSIX timezone definition syntax"<abbrev>offset", and get rid of the bogus lookaside check inDetermineTimeZoneOffset(). Aside from fixing the erroneous behavior indatetime parsing and AT TIME ZONE, this will cause the timeofday() functionto print its result in the user-requested time zone rather than somepreviously-set zone. It might also affect results in third-partyextensions, if there are any that make use of session_timezone withoutconsidering HasCTZSet, but in all cases the new behavior should be sanerthan before.Back-patch to all supported branches.
1 parentcacbdd7 commit631dc39

File tree

6 files changed

+89
-6
lines changed

6 files changed

+89
-6
lines changed

‎src/backend/commands/variable.c

Lines changed: 2 additions & 0 deletions
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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,12 +1465,6 @@ DetermineTimeZoneOffset(struct pg_tm * tm, pg_tz *tzp)
14651465
after_isdst;
14661466
intres;
14671467

1468-
if (tzp==session_timezone&&HasCTZSet)
1469-
{
1470-
tm->tm_isdst=0;/* for lack of a better idea */
1471-
returnCTimeZone;
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ extern pg_tz *log_timezone;
6868

6969
externvoidpg_timezone_initialize(void);
7070
externpg_tz*pg_tzset(constchar*tzname);
71+
externpg_tz*pg_tzset_offset(longgmtoffset);
7172

7273
externpg_tzenum*pg_tzenumerate_start(void);
7374
externpg_tz*pg_tzenumerate_next(pg_tzenum*dir);

‎src/test/regress/expected/horology.out

Lines changed: 30 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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+
SETTIME ZONE'America/New_York';
470+
SETTIME ZONE'-1.5';
471+
472+
SHOWTIME 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+
RESETTIME ZONE;

‎src/timezone/pgtz.c

Lines changed: 40 additions & 0 deletions
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(longgmtoffset)
302+
{
303+
longabsoffset= (gmtoffset<0) ?-gmtoffset :gmtoffset;
304+
charoffsetstr[64];
305+
chartzname[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+
returnpg_tzset(tzname);
329+
}
330+
291331

292332
/*
293333
* Initialize timezone library

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp