aboutsummaryrefslogtreecommitdiff
path: root/src/include/utils/datetime.h
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/include/utils/datetime.h
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/include/utils/datetime.h')
-rw-r--r--src/include/utils/datetime.h48
1 files changed, 34 insertions, 14 deletions
diff --git a/src/include/utils/datetime.h b/src/include/utils/datetime.h
index 2e69503f96d..9b53ee38ccf 100644
--- a/src/include/utils/datetime.h
+++ b/src/include/utils/datetime.h
@@ -77,7 +77,7 @@ struct tzEntry;
#define BC 1
/*
- * Fields for time decoding.
+ * Field types for time decoding.
*
* Can't have more of these than there are bits in an unsigned int
* since these are turned into bit masks during parsing and decoding.
@@ -93,9 +93,9 @@ struct tzEntry;
#define YEAR 2
#define DAY 3
#define JULIAN 4
-#define TZ 5
-#define DTZ 6
-#define DTZMOD 7
+#define TZ 5 /* fixed-offset timezone abbreviation */
+#define DTZ 6 /* fixed-offset timezone abbrev, DST */
+#define DYNTZ 7 /* dynamic timezone abbreviation */
#define IGNORE_DTF 8
#define AMPM 9
#define HOUR 10
@@ -119,18 +119,24 @@ struct tzEntry;
#define DECADE 25
#define CENTURY 26
#define MILLENNIUM 27
+/* hack for parsing two-word timezone specs "MET DST" etc */
+#define DTZMOD 28 /* "DST" as a separate word */
/* reserved for unrecognized string values */
#define UNKNOWN_FIELD 31
/*
* Token field definitions for time parsing and decoding.
- * These need to fit into the datetkn table type.
- * At the moment, that means keep them within [-127,127].
- * These are also used for bit masks in DecodeDateDelta()
+ *
+ * Some field type codes (see above) use these as the "value" in datetktbl[].
+ * These are also used for bit masks in DecodeDateTime and friends
* so actually restrict them to within [0,31] for now.
* - thomas 97/06/19
- * Not all of these fields are used for masks in DecodeDateDelta
+ * Not all of these fields are used for masks in DecodeDateTime
* so allow some larger than 31. - thomas 1997-11-17
+ *
+ * Caution: there are undocumented assumptions in the code that most of these
+ * values are not equal to IGNORE_DTF nor RESERV. Be very careful when
+ * renumbering values in either of these apparently-independent lists :-(
*/
#define DTK_NUMBER 0
@@ -203,18 +209,27 @@ struct tzEntry;
/* keep this struct small; it gets used a lot */
typedef struct
{
- char token[TOKMAXLEN];
- char type;
- char value; /* this may be unsigned, alas */
+ char token[TOKMAXLEN + 1]; /* always NUL-terminated */
+ char type; /* see field type codes above */
+ int32 value; /* meaning depends on type */
} datetkn;
/* one of its uses is in tables of time zone abbreviations */
typedef struct TimeZoneAbbrevTable
{
- int numabbrevs;
+ Size tblsize; /* size in bytes of TimeZoneAbbrevTable */
+ int numabbrevs; /* number of entries in abbrevs[] array */
datetkn abbrevs[1]; /* VARIABLE LENGTH ARRAY */
+ /* DynamicZoneAbbrev(s) may follow the abbrevs[] array */
} TimeZoneAbbrevTable;
+/* auxiliary data for a dynamic time zone abbreviation (non-fixed-offset) */
+typedef struct DynamicZoneAbbrev
+{
+ pg_tz *tz; /* NULL if not yet looked up */
+ char zone[1]; /* zone name (var length, NUL-terminated) */
+} DynamicZoneAbbrev;
+
/* FMODULO()
* Macro to replace modf(), which is broken on some platforms.
@@ -296,6 +311,9 @@ extern void DateTimeParseError(int dterr, const char *str,
const char *datatype) __attribute__((noreturn));
extern int DetermineTimeZoneOffset(struct pg_tm * tm, pg_tz *tzp);
+extern int DetermineTimeZoneAbbrevOffset(struct pg_tm * tm, const char *abbr, pg_tz *tzp);
+extern int DetermineTimeZoneAbbrevOffsetTS(TimestampTz ts, const char *abbr,
+ pg_tz *tzp, int *isdst);
extern void EncodeDateOnly(struct pg_tm * tm, int style, char *str);
extern void EncodeTimeOnly(struct pg_tm * tm, fsec_t fsec, bool print_tz, int tz, int style, char *str);
@@ -305,6 +323,8 @@ extern void EncodeInterval(struct pg_tm * tm, fsec_t fsec, int style, char *str)
extern int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc,
struct pg_tm * tm);
+extern int DecodeTimezoneAbbrev(int field, char *lowtoken,
+ int *offset, pg_tz **tz);
extern int DecodeSpecial(int field, char *lowtoken, int *val);
extern int DecodeUnits(int field, char *lowtoken, int *val);
@@ -314,8 +334,8 @@ extern Node *TemporalTransform(int32 max_precis, Node *node);
extern bool CheckDateTokenTables(void);
-extern void ConvertTimeZoneAbbrevs(TimeZoneAbbrevTable *tbl,
- struct tzEntry *abbrevs, int n);
+extern TimeZoneAbbrevTable *ConvertTimeZoneAbbrevs(struct tzEntry *abbrevs,
+ int n);
extern void InstallTimeZoneAbbrevs(TimeZoneAbbrevTable *tbl);
extern Datum pg_timezone_abbrevs(PG_FUNCTION_ARGS);