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

Commit 32c9950

Browse files
committed
Don't require dynamic timezone abbreviations to match underlying time zone.
Previously, we threw an error if a dynamic timezone abbreviation did not match any abbreviation recorded in the referenced IANA time zone entry. That seemed like a good consistency check at the time, but it turns out that a number of the abbreviations in the IANA database are things that Olson and crew made up out of whole cloth. Their current policy is to remove such names in favor of using simple numeric offsets. Perhaps unsurprisingly, a lot of these made-up abbreviations have varied in meaning over time, which meant that our commit b2cbced and later changes made them into dynamic abbreviations. So with newer IANA database versions that don't mention these abbreviations at all, we fail, as reported in bug #14307 from Neil Anderson. It's worse than just a few unused-in-the-wild abbreviations not working, because the pg_timezone_abbrevs view stops working altogether (since its underlying function tries to compute the whole view result in one call). We considered deleting these abbreviations from our abbreviations list, but the problem with that is that we can't stay ahead of possible future IANA changes. Instead, let's leave the abbreviations list alone, and treat any "orphaned" dynamic abbreviation as just meaning the referenced time zone. It will behave a bit differently than it used to, in that you can't any longer override the zone's standard vs. daylight rule by using the "wrong" abbreviation of a pair, but that's better than failing entirely. (Also, this solution can be interpreted as adding a small new feature, which is that any abbreviation a user wants can be defined as referencing a time zone name.) Back-patch to all supported branches, since this problem affects all of them when using tzdata 2016f or newer. Report: <20160902031551.15674.67337@wrigleys.postgresql.org> Discussion: <6189.1472820913@sss.pgh.pa.us>
1 parent 3fc489c commit 32c9950

File tree

5 files changed

+132
-32
lines changed

5 files changed

+132
-32
lines changed

doc/src/sgml/catalogs.sgml

+7
Original file line numberDiff line numberDiff line change
@@ -9811,6 +9811,13 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
98119811
</tgroup>
98129812
</table>
98139813

9814+
<para>
9815+
While most timezone abbreviations represent fixed offsets from UTC,
9816+
there are some that have historically varied in value
9817+
(see <xref linkend="datetime-config-files"> for more information).
9818+
In such cases this view presents their current meaning.
9819+
</para>
9820+
98149821
</sect1>
98159822

98169823
<sect1 id="view-pg-timezone-names">

doc/src/sgml/datetime.sgml

+30-11
Original file line numberDiff line numberDiff line change
@@ -384,19 +384,38 @@
384384

385385
<para>
386386
A <replaceable>zone_abbreviation</replaceable> is just the abbreviation
387-
being defined. The <replaceable>offset</replaceable> is the equivalent
388-
offset in seconds from UTC, positive being east from Greenwich and
389-
negative being west. For example, -18000 would be five hours west
390-
of Greenwich, or North American east coast standard time. <literal>D</>
391-
indicates that the zone name represents local daylight-savings time rather
392-
than standard time. Alternatively, a <replaceable>time_zone_name</> can
393-
be given, in which case that time zone definition is consulted, and the
394-
abbreviation's meaning in that zone is used. This alternative is
395-
recommended only for abbreviations whose meaning has historically varied,
396-
as looking up the meaning is noticeably more expensive than just using
397-
a fixed integer value.
387+
being defined. An <replaceable>offset</replaceable> is an integer giving
388+
the equivalent offset in seconds from UTC, positive being east from
389+
Greenwich and negative being west. For example, -18000 would be five
390+
hours west of Greenwich, or North American east coast standard time.
391+
<literal>D</> indicates that the zone name represents local
392+
daylight-savings time rather than standard time.
398393
</para>
399394

395+
<para>
396+
Alternatively, a <replaceable>time_zone_name</> can be given, referencing
397+
a zone name defined in the IANA timezone database. The zone's definition
398+
is consulted to see whether the abbreviation is or has been in use in
399+
that zone, and if so, the appropriate meaning is used &mdash; that is,
400+
the meaning that was currently in use at the timestamp whose value is
401+
being determined, or the meaning in use immediately before that if it
402+
wasn't current at that time, or the oldest meaning if it was used only
403+
after that time. This behavior is essential for dealing with
404+
abbreviations whose meaning has historically varied. It is also allowed
405+
to define an abbreviation in terms of a zone name in which that
406+
abbreviation does not appear; then using the abbreviation is just
407+
equivalent to writing out the zone name.
408+
</para>
409+
410+
<tip>
411+
<para>
412+
Using a simple integer <replaceable>offset</replaceable> is preferred
413+
when defining an abbreviation whose offset from UTC has never changed,
414+
as such abbreviations are much cheaper to process than those that
415+
require consulting a time zone definition.
416+
</para>
417+
</tip>
418+
400419
<para>
401420
The <literal>@INCLUDE</> syntax allows inclusion of another file in the
402421
<filename>.../share/timezonesets/</> directory. Inclusion can be nested,

