CREATETABLE TIMESTAMP_TBL (d1 timestamp(2) without 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.
BEGIN;
INSERTINTO TIMESTAMP_TBL VALUES ('today'); INSERTINTO TIMESTAMP_TBL VALUES ('yesterday'); INSERTINTO TIMESTAMP_TBL VALUES ('tomorrow'); -- time zone should be ignored by this data type INSERTINTO TIMESTAMP_TBL VALUES ('tomorrow EST'); INSERTINTO TIMESTAMP_TBL VALUES ('tomorrow zulu');
SELECT count(*) AS One FROM TIMESTAMP_TBL WHERE d1 = timestamp without time zone 'today'; SELECT count(*) AS Three FROM TIMESTAMP_TBL WHERE d1 = timestamp without time zone 'tomorrow'; SELECT count(*) AS One FROM TIMESTAMP_TBL WHERE d1 = timestamp without time zone 'yesterday';
COMMIT;
DELETEFROM TIMESTAMP_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 TIMESTAMP_TBL VALUES ('now'); SELECT pg_sleep(0.1); INSERTINTO TIMESTAMP_TBL VALUES ('now'); SELECT pg_sleep(0.1); SELECT count(*) AS two FROM TIMESTAMP_TBL WHERE d1 = timestamp(2) without time zone 'now'; SELECT count(d1) AS three, count(DISTINCT d1) AS two FROM TIMESTAMP_TBL; COMMIT;
-- Check behavior at the boundaries of the timestamp range SELECT'4714-11-24 00:00:00 BC'::timestamp; SELECT'4714-11-23 23:59:59 BC'::timestamp; -- out of range SELECT'294276-12-31 23:59:59'::timestamp; SELECT'294277-01-01 00:00:00'::timestamp; -- out of range
-- Demonstrate functions and operators SELECT d1 FROM TIMESTAMP_TBL WHERE d1 > timestamp without time zone '1997-01-02';
SELECT d1 FROM TIMESTAMP_TBL WHERE d1 < timestamp without time zone '1997-01-02';
SELECT d1 FROM TIMESTAMP_TBL WHERE d1 = timestamp without time zone '1997-01-02';
SELECT d1 FROM TIMESTAMP_TBL WHERE d1 != timestamp without time zone '1997-01-02';
SELECT d1 FROM TIMESTAMP_TBL WHERE d1 <= timestamp without time zone '1997-01-02';
SELECT d1 FROM TIMESTAMP_TBL WHERE d1 >= timestamp without time zone '1997-01-02';
SELECT d1 - timestamp without time zone '1997-01-02'AS diff FROM TIMESTAMP_TBL WHERE d1 BETWEEN'1902-01-01'AND'2038-01-01';
SELECT date_trunc( 'week', timestamp '2004-02-29 15:44:17.71393' ) AS week_trunc; SELECT date_trunc( 'week', timestamp 'infinity' ) AS inf_trunc; SELECT date_trunc( 'timezone', timestamp '2004-02-29 15:44:17.71393' ) AS notsupp_trunc; SELECT date_trunc( 'timezone', timestamp 'infinity' ) AS notsupp_inf_trunc; SELECT date_trunc( 'ago', timestamp 'infinity' ) AS invalid_trunc;
-- verify date_bin behaves the same as date_trunc for relevant intervals
-- Test casting within a BETWEEN qualifier SELECT d1 - timestamp without time zone '1997-01-02'AS diff FROM TIMESTAMP_TBL WHERE d1 BETWEEN timestamp without time zone '1902-01-01' AND timestamp without time zone '2038-01-01';
-- DATE_PART (timestamp_part) SELECT d1 as"timestamp",
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 TIMESTAMP_TBL;
SELECT d1 as"timestamp",
date_part( 'quarter', d1) AS quarter, date_part( 'msec', d1) AS msec,
date_part( 'usec', d1) AS usec FROM TIMESTAMP_TBL;
SELECT d1 as"timestamp",
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 TIMESTAMP_TBL;
SELECT d1 as"timestamp",
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 TIMESTAMP_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 TIMESTAMP_TBL;
-- value near upper bound uses special case in code SELECT date_part('epoch', '294270-01-01 00:00:00'::timestamp); SELECT extract(epoch from'294270-01-01 00:00:00'::timestamp); -- another internal overflow test case SELECT extract(epoch from'5000-01-01 00:00:00'::timestamp);
-- Roman months, with upper and lower case. SELECT i,
to_char(i * interval'1mon', 'rm'),
to_char(i * interval'1mon', 'RM') FROM generate_series(-13, 13) i;
-- generate_series for timestamp select * from generate_series('2020-01-01 00:00'::timestamp, '2020-01-02 03:00'::timestamp, '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'::timestamp, 'infinity'::timestamp, '1 month'::interval) limit10; -- errors select * from generate_series('2020-01-01 00:00'::timestamp, '2020-01-02 03:00'::timestamp, '0 hour'::interval); select generate_series(timestamp '1995-08-06 12:12:12', timestamp '1996-08-06 12:12:12', interval'infinity'); select generate_series(timestamp '1995-08-06 12:12:12', timestamp '1996-08-06 12:12:12', interval'-infinity');
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.