aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/misc/tzparser.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2014-10-16 15:22:10 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2014-10-16 15:22:10 -0400
commitb2cbced9eef20692b51a84d68d469627f4fc43ac (patch)
tree21774c6c010312abd2045c5d7b73f63a6828ec2c /src/backend/utils/misc/tzparser.c
parent90063a7612e2730f7757c2a80ba384bbe7e35c4b (diff)
downloadpostgresql-b2cbced9eef20692b51a84d68d469627f4fc43ac.tar.gz
postgresql-b2cbced9eef20692b51a84d68d469627f4fc43ac.zip
Support timezone abbreviations that sometimes change.
Up to now, PG has assumed that any given timezone abbreviation (such as "EDT") represents a constant GMT offset in the usage of any particular region; we had a way to configure what that offset was, but not for it to be changeable over time. But, as with most things horological, this view of the world is too simplistic: there are numerous regions that have at one time or another switched to a different GMT offset but kept using the same timezone abbreviation. Almost the entire Russian Federation did that a few years ago, and later this month they're going to do it again. And there are similar examples all over the world. To cope with this, invent the notion of a "dynamic timezone abbreviation", which is one that is referenced to a particular underlying timezone (as defined in the IANA timezone database) and means whatever it currently means in that zone. For zones that use or have used daylight-savings time, the standard and DST abbreviations continue to have the property that you can specify standard or DST time and get that time offset whether or not DST was theoretically in effect at the time. However, the abbreviations mean what they meant at the time in question (or most recently before that time) rather than being absolutely fixed. The standard abbreviation-list files have been changed to use this behavior for abbreviations that have actually varied in meaning since 1970. The old simple-numeric definitions are kept for abbreviations that have not changed, since they are a bit faster to resolve. While this is clearly a new feature, it seems necessary to back-patch it into all active branches, because otherwise use of Russian zone abbreviations is going to become even more problematic than it already was. This change supersedes the changes in commit 513d06ded et al to modify the fixed meanings of the Russian abbreviations; since we've not shipped that yet, this will avoid an undesirably incompatible (not to mention incorrect) change in behavior for timestamps between 2011 and 2014. This patch makes some cosmetic changes in ecpglib to keep its usage of datetime lookup tables as similar as possible to the backend code, but doesn't do anything about the increasingly obsolete set of timezone abbreviation definitions that are hard-wired into ecpglib. Whatever we do about that will likely not be appropriate material for back-patching. Also, a potential free() of a garbage pointer after an out-of-memory failure in ecpglib has been fixed. This patch also fixes pre-existing bugs in DetermineTimeZoneOffset() that caused it to produce unexpected results near a timezone transition, if both the "before" and "after" states are marked as standard time. We'd only ever thought about or tested transitions between standard and DST time, but that's not what's happening when a zone simply redefines their base GMT offset. In passing, update the SGML documentation to refer to the Olson/zoneinfo/ zic timezone database as the "IANA" database, since it's now being maintained under the auspices of IANA.
Diffstat (limited to 'src/backend/utils/misc/tzparser.c')
-rw-r--r--src/backend/utils/misc/tzparser.c77
1 files changed, 45 insertions, 32 deletions
diff --git a/src/backend/utils/misc/tzparser.c b/src/backend/utils/misc/tzparser.c
index 6a5a7b39abf..a6a12ff06e3 100644
--- a/src/backend/utils/misc/tzparser.c
+++ b/src/backend/utils/misc/tzparser.c
@@ -63,13 +63,6 @@ validateTzEntry(tzEntry *tzentry)
tzentry->filename, tzentry->lineno);
return false;
}
- if (tzentry->offset % 900 != 0)
- {
- GUC_check_errmsg("time zone offset %d is not a multiple of 900 sec (15 min) in time zone file \"%s\", line %d",
- tzentry->offset,
- tzentry->filename, tzentry->lineno);
- return false;
- }
/*
* Sanity-check the offset: shouldn't exceed 14 hours
@@ -93,7 +86,11 @@ validateTzEntry(tzEntry *tzentry)
}
/*
- * Attempt to parse the line as a timezone abbrev spec (name, offset, dst)
+ * Attempt to parse the line as a timezone abbrev spec
+ *
+ * Valid formats are:
+ * name zone
+ * name offset dst
*
* Returns TRUE if OK, else false; data is stored in *tzentry
*/
@@ -116,7 +113,7 @@ splitTzLine(const char *filename, int lineno, char *line, tzEntry *tzentry)
filename, lineno);
return false;
}
- tzentry->abbrev = abbrev;
+ tzentry->abbrev = pstrdup(abbrev);
offset = strtok(NULL, WHITESPACE);
if (!offset)
@@ -125,25 +122,43 @@ splitTzLine(const char *filename, int lineno, char *line, tzEntry *tzentry)
filename, lineno);
return false;
}
- tzentry->offset = strtol(offset, &offset_endptr, 10);
- if (offset_endptr == offset || *offset_endptr != '\0')
- {
- GUC_check_errmsg("invalid number for time zone offset in time zone file \"%s\", line %d",
- filename, lineno);
- return false;
- }
- is_dst = strtok(NULL, WHITESPACE);
- if (is_dst && pg_strcasecmp(is_dst, "D") == 0)
+ /* We assume zone names don't begin with a digit or sign */
+ if (isdigit((unsigned char) *offset) || *offset == '+' || *offset == '-')
{
- tzentry->is_dst = true;
- remain = strtok(NULL, WHITESPACE);
+ tzentry->zone = NULL;
+ tzentry->offset = strtol(offset, &offset_endptr, 10);
+ if (offset_endptr == offset || *offset_endptr != '\0')
+ {
+ GUC_check_errmsg("invalid number for time zone offset in time zone file \"%s\", line %d",
+ filename, lineno);
+ return false;
+ }
+
+ is_dst = strtok(NULL, WHITESPACE);
+ if (is_dst && pg_strcasecmp(is_dst, "D") == 0)
+ {
+ tzentry->is_dst = true;
+ remain = strtok(NULL, WHITESPACE);
+ }
+ else
+ {
+ /* there was no 'D' dst specifier */
+ tzentry->is_dst = false;
+ remain = is_dst;
+ }
}
else
{
- /* there was no 'D' dst specifier */
+ /*
+ * Assume entry is a zone name. We do not try to validate it by
+ * looking up the zone, because that would force loading of a lot of
+ * zones that probably will never be used in the current session.
+ */
+ tzentry->zone = pstrdup(offset);
+ tzentry->offset = 0;
tzentry->is_dst = false;
- remain = is_dst;
+ remain = strtok(NULL, WHITESPACE);
}
if (!remain) /* no more non-whitespace chars */
@@ -201,8 +216,11 @@ addToArray(tzEntry **base, int *arraysize, int n,
/*
* Found a duplicate entry; complain unless it's the same.
*/
- if (midptr->offset == entry->offset &&
- midptr->is_dst == entry->is_dst)
+ if ((midptr->zone == NULL && entry->zone == NULL &&
+ midptr->offset == entry->offset &&
+ midptr->is_dst == entry->is_dst) ||
+ (midptr->zone != NULL && entry->zone != NULL &&
+ strcmp(midptr->zone, entry->zone) == 0))
{
/* return unchanged array */
return n;
@@ -210,6 +228,7 @@ addToArray(tzEntry **base, int *arraysize, int n,
if (override)
{
/* same abbrev but something is different, override */
+ midptr->zone = entry->zone;
midptr->offset = entry->offset;
midptr->is_dst = entry->is_dst;
return n;
@@ -239,9 +258,6 @@ addToArray(tzEntry **base, int *arraysize, int n,
memcpy(arrayptr, entry, sizeof(tzEntry));
- /* Must dup the abbrev to ensure it survives */
- arrayptr->abbrev = pstrdup(entry->abbrev);
-
return n + 1;
}
@@ -446,15 +462,12 @@ load_tzoffsets(const char *filename)
/* Parse the file(s) */
n = ParseTzFile(filename, 0, &array, &arraysize, 0);
- /* If no errors so far, allocate result and let datetime.c convert data */
+ /* If no errors so far, let datetime.c allocate memory & convert format */
if (n >= 0)
{
- result = malloc(offsetof(TimeZoneAbbrevTable, abbrevs) +
- n * sizeof(datetkn));
+ result = ConvertTimeZoneAbbrevs(array, n);
if (!result)
GUC_check_errmsg("out of memory");
- else
- ConvertTimeZoneAbbrevs(result, array, n);
}
/* Clean up */