]> granicus.if.org Git - postgresql/commitdiff
Fix some odd behaviors when using a SQL-style simple GMT offset timezone.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 1 Nov 2013 16:13:33 +0000 (12:13 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 1 Nov 2013 16:13:33 +0000 (12:13 -0400)
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.

src/backend/commands/variable.c
src/backend/utils/adt/datetime.c
src/include/pgtime.h
src/test/regress/expected/horology.out
src/test/regress/sql/horology.sql
src/timezone/pgtz.c

index c078443c5e0d8272dde8762bca9ec4068cc7ebd4..403444493e58c91571da1eca64100e6823d4375b 100644 (file)
@@ -302,6 +302,7 @@ assign_timezone(const char *value, bool doit, GucSource source)
 #else
                        CTimeZone = -interval->time;
 #endif
+                       session_timezone = pg_tzset_offset(CTimeZone);
 
                        HasCTZSet = true;
                }
@@ -319,6 +320,7 @@ assign_timezone(const char *value, bool doit, GucSource source)
                        {
                                /* Here we change from SQL to Unix sign convention */
                                CTimeZone = -hours * SECS_PER_HOUR;
+                               session_timezone = pg_tzset_offset(CTimeZone);
                                HasCTZSet = true;
                        }
                }
index 899aeb7ae717b82133a2d5d6274f074a145a91f1..95d4556c734fcf36dc2a9aa9b155cd7b7ce16afb 100644 (file)
@@ -1447,12 +1447,6 @@ DetermineTimeZoneOffset(struct pg_tm * tm, pg_tz *tzp)
                                after_isdst;
        int                     res;
 
-       if (tzp == session_timezone && HasCTZSet)
-       {
-               tm->tm_isdst = 0;               /* for lack of a better idea */
-               return CTimeZone;
-       }
-
        /*
         * First, generate the pg_time_t value corresponding to the given
         * y/m/d/h/m/s taken as GMT time.  If this overflows, punt and decide the
index 60f3773e1279a5e883d505757f7cbb53c9bcd6e7..7079654669909904a066f7e11ba5d07ee9ff6e6f 100644 (file)
@@ -55,6 +55,8 @@ extern size_t pg_strftime(char *s, size_t max, const char *format,
 extern void pg_timezone_pre_initialize(void);
 extern void pg_timezone_initialize(void);
 extern pg_tz *pg_tzset(const char *tzname);
+extern pg_tz *pg_tzset_offset(long gmtoffset);
+
 extern bool tz_acceptable(pg_tz *tz);
 extern bool pg_get_timezone_offset(const pg_tz *tz, long int *gmtoff);
 extern const char *pg_get_timezone_name(pg_tz *tz);
index b13f7d7c5b531b41aaab96439748265b1e0a3609..bdcae7c419e20016ee1ae367184a64f7aa132fe8 100644 (file)
@@ -2899,3 +2899,33 @@ DETAIL:  Value must be an integer.
 SELECT to_timestamp('10000000000', 'FMYYYY');
 ERROR:  value for "YYYY" in source string is out of range
 DETAIL:  Value must be in the range -2147483648 to 2147483647.
+--
+-- Check behavior with SQL-style fixed-GMT-offset time zone (cf bug #8572)
+--
+SET TIME ZONE 'America/New_York';
+SET TIME ZONE '-1.5';
+SHOW TIME ZONE;
+       TimeZone       
+----------------------
+ @ 1 hour 30 mins ago
+(1 row)
+
+SELECT '2012-12-12 12:00'::timestamptz;
+           timestamptz           
+---------------------------------
+ Wed Dec 12 12:00:00 2012 -01:30
+(1 row)
+
+SELECT '2012-12-12 12:00 America/New_York'::timestamptz;
+           timestamptz           
+---------------------------------
+ Wed Dec 12 15:30:00 2012 -01:30
+(1 row)
+
+SELECT to_char('2012-12-12 12:00'::timestamptz, 'YYYY-MM-DD HH:MI:SS TZ');
+       to_char        
+----------------------
+ 2012-12-12 12:00:00 
+(1 row)
+
+RESET TIME ZONE;
index 97ff9f20c798f421df911c44144557de5bb79c08..156b07033bccd5b846208faa7e7704fb8c1885de 100644 (file)
@@ -455,3 +455,19 @@ SELECT to_timestamp('199711xy', 'YYYYMMDD');
 
 -- Input that doesn't fit in an int:
 SELECT to_timestamp('10000000000', 'FMYYYY');
+
+--
+-- Check behavior with SQL-style fixed-GMT-offset time zone (cf bug #8572)
+--
+
+SET TIME ZONE 'America/New_York';
+SET TIME ZONE '-1.5';
+
+SHOW TIME ZONE;
+
+SELECT '2012-12-12 12:00'::timestamptz;
+SELECT '2012-12-12 12:00 America/New_York'::timestamptz;
+
+SELECT to_char('2012-12-12 12:00'::timestamptz, 'YYYY-MM-DD HH:MI:SS TZ');
+
+RESET TIME ZONE;
index fcca754fd14e20a7e320c069a7348a3acbba663a..13e3bf194400dacbea4f964e2d62805997ba93a5 100644 (file)
@@ -1325,6 +1325,46 @@ pg_tzset(const char *name)
        return &tzp->tz;
 }
 
+/*
+ * Load a fixed-GMT-offset timezone.
+ * This is used for SQL-spec SET TIME ZONE INTERVAL 'foo' cases.
+ * It's otherwise equivalent to pg_tzset().
+ *
+ * The GMT offset is specified in seconds, positive values meaning west of
+ * Greenwich (ie, POSIX not ISO sign convention).  However, we use ISO
+ * sign convention in the displayable abbreviation for the zone.
+ */
+pg_tz *
+pg_tzset_offset(long gmtoffset)
+{
+       long            absoffset = (gmtoffset < 0) ? -gmtoffset : gmtoffset;
+       char            offsetstr[64];
+       char            tzname[128];
+
+       snprintf(offsetstr, sizeof(offsetstr),
+                        "%02ld", absoffset / SECSPERHOUR);
+       absoffset %= SECSPERHOUR;
+       if (absoffset != 0)
+       {
+               snprintf(offsetstr + strlen(offsetstr),
+                                sizeof(offsetstr) - strlen(offsetstr),
+                                ":%02ld", absoffset / SECSPERMIN);
+               absoffset %= SECSPERMIN;
+               if (absoffset != 0)
+                       snprintf(offsetstr + strlen(offsetstr),
+                                        sizeof(offsetstr) - strlen(offsetstr),
+                                        ":%02ld", absoffset);
+       }
+       if (gmtoffset > 0)
+               snprintf(tzname, sizeof(tzname), "<-%s>+%s",
+                                offsetstr, offsetstr);
+       else
+               snprintf(tzname, sizeof(tzname), "<+%s>-%s",
+                                offsetstr, offsetstr);
+
+       return pg_tzset(tzname);
+}
+
 
 /*
  * Check whether timezone is acceptable.