src/backend/utils/adt/datetime.c

+64-21
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ static void AdjustFractDays(double frac, struct pg_tm * tm, fsec_t *fsec,
5656
int scale);
5757
static int DetermineTimeZoneOffsetInternal(struct pg_tm * tm, pg_tz *tzp,
5858
pg_time_t *tp);
59-
static int DetermineTimeZoneAbbrevOffsetInternal(pg_time_t t, const char *abbr,
60-
pg_tz *tzp, int *isdst);
59+
static bool DetermineTimeZoneAbbrevOffsetInternal(pg_time_t t,
60+
const char *abbr, pg_tz *tzp,
61+
int *offset, int *isdst);
6162
static pg_tz *FetchDynamicTimeZone(TimeZoneAbbrevTable *tbl, const datetkn *tp);
6263

6364

@@ -1689,19 +1690,40 @@ DetermineTimeZoneOffsetInternal(struct pg_tm * tm, pg_tz *tzp, pg_time_t *tp)
16891690
* This differs from the behavior of DetermineTimeZoneOffset() in that a
16901691
* standard-time or daylight-time abbreviation forces use of the corresponding
16911692
* GMT offset even when the zone was then in DS or standard time respectively.
1693+
* (However, that happens only if we can match the given abbreviation to some
1694+
* abbreviation that appears in the IANA timezone data. Otherwise, we fall
1695+
* back to doing DetermineTimeZoneOffset().)
16921696
*/
16931697
int
16941698
DetermineTimeZoneAbbrevOffset(struct pg_tm * tm, const char *abbr, pg_tz *tzp)
16951699
{
16961700
pg_time_t t;
1701+
int zone_offset;
1702+
int abbr_offset;
1703+
int abbr_isdst;
16971704

16981705
/*
16991706
* Compute the UTC time we want to probe at. (In event of overflow, we'll
17001707
* probe at the epoch, which is a bit random but probably doesn't matter.)
17011708
*/
1702-
(void) DetermineTimeZoneOffsetInternal(tm, tzp, &t);
1709+
zone_offset = DetermineTimeZoneOffsetInternal(tm, tzp, &t);
17031710

1704-
return DetermineTimeZoneAbbrevOffsetInternal(t, abbr, tzp, &tm->tm_isdst);
1711+
/*
1712+
* Try to match the abbreviation to something in the zone definition.
1713+
*/
1714+
if (DetermineTimeZoneAbbrevOffsetInternal(t, abbr, tzp,
1715+
&abbr_offset, &abbr_isdst))
1716+
{
1717+
/* Success, so use the abbrev-specific answers. */
1718+
tm->tm_isdst = abbr_isdst;
1719+
return abbr_offset;
1720+
}
1721+
1722+
/*
1723+
* No match, so use the answers we already got from
1724+
* DetermineTimeZoneOffsetInternal.
1725+
*/
1726+
return zone_offset;
17051727
}
17061728

17071729

@@ -1715,19 +1737,41 @@ DetermineTimeZoneAbbrevOffsetTS(TimestampTz ts, const char *abbr,
17151737
pg_tz *tzp, int *isdst)
17161738
{
17171739
pg_time_t t = timestamptz_to_time_t(ts);
1740+
int zone_offset;
1741+
int abbr_offset;
1742+
int tz;
1743+
struct pg_tm tm;
1744+
fsec_t fsec;
17181745

1719-
return DetermineTimeZoneAbbrevOffsetInternal(t, abbr, tzp, isdst);
1746+
/*
1747+
* If the abbrev matches anything in the zone data, this is pretty easy.
1748+
*/
1749+
if (DetermineTimeZoneAbbrevOffsetInternal(t, abbr, tzp,
1750+
&abbr_offset, isdst))
1751+
return abbr_offset;
1752+
1753+
/*
1754+
* Else, break down the timestamp so we can use DetermineTimeZoneOffset.
1755+
*/
1756+
if (timestamp2tm(ts, &tz, &tm, &fsec, NULL, tzp) != 0)
1757+
ereport(ERROR,
1758+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1759+
errmsg("timestamp out of range")));
1760+
1761+
zone_offset = DetermineTimeZoneOffset(&tm, tzp);
1762+
*isdst = tm.tm_isdst;
1763+
return zone_offset;
17201764
}
17211765

