CREATETABLE TIMESTAMPTZ_TBL (d1 timestamp(2) with time zone);
-- Test shorthand input values -- We can't just "select" the results since they aren't constants; test for -- equality instead. We can do that by running the test inside a transaction -- block, within which the value of 'now' shouldn't change, and so these -- related values shouldn't either.
SELECT count(*) AS One FROM TIMESTAMPTZ_TBL WHERE d1 = timestamp with time zone 'today'; SELECT count(*) AS One FROM TIMESTAMPTZ_TBL WHERE d1 = timestamp with time zone 'tomorrow'; SELECT count(*) AS One FROM TIMESTAMPTZ_TBL WHERE d1 = timestamp with time zone 'yesterday'; SELECT count(*) AS One FROM TIMESTAMPTZ_TBL WHERE d1 = timestamp with time zone 'tomorrow EST'; SELECT count(*) AS One FROM TIMESTAMPTZ_TBL WHERE d1 = timestamp with time zone 'tomorrow zulu';
COMMIT;
DELETEFROM TIMESTAMPTZ_TBL;
-- Verify that 'now' *does* change over a reasonable interval such as 100 msec, -- and that it doesn't change over the same interval within a transaction block
BEGIN; INSERTINTO TIMESTAMPTZ_TBL VALUES ('now'); SELECT pg_sleep(0.1); INSERTINTO TIMESTAMPTZ_TBL VALUES ('now'); SELECT pg_sleep(0.1); SELECT count(*) AS two FROM TIMESTAMPTZ_TBL WHERE d1 = timestamp(2) with time zone 'now'; SELECT count(d1) AS three, count(DISTINCT d1) AS two FROM TIMESTAMPTZ_TBL; COMMIT;
-- timestamps at different timezones INSERTINTO TIMESTAMPTZ_TBL VALUES ('19970210 173201 America/New_York'); SELECT'19970210 173201' AT TIME ZONE 'America/New_York'; INSERTINTO TIMESTAMPTZ_TBL VALUES ('19970710 173201 America/New_York'); SELECT'19970710 173201' AT TIME ZONE 'America/New_York'; INSERTINTO TIMESTAMPTZ_TBL VALUES ('19970710 173201 America/Does_not_exist'); SELECT'19970710 173201' AT TIME ZONE 'America/Does_not_exist';
-- Daylight saving time for timestamps beyond 32-bit time_t range. SELECT'20500710 173201 Europe/Helsinki'::timestamptz; -- DST SELECT'20500110 173201 Europe/Helsinki'::timestamptz; -- non-DST
-- Recognize "LMT" as whatever it means in the current zone SELECT'Jan 01 00:00:00 1000 LMT'::timestamptz; SELECT'Jan 01 00:00:00 2024 LMT'::timestamptz; SET timezone = 'Europe/London'; SELECT'Jan 01 00:00:00 1000 LMT'::timestamptz; SELECT'Jan 01 00:00:00 2024 LMT'::timestamptz; -- which might be nothing SET timezone = 'UTC'; SELECT'Jan 01 00:00:00 2024 LMT'::timestamptz; -- fail -- Another example of an abbrev that varies across zones SELECT'1912-01-01 00:00 MMT'::timestamptz; -- from timezone_abbreviations SET timezone = 'America/Montevideo'; SELECT'1912-01-01 00:00'::timestamptz; SELECT'1912-01-01 00:00 MMT'::timestamptz; SELECT'1912-01-01 00:00 MMT'::timestamptz AT TIME ZONE 'UTC';
RESET timezone;
-- Test non-error-throwing API SELECT pg_input_is_valid('now', 'timestamptz'); SELECT pg_input_is_valid('garbage', 'timestamptz'); SELECT pg_input_is_valid('2001-01-01 00:00 Nehwon/Lankhmar', 'timestamptz'); SELECT * FROM pg_input_error_info('garbage', 'timestamptz'); SELECT * FROM pg_input_error_info('2001-01-01 00:00 Nehwon/Lankhmar', 'timestamptz');
-- Check date conversion and date arithmetic INSERTINTO TIMESTAMPTZ_TBL VALUES ('1997-06-10 18:32:01 PDT');
-- Alternative field order that we've historically supported (sort of) -- with regular and POSIXy timezone specs SELECT'Wed Jul 11 10:51:14 America/New_York 2001'::timestamptz; SELECT'Wed Jul 11 10:51:14 GMT-4 2001'::timestamptz; SELECT'Wed Jul 11 10:51:14 GMT+4 2001'::timestamptz; SELECT'Wed Jul 11 10:51:14 PST-03:00 2001'::timestamptz; SELECT'Wed Jul 11 10:51:14 PST+03:00 2001'::timestamptz;
SELECT d1 FROM TIMESTAMPTZ_TBL;
-- Check behavior at the boundaries of the timestamp range SELECT'4714-11-24 00:00:00+00 BC'::timestamptz; SELECT'4714-11-23 16:00:00-08 BC'::timestamptz; SELECT'Sun Nov 23 16:00:00 4714 PST BC'::timestamptz; SELECT'4714-11-23 23:59:59+00 BC'::timestamptz; -- out of range SELECT'294276-12-31 23:59:59+00'::timestamptz; SELECT'294276-12-31 15:59:59-08'::timestamptz; SELECT'294277-01-01 00:00:00+00'::timestamptz; -- out of range SELECT'294277-12-31 16:00:00-08'::timestamptz; -- out of range
-- Demonstrate functions and operators SELECT d1 FROM TIMESTAMPTZ_TBL WHERE d1 > timestamp with time zone '1997-01-02';
SELECT d1 FROM TIMESTAMPTZ_TBL WHERE d1 < timestamp with time zone '1997-01-02';
SELECT d1 FROM TIMESTAMPTZ_TBL WHERE d1 = timestamp with time zone '1997-01-02';
SELECT d1 FROM TIMESTAMPTZ_TBL WHERE d1 != timestamp with time zone '1997-01-02';
SELECT d1 FROM TIMESTAMPTZ_TBL WHERE d1 <= timestamp with time zone '1997-01-02';
SELECT d1 FROM TIMESTAMPTZ_TBL WHERE d1 >= timestamp with time zone '1997-01-02';
SELECT d1 - timestamp with time zone '1997-01-02'AS diff FROM TIMESTAMPTZ_TBL WHERE d1 BETWEEN'1902-01-01'AND'2038-01-01';
SELECT date_trunc( 'week', timestamp with time zone '2004-02-29 15:44:17.71393' ) AS week_trunc; SELECT date_trunc( 'week', timestamp with time zone 'infinity' ) AS inf_trunc; SELECT date_trunc( 'timezone', timestamp with time zone '2004-02-29 15:44:17.71393' ) AS notsupp_trunc; SELECT date_trunc( 'timezone', timestamp with time zone 'infinity' ) AS notsupp_inf_trunc; SELECT date_trunc( 'ago', timestamp with time zone 'infinity' ) AS invalid_trunc;
SELECT date_trunc('day', timestamp with time zone '2001-02-16 20:38:40+00', 'Australia/Sydney') as sydney_trunc; -- zone name SELECT date_trunc('day', timestamp with time zone '2001-02-16 20:38:40+00', 'GMT') as gmt_trunc; -- fixed-offset abbreviation SELECT date_trunc('day', timestamp with time zone '2001-02-16 20:38:40+00', 'VET') as vet_trunc; -- variable-offset abbreviation SELECT date_trunc('timezone', timestamp with time zone 'infinity', 'GMT') AS notsupp_zone_trunc; SELECT date_trunc( 'week', timestamp with time zone 'infinity', 'GMT') AS inf_zone_trunc; SELECT date_trunc('ago', timestamp with time zone 'infinity', 'GMT') AS invalid_zone_trunc;
-- verify date_bin behaves the same as date_trunc for relevant intervals SELECT
str, interval,
date_trunc(str, ts, 'Australia/Sydney') = date_bin(interval::interval, ts, timestamp with time zone '2001-01-01+11') AS equal FROM ( VALUES
('day', '1 d'),
('hour', '1 h'),
('minute', '1 m'),
('second', '1 s'),
('millisecond', '1 ms'),
('microsecond', '1 us')
) intervals (str, interval),
(VALUES (timestamptz '2020-02-29 15:44:17.71393+00')) ts (ts);
-- shift bins using the origin parameter: SELECT date_bin('5 min'::interval, timestamptz '2020-02-01 01:01:01+00', timestamptz '2020-02-01 00:02:30+00');
-- test roundoff edge case when source < origin SELECT date_bin('30 minutes'::interval, timestamptz '2024-02-01 15:00:00', timestamptz '2024-02-01 17:00:00');
-- disallow intervals with months or years SELECT date_bin('5 months'::interval, timestamp with time zone '2020-02-01 01:01:01+00', timestamp with time zone '2001-01-01+00'); SELECT date_bin('5 years'::interval, timestamp with time zone '2020-02-01 01:01:01+00', timestamp with time zone '2001-01-01+00');
-- disallow zero intervals SELECT date_bin('0 days'::interval, timestamp with time zone '1970-01-01 01:00:00+00' , timestamp with time zone '1970-01-01 00:00:00+00');
-- disallow negative intervals SELECT date_bin('-2 days'::interval, timestamp with time zone '1970-01-01 01:00:00+00' , timestamp with time zone '1970-01-01 00:00:00+00');
-- Test casting within a BETWEEN qualifier SELECT d1 - timestamp with time zone '1997-01-02'AS diff FROM TIMESTAMPTZ_TBL WHERE d1 BETWEEN timestamp with time zone '1902-01-01'AND timestamp with time zone '2038-01-01';
-- DATE_PART (timestamptz_part) SELECT d1 as timestamptz,
date_part( 'year', d1) AS year, date_part( 'month', d1) AS month,
date_part( 'day', d1) AS day, date_part( 'hour', d1) AS hour,
date_part( 'minute', d1) AS minute, date_part( 'second', d1) AS second FROM TIMESTAMPTZ_TBL;
SELECT d1 as timestamptz,
date_part( 'quarter', d1) AS quarter, date_part( 'msec', d1) AS msec,
date_part( 'usec', d1) AS usec FROM TIMESTAMPTZ_TBL;
SELECT d1 as timestamptz,
date_part( 'isoyear', d1) AS isoyear, date_part( 'week', d1) AS week,
date_part( 'isodow', d1) AS isodow, date_part( 'dow', d1) AS dow,
date_part( 'doy', d1) AS doy FROM TIMESTAMPTZ_TBL;
SELECT d1 as timestamptz,
date_part( 'decade', d1) AS decade,
date_part( 'century', d1) AS century,
date_part( 'millennium', d1) AS millennium,
round(date_part( 'julian', d1)) AS julian,
date_part( 'epoch', d1) AS epoch FROM TIMESTAMPTZ_TBL;
SELECT d1 as timestamptz,
date_part( 'timezone', d1) AS timezone,
date_part( 'timezone_hour', d1) AS timezone_hour,
date_part( 'timezone_minute', d1) AS timezone_minute FROM TIMESTAMPTZ_TBL;
-- extract implementation is mostly the same as date_part, so only -- test a few cases for additional coverage. SELECT d1 as"timestamp",
extract(microseconds from d1) AS microseconds,
extract(milliseconds from d1) AS milliseconds,
extract(seconds from d1) AS seconds,
round(extract(julian from d1)) AS julian,
extract(epoch from d1) AS epoch FROM TIMESTAMPTZ_TBL;
-- value near upper bound uses special case in code SELECT date_part('epoch', '294270-01-01 00:00:00+00'::timestamptz); SELECT extract(epoch from'294270-01-01 00:00:00+00'::timestamptz); -- another internal overflow test case SELECT extract(epoch from'5000-01-01 00:00:00+00'::timestamptz);
-- generate_series for timestamptz select * from generate_series('2020-01-01 00:00'::timestamptz, '2020-01-02 03:00'::timestamptz, '1 hour'::interval); -- the LIMIT should allow this to terminate in a reasonable amount of time -- (but that unfortunately doesn't work yet for SELECT * FROM ...) select generate_series('2022-01-01 00:00'::timestamptz, 'infinity'::timestamptz, '1 month'::interval) limit10; -- errors select * from generate_series('2020-01-01 00:00'::timestamptz, '2020-01-02 03:00'::timestamptz, '0 hour'::interval); select generate_series(timestamptz '1995-08-06 12:12:12', timestamptz '1996-08-06 12:12:12', interval'infinity'); select generate_series(timestamptz '1995-08-06 12:12:12', timestamptz '1996-08-06 12:12:12', interval'-infinity');
-- Interval crossing time shift for Europe/Warsaw timezone (with DST) SET TimeZone to'UTC';
-- -- Test behavior with a dynamic (time-varying) timezone abbreviation. -- These tests rely on the knowledge that MSK (Europe/Moscow standard time) -- moved forwards in Mar 2011 and backwards again in Oct 2014. --
SELECT'2011-03-27 00:00:00'::timestamp AT TIME ZONE 'Europe/Moscow'; SELECT'2011-03-27 01:00:00'::timestamp AT TIME ZONE 'Europe/Moscow'; SELECT'2011-03-27 01:59:59'::timestamp AT TIME ZONE 'Europe/Moscow'; SELECT'2011-03-27 02:00:00'::timestamp AT TIME ZONE 'Europe/Moscow'; SELECT'2011-03-27 02:00:01'::timestamp AT TIME ZONE 'Europe/Moscow'; SELECT'2011-03-27 02:59:59'::timestamp AT TIME ZONE 'Europe/Moscow'; SELECT'2011-03-27 03:00:00'::timestamp AT TIME ZONE 'Europe/Moscow'; SELECT'2011-03-27 03:00:01'::timestamp AT TIME ZONE 'Europe/Moscow'; SELECT'2011-03-27 04:00:00'::timestamp AT TIME ZONE 'Europe/Moscow';
SELECT'2011-03-27 00:00:00'::timestamp AT TIME ZONE 'MSK'; SELECT'2011-03-27 01:00:00'::timestamp AT TIME ZONE 'MSK'; SELECT'2011-03-27 01:59:59'::timestamp AT TIME ZONE 'MSK'; SELECT'2011-03-27 02:00:00'::timestamp AT TIME ZONE 'MSK'; SELECT'2011-03-27 02:00:01'::timestamp AT TIME ZONE 'MSK'; SELECT'2011-03-27 02:59:59'::timestamp AT TIME ZONE 'MSK'; SELECT'2011-03-27 03:00:00'::timestamp AT TIME ZONE 'MSK'; SELECT'2011-03-27 03:00:01'::timestamp AT TIME ZONE 'MSK'; SELECT'2011-03-27 04:00:00'::timestamp AT TIME ZONE 'MSK';
SELECT'2014-10-26 00:00:00'::timestamp AT TIME ZONE 'Europe/Moscow'; SELECT'2014-10-26 00:59:59'::timestamp AT TIME ZONE 'Europe/Moscow'; SELECT'2014-10-26 01:00:00'::timestamp AT TIME ZONE 'Europe/Moscow'; SELECT'2014-10-26 01:00:01'::timestamp AT TIME ZONE 'Europe/Moscow'; SELECT'2014-10-26 02:00:00'::timestamp AT TIME ZONE 'Europe/Moscow';
SELECT'2014-10-26 00:00:00'::timestamp AT TIME ZONE 'MSK'; SELECT'2014-10-26 00:59:59'::timestamp AT TIME ZONE 'MSK'; SELECT'2014-10-26 01:00:00'::timestamp AT TIME ZONE 'MSK'; SELECT'2014-10-26 01:00:01'::timestamp AT TIME ZONE 'MSK'; SELECT'2014-10-26 02:00:00'::timestamp AT TIME ZONE 'MSK';
SELECT'2011-03-26 21:00:00 UTC'::timestamptz AT TIME ZONE 'Europe/Moscow'; SELECT'2011-03-26 22:00:00 UTC'::timestamptz AT TIME ZONE 'Europe/Moscow'; SELECT'2011-03-26 22:59:59 UTC'::timestamptz AT TIME ZONE 'Europe/Moscow'; SELECT'2011-03-26 23:00:00 UTC'::timestamptz AT TIME ZONE 'Europe/Moscow'; SELECT'2011-03-26 23:00:01 UTC'::timestamptz AT TIME ZONE 'Europe/Moscow'; SELECT'2011-03-26 23:59:59 UTC'::timestamptz AT TIME ZONE 'Europe/Moscow'; SELECT'2011-03-27 00:00:00 UTC'::timestamptz AT TIME ZONE 'Europe/Moscow';
SELECT'2014-10-25 21:00:00 UTC'::timestamptz AT TIME ZONE 'Europe/Moscow'; SELECT'2014-10-25 21:59:59 UTC'::timestamptz AT TIME ZONE 'Europe/Moscow'; SELECT'2014-10-25 22:00:00 UTC'::timestamptz AT TIME ZONE 'Europe/Moscow'; SELECT'2014-10-25 22:00:01 UTC'::timestamptz AT TIME ZONE 'Europe/Moscow'; SELECT'2014-10-25 23:00:00 UTC'::timestamptz AT TIME ZONE 'Europe/Moscow';
SELECT'2011-03-26 21:00:00 UTC'::timestamptz AT TIME ZONE 'MSK'; SELECT'2011-03-26 22:00:00 UTC'::timestamptz AT TIME ZONE 'MSK'; SELECT'2011-03-26 22:59:59 UTC'::timestamptz AT TIME ZONE 'MSK'; SELECT'2011-03-26 23:00:00 UTC'::timestamptz AT TIME ZONE 'MSK'; SELECT'2011-03-26 23:00:01 UTC'::timestamptz AT TIME ZONE 'MSK'; SELECT'2011-03-26 23:59:59 UTC'::timestamptz AT TIME ZONE 'MSK'; SELECT'2011-03-27 00:00:00 UTC'::timestamptz AT TIME ZONE 'MSK';
SELECT'2014-10-25 21:00:00 UTC'::timestamptz AT TIME ZONE 'MSK'; SELECT'2014-10-25 21:59:59 UTC'::timestamptz AT TIME ZONE 'MSK'; SELECT'2014-10-25 22:00:00 UTC'::timestamptz AT TIME ZONE 'MSK'; SELECT'2014-10-25 22:00:01 UTC'::timestamptz AT TIME ZONE 'MSK'; SELECT'2014-10-25 23:00:00 UTC'::timestamptz AT TIME ZONE 'MSK';
-- -- Test LOCAL time zone --
BEGIN; SET LOCAL TIME ZONE 'Europe/Paris'; VALUES (CAST('1978-07-07 19:38 America/New_York'AS TIMESTAMP WITH TIME ZONE) AT LOCAL); VALUES (TIMESTAMP '1978-07-07 19:38' AT LOCAL); SET LOCAL TIME ZONE 'Australia/Sydney'; VALUES (CAST('1978-07-07 19:38 America/New_York'AS TIMESTAMP WITH TIME ZONE) AT LOCAL); VALUES (TIMESTAMP '1978-07-07 19:38' AT LOCAL); SET LOCAL TimeZone TO'UTC'; CREATE VIEW timestamp_local_view AS SELECT CAST('1978-07-07 19:38 America/New_York'AS TIMESTAMP WITH TIME ZONE) AT LOCAL AS ttz_at_local,
timezone(CAST('1978-07-07 19:38 America/New_York'AS TIMESTAMP WITH TIME ZONE)) AS ttz_func,
TIMESTAMP '1978-07-07 19:38' AT LOCAL AS t_at_local,
timezone(TIMESTAMP '1978-07-07 19:38') AS t_func; SELECT pg_get_viewdef('timestamp_local_view', true);
\x TABLE timestamp_local_view;
\x DROP VIEW timestamp_local_view; COMMIT;
-- -- Test that AT TIME ZONE isn't misoptimized when using an index (bug #14504) -- create temp table tmptz (f1 timestamptz primarykey); insertinto tmptz values ('2017-01-18 00:00+00'); explain (costs off) select * from tmptz where f1 at time zone 'utc' = '2017-01-18 00:00'; select * from tmptz where f1 at time zone 'utc' = '2017-01-18 00:00';
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.