diff --git a/doc/src/sgml/datatype.sgml b/doc/src/sgml/datatype.sgml index 1e247ec033e..c81c0bb9700 100644 --- a/doc/src/sgml/datatype.sgml +++ b/doc/src/sgml/datatype.sgml @@ -1,5 +1,5 @@ @@ -245,14 +245,15 @@ $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.106.2.1 2002/11/10 12:45: Compatibility - The following types (or spellings thereof) are specified by SQL: - bit, bit varying, boolean, - char, character, character - varying, varchar, date, - double precision, integer, - interval, numeric, decimal, - real, smallint, time, - timestamp (both with or without time zone). + The following types (or spellings thereof) are specified by + SQL: bit, bit + varying, boolean, char, + character, character varying, + varchar, date, double + precision, integer, interval, + numeric, decimal, real, + smallint, time, timestamp + (both with or without time zone). @@ -464,11 +465,12 @@ $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.106.2.1 2002/11/10 12:45: - SQL only specifies the integer types integer (or - int) and smallint. The type - bigint, and the type names int2, - int4, and int8 are extensions, which - are shared with various other SQL database systems. + SQL only specifies the integer types + integer (or int) and + smallint. The type bigint, and the + type names int2, int4, and + int8 are extensions, which are shared with various + other SQL database systems. @@ -536,13 +538,15 @@ NUMERIC(precision) NUMERIC without any precision or scale creates a column in which numeric - values of any precision and scale can be stored, up to the implementation - limit on precision. A column of this kind will not coerce input - values to any particular scale, whereas numeric columns - with a declared scale will coerce input values to that scale. - (The SQL standard requires a default scale of 0, i.e., coercion to - integer precision. We find this a bit useless. If you're concerned about - portability, always specify the precision and scale explicitly.) + values of any precision and scale can be stored, up to the + implementation limit on precision. A column of this kind will + not coerce input values to any particular scale, whereas + numeric columns with a declared scale will coerce + input values to that scale. (The SQL standard + requires a default scale of 0, i.e., coercion to integer + precision. We find this a bit useless. If you're concerned + about portability, always specify the precision and scale + explicitly.) @@ -554,7 +558,8 @@ NUMERIC The types decimal and numeric are - equivalent. Both types are part of the SQL standard. + equivalent. Both types are part of the SQL + standard. @@ -806,7 +811,8 @@ CREATE TABLE tablename ( shows the - general-purpose character types available in PostgreSQL. + general-purpose character types available in + PostgreSQL. @@ -818,11 +824,12 @@ CREATE TABLE tablename ( longer string into a column of these types will result in an error, unless the excess characters are all spaces, in which case the string will be truncated to the maximum length. (This - somewhat bizarre exception is required by the SQL standard.) If - the string to be stored is shorter than the declared length, - values of type character will be space-padded; values - of type character varying will simply store the - shorter string. + somewhat bizarre exception is required by the + SQL standard.) If the string to be stored is + shorter than the declared length, values of type + character will be space-padded; values of type + character varying will simply store the shorter + string. @@ -831,7 +838,8 @@ CREATE TABLE tablename ( character(n) or character varying(n), then an overlength value will be truncated to n characters without raising an - error. (This too is required by the SQL standard.) + error. (This too is required by the SQL + standard.) @@ -859,8 +867,9 @@ CREATE TABLE tablename ( more general text type, which stores strings of any length. Unlike character varying, text does not require an explicit declared upper limit on the size of - the string. Although the type text is not in the SQL - standard, many other RDBMS packages have it as well. + the string. Although the type text is not in the + SQL standard, many other RDBMS packages have it + as well. @@ -1125,12 +1134,12 @@ SELECT b, char_length(b) FROM test2; To use the bytea escaped octet notation, string - literals (input strings) must contain two backslashes due because - they must pass through two parsers in the PostgreSQL server. The - first backslash is interpreted as an escape character by the - string-literal parser, and therefore is consumed, leaving the - characters that follow. The remaining backslash is recognized by - the bytea input function as the prefix of a three + literals (input strings) must contain two backslashes because they + must pass through two parsers in the PostgreSQL + server. The first backslash is interpreted as an escape character + by the string-literal parser, and therefore is consumed, leaving + the characters that follow. The remaining backslash is recognized + by the bytea input function as the prefix of a three digit octal value. For example, a string literal passed to the backend as '\\001' becomes '\001' after passing through the string-literal @@ -1170,21 +1179,22 @@ SELECT b, char_length(b) FROM test2; - Depending on the front end to PostgreSQL you use, you may have - additional work to do in terms of escaping and unescaping - bytea strings. For example, you may also have to escape - line feeds and carriage returns if your interface automatically - translates these. Or you may have to double up on backslashes if - the parser for your language or choice also treats them as an - escape character. + Depending on the front end to PostgreSQL you use, + you may have additional work to do in terms of escaping and + unescaping bytea strings. For example, you may also + have to escape line feeds and carriage returns if your interface + automatically translates these. Or you may have to double up on + backslashes if the parser for your language or choice also treats + them as an escape character. - - The SQL standard defines a different binary string type, called - BLOB or BINARY LARGE OBJECT. The input - format is different compared to bytea, but the - provided functions and operators are mostly the same. - + + The SQL standard defines a different binary + string type, called BLOB or BINARY LARGE + OBJECT. The input format is different compared to + bytea, but the provided functions and operators are + mostly the same. + @@ -1559,20 +1569,32 @@ SELECT b, char_length(b) FROM test2; data type + + timestamp with time zone + data type + + timestamp without time zone data type - Time stamp types exist as timestamp [ - (p) ], timestamp [ + The time stamp types are timestamp [ (p) ] without time zone and - timestamp [ (p) ] without time - zone. A plain timestamp is equivalent to - timestamp without timezone. + timestamp [ (p) ] with time + zone. Writing just timestamp is equivalent to + timestamp without time zone. + + + Prior to PostgreSQL 7.3, writing just + timestamp was equivalent to timestamp with time + zone. This was changed for SQL spec compliance. + + + Valid input for the time stamp types consists of a concatenation of a date and a time, followed by an optional @@ -1605,11 +1627,38 @@ January 8 04:05:06 1999 PST For timestamp without time zone, any explicit time - zone specified in the input is silently swallowed. That is, the + zone specified in the input is silently ignored. That is, the resulting date/time value is derived from the explicit date/time fields in the input value, and is not adjusted for time zone. + + For timestamp with time zone, the internally stored + value is always in UTC (GMT). An input value that has an explicit + time zone specified is converted to UTC using the appropriate offset + for that time zone. If no time zone is stated in the input string, + then it is assumed to be in the time zone indicated by the system's + TimeZone parameter, and is converted to UTC using the + offset for the TimeZone zone. + + + + When a timestamp with time + zone value is output, it is always converted from UTC to the + current TimeZone zone, and displayed as local time in that + zone. To see the time in another time zone, either change + TimeZone or use the AT TIME ZONE construct + (see ). + + + + Conversions between timestamp without time zone and + timestamp with time zone normally assume that the + timestamp without time zone value should be taken or given + as TimeZone local time. A different zone reference can + be specified for the conversion using AT TIME ZONE. + + Time Zone Input @@ -1697,24 +1746,28 @@ January 8 04:05:06 1999 PST The following SQL-compatible functions can be used as date or time - input for the corresponding data type: CURRENT_DATE, + values for the corresponding data type: CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP. The latter two accept an - optional precision specification. (See also .) + optional precision specification. (See also .) PostgreSQL also supports several - special constants for convenience, shown in . + special date/time input values for convenience, as shown in . The values + infinity and -infinity + are specially represented inside the system and will be displayed + the same way; but the others are simply notational shorthands + that will be converted to ordinary date/time values when read.
- Special Date/Time Constants + Special Date/Time Inputs - Constant + Input string Description @@ -1725,15 +1778,13 @@ January 8 04:05:06 1999 PST infinity - later than other valid times + later than all other timestamps (not available for + type date) -infinity - earlier than other valid times - - - invalid - illegal entry + earlier than all other timestamps (not available for + type date) now @@ -1781,11 +1832,12 @@ January 8 04:05:06 1999 PST Output formats can be set to one of the four styles ISO 8601, SQL (Ingres), traditional PostgreSQL, and German, using the SET DateStyle. The default - is the ISO format. (The SQL standard requires - the use of the ISO 8601 format. The name of the - SQL output format is a historical accident.) - shows examples of - each output style. The output of the date and + is the ISO format. (The + SQL standard requires the use of the ISO 8601 + format. The name of the SQL output format is a + historical accident.) shows examples of each + output style. The output of the date and time types is of course only the date or time part in accordance with the given examples. @@ -1920,44 +1972,52 @@ January 8 04:05:06 1999 PST - To address these difficulties, we recommend using date/time - types that contain both date and time when using time zones. We - recommend not using the type time - with time zone (though it is supported by + To address these difficulties, we recommend using date/time types + that contain both date and time when using time zones. We + recommend not using the type time with + time zone (though it is supported by PostgreSQL for legacy applications and - for compatibility with other SQL implementations). - PostgreSQL - assumes your local time zone for any type containing only - date or time. Further, time zone support is derived from - the underlying operating system - time-zone capabilities, and hence can handle daylight-saving time - and other expected behavior. + for compatibility with other SQL + implementations). PostgreSQL assumes + your local time zone for any type containing only date or + time. Further, time zone support is derived from the underlying + operating system time-zone capabilities, and hence can handle + daylight-saving time and other expected behavior. - PostgreSQL obtains time-zone support + PostgreSQL obtains time-zone support from the underlying operating system for dates between 1902 and 2038 (near the typical date limits for Unix-style systems). Outside of this range, all dates are assumed to be - specified and used in Universal Coordinated Time (UTC). + specified and used in Universal Coordinated Time + (UTC). - All dates and times are stored internally in UTC, - traditionally known as Greenwich Mean Time (GMT). - Times are converted to local time on the database server before being - sent to the client frontend, hence by default are in the server - time zone. + All dates and times are stored internally in + UTC, traditionally known as Greenwich Mean + Time (GMT). Times are converted to local time + on the database server before being sent to the client frontend, + hence by default are in the server time zone. - There are several ways to affect the time-zone behavior: + There are several ways to select the time zone used by the server: The TZ environment variable on the server host - is used by the server as the default time zone. + is used by the server as the default time zone, if no other is + specified. + + + + + + The timezone configuration parameter can be + set in postgresql.conf. @@ -1976,25 +2036,13 @@ January 8 04:05:06 1999 PST sets the time zone for the session. - - - - The construct - -timestamp AT TIME ZONE 'zone' - - where zone can be specified as a - text time zone (e.g., 'PST') or as an - interval (e.g., INTERVAL '-08:00'). - - - If an invalid time zone is specified, - the time zone becomes GMT (on most systems anyway). + If an invalid time zone is specified, the time zone becomes + UTC (on most systems anyway). @@ -2124,8 +2172,9 @@ SELECT * FROM test1 WHERE a; Geometric data types represent two-dimensional spatial objects. shows the geometric - types available in PostgreSQL. The most fundamental type, the - point, forms the basis for all of the other types. + types available in PostgreSQL. The + most fundamental type, the point, forms the basis for all of the + other types.
@@ -2746,9 +2795,10 @@ SELECT * FROM test1 WHERE a; - Prior to PostgreSQL 7.2, BIT data was - always silently truncated or zero-padded on the right, with or without an - explicit cast. This was changed to comply with the SQL standard. + Prior to PostgreSQL 7.2, BIT data + was always silently truncated or zero-padded on the right, with + or without an explicit cast. This was changed to comply with the + SQL standard. @@ -2978,14 +3028,14 @@ SELECT * FROM test; - A third identifier type used by the system is cid, or command - identifier. This is the data type of the system columns - cmin and cmax. - Command identifiers are also 32-bit quantities. This creates a hard - limit of 232 (4 billion) SQL commands within a single - transaction. - In practice this limit is not a problem --- note that the limit is on - number of SQL commands, not number of tuples processed. + A third identifier type used by the system is cid, or + command identifier. This is the data type of the system columns + cmin and cmax. Command + identifiers are also 32-bit quantities. This creates a hard limit + of 232 (4 billion) SQL commands + within a single transaction. In practice this limit is not a + problem --- note that the limit is on number of + SQL commands, not number of tuples processed. @@ -3044,9 +3094,10 @@ SELECT * FROM test; column data type, but it can be used to declare a function's argument or result type. Each of the available pseudo-types is useful in situations where a function's behavior does not - correspond to simply taking or returning a value of a specific SQL - data type. lists the - existing pseudo-types. + correspond to simply taking or returning a value of a specific + SQL data type. lists the existing + pseudo-types.
@@ -3126,14 +3177,15 @@ SELECT * FROM test; - The internal pseudo-type is used to declare functions that are - meant only to be called internally by the database system, and not by - direct invocation in a SQL query. If a function has at least one - internal-type argument then it cannot be called from SQL. - To preserve the type safety of this restriction it is important to - follow this coding rule: do not create any function that is declared - to return internal unless it has at least one internal - argument. + The internal pseudo-type is used to declare functions + that are meant only to be called internally by the database + system, and not by direct invocation in a SQL + query. If a function has at least one internal-type + argument then it cannot be called from SQL. To + preserve the type safety of this restriction it is important to + follow this coding rule: do not create any function that is + declared to return internal unless it has at least one + internal argument. diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 8030497e773..6b1f46984f5 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -1,5 +1,5 @@ @@ -3549,9 +3549,14 @@ SUBSTRING('foobar' FROM 'o(.)b') o*, etc.). For formatting functions, refer to . You should be familiar with the background information on date/time data types (see ). The date/time operators described - below behave similarly for types involving time zones as well as - those without. + linkend="datatype-datetime">). + + + + All the functions and operators described below that take time or timestamp + inputs actually come in two variants: one that takes time or timestamp + with time zone, and one that takes time or timestamp without time zone. + For brevity, these variants are not shown separately.
@@ -3771,7 +3776,7 @@ SUBSTRING('foobar' FROM 'o(.)b') o now() - timestamp + timestamp with time zone Current date and time (equivalent to current_timestamp); see @@ -3898,8 +3903,8 @@ SELECT EXTRACT(DOY FROM TIMESTAMP '2001-02-16 20:38:40'); For date and timestamp values, the - number of seconds since 1970-01-01 00:00:00-00 (Result may be - negative.); for interval values, the total number + number of seconds since 1970-01-01 00:00:00-00 (can be negative); + for interval values, the total number of seconds in the interval @@ -4122,12 +4127,12 @@ SELECT EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40'); The date_part function is modeled on the traditional Ingres equivalent to the - SQL-function extract: + SQL-standard function extract: date_part('field', source) - Note that here the field value needs to - be a string. The valid field values for + Note that here the field parameter needs to + be a string value, not a name. The valid field values for date_part are the same as for extract. @@ -4192,6 +4197,95 @@ SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40'); + + <function>AT TIME ZONE</function> + + + timezone + conversion + + + + The AT TIME ZONE construct allows conversions + of timestamps to different timezones. + + +
+ AT TIME ZONE Variants + + + + Expression + Returns + Description + + + + + + + + timestamp without time zone + AT TIME ZONE + zone + + timestamp with time zone + Convert local time in given timezone to UTC + + + + + timestamp with time zone + AT TIME ZONE + zone + + timestamp without time zone + Convert UTC to local time in given timezone + + + + + time with time zone + AT TIME ZONE + zone + + time with time zone + Convert local time across timezones + + + + +
+ + + In these expressions, the desired time zone can be + specified either as a text string (e.g., 'PST') + or as an interval (e.g., INTERVAL '-08:00'). + + + + Examples (supposing that TimeZone is PST8PDT): + +SELECT TIMESTAMP '2001-02-16 20:38:40' AT TIME ZONE 'MST'; +Result: 2001-02-16 19:38:40-08 + +SELECT TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-05' AT TIME ZONE 'MST'; +Result: 2001-02-16 18:38:40 + + The first example takes a zone-less timestamp and interprets it as MST time + (GMT-7) to produce a UTC timestamp, which is then rotated to PST (GMT-8) + for display. The second example takes a timestamp specified in EST + (GMT-5) and converts it to local time in MST (GMT-7). + + + + The function timezone(zone, + timestamp) is equivalent to the SQL-compliant construct + timestamp AT TIME ZONE + zone. + + + Current Date/Time @@ -4219,6 +4313,16 @@ LOCALTIMESTAMP LOCALTIME ( precision ) LOCALTIMESTAMP ( precision ) +
+ + + CURRENT_TIME and + CURRENT_TIMESTAMP deliver values with time zone; + LOCALTIME and + LOCALTIMESTAMP deliver values without time zone. + + + CURRENT_TIME, CURRENT_TIMESTAMP, LOCALTIME, and diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 6c35f3ae009..a35353f450d 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.73 2002/09/21 19:52:41 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.73.2.1 2002/11/21 23:31:37 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -2013,7 +2013,6 @@ timetz_zone(PG_FUNCTION_ARGS) text *zone = PG_GETARG_TEXT_P(0); TimeTzADT *time = PG_GETARG_TIMETZADT_P(1); TimeTzADT *result; - TimeADT time1; int tz; int type, val; @@ -2040,15 +2039,17 @@ timetz_zone(PG_FUNCTION_ARGS) { tz = val * 60; #ifdef HAVE_INT64_TIMESTAMP - time1 = (time->time - ((time->zone + tz) * INT64CONST(1000000))); - result->time -= ((result->time / time1) * time1); - if (result->time < INT64CONST(0)) + result->time = time->time + ((time->zone - tz) * INT64CONST(1000000)); + while (result->time < INT64CONST(0)) result->time += INT64CONST(86400000000); + while (result->time >= INT64CONST(86400000000)) + result->time -= INT64CONST(86400000000); #else - time1 = (time->time - time->zone + tz); - TMODULO(result->time, time1, 86400e0); - if (result->time < 0) + result->time = time->time + (time->zone - tz); + while (result->time < 0) result->time += 86400; + while (result->time >= 86400) + result->time -= 86400; #endif result->zone = tz; @@ -2087,13 +2088,13 @@ timetz_izone(PG_FUNCTION_ARGS) result = (TimeTzADT *) palloc(sizeof(TimeTzADT)); #ifdef HAVE_INT64_TIMESTAMP - result->time = (time->time + ((time->zone - tz) * INT64CONST(1000000))); + result->time = time->time + ((time->zone - tz) * INT64CONST(1000000)); while (result->time < INT64CONST(0)) result->time += INT64CONST(86400000000); while (result->time >= INT64CONST(86400000000)) result->time -= INT64CONST(86400000000); #else - result->time = (time->time + (time->zone - tz)); + result->time = time->time + (time->zone - tz); while (result->time < 0) result->time += 86400; while (result->time >= 86400)