17221766

17231767
/* DetermineTimeZoneAbbrevOffsetInternal()
17241768
*
17251769
* Workhorse for above two functions: work from a pg_time_t probe instant.
1726-
* DST status is returned into *isdst.
1770+
* On success, return GMT offset and DST status into *offset and *isdst.
17271771
*/
1728-
static int
1729-
DetermineTimeZoneAbbrevOffsetInternal(pg_time_t t, const char *abbr,
1730-
pg_tz *tzp, int *isdst)
1772+
static bool
1773+
DetermineTimeZoneAbbrevOffsetInternal(pg_time_t t, const char *abbr, pg_tz *tzp,
1774+
int *offset, int *isdst)
17311775
{
17321776
char upabbr[TZ_STRLEN_MAX + 1];
17331777
unsigned char *p;
@@ -1739,18 +1783,17 @@ DetermineTimeZoneAbbrevOffsetInternal(pg_time_t t, const char *abbr,
17391783
*p = pg_toupper(*p);
17401784

17411785
/* Look up the abbrev's meaning at this time in this zone */
1742-
if (!pg_interpret_timezone_abbrev(upabbr,
1743-
&t,
1744-
&gmtoff,
1745-
isdst,
1746-
tzp))
1747-
ereport(ERROR,
1748-
(errcode(ERRCODE_CONFIG_FILE_ERROR),
1749-
errmsg("time zone abbreviation \"%s\" is not used in time zone \"%s\"",
1750-
abbr, pg_get_timezone_name(tzp))));
1751-
1752-
/* Change sign to agree with DetermineTimeZoneOffset() */
1753-
return (int) -gmtoff;
1786+
if (pg_interpret_timezone_abbrev(upabbr,
1787+
&t,
1788+
&gmtoff,
1789+
isdst,
1790+
tzp))
1791+
{
1792+
/* Change sign to agree with DetermineTimeZoneOffset() */
1793+
*offset = (int) -gmtoff;
1794+
return true;
1795+
}
1796+
return false;
17541797
}
17551798

17561799

src/test/regress/expected/timestamptz.out

+20
Original file line numberDiff line numberDiff line change
@@ -2603,3 +2603,23 @@ SELECT '2007-12-09 07:30:00 UTC'::timestamptz AT TIME ZONE 'VET';
26032603
Sun Dec 09 03:00:00 2007
26042604
(1 row)
26052605

2606+
--
2607+
-- Test that the pg_timezone_names and pg_timezone_abbrevs views are
2608+
-- more-or-less working. We can't test their contents in any great detail
2609+
-- without the outputs changing anytime IANA updates the underlying data,
2610+
-- but it seems reasonable to expect at least one entry per major meridian.
2611+
-- (At the time of writing, the actual counts are around 38 because of
2612+
-- zones using fractional GMT offsets, so this is a pretty loose test.)
2613+
--
2614+
select count(distinct utc_offset) >= 24 as ok from pg_timezone_names;
2615+
ok
2616+
----
2617+
t
2618+
(1 row)
2619+
2620+
select count(distinct utc_offset) >= 24 as ok from pg_timezone_abbrevs;
2621+
ok
2622+
----
2623+
t
2624+
(1 row)
2625+

src/test/regress/sql/timestamptz.sql

+11
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,14 @@ SELECT '2007-12-09 07:00:00 UTC'::timestamptz AT TIME ZONE 'VET';
468468
SELECT '2007-12-09 07:00:01 UTC'::timestamptz AT TIME ZONE 'VET';
469469
SELECT '2007-12-09 07:29:59 UTC'::timestamptz AT TIME ZONE 'VET';
470470
SELECT '2007-12-09 07:30:00 UTC'::timestamptz AT TIME ZONE 'VET';
471+
472+
--
473+
-- Test that the pg_timezone_names and pg_timezone_abbrevs views are
474+
-- more-or-less working. We can't test their contents in any great detail
475+
-- without the outputs changing anytime IANA updates the underlying data,
476+
-- but it seems reasonable to expect at least one entry per major meridian.
477+
-- (At the time of writing, the actual counts are around 38 because of
478+
-- zones using fractional GMT offsets, so this is a pretty loose test.)
479+
--
480+
select count(distinct utc_offset) >= 24 as ok from pg_timezone_names;
481+
select count(distinct utc_offset) >= 24 as ok from pg_timezone_abbrevs;

0 commit comments

Comments
 (0)