4 -- needed so tests pass even in Australia
5 SET australian_timezones = 'off';
6 SET DateStyle = 'Postgres,US';
9 -- date, time arithmetic
12 SELECT date '1981-02-03' + time '04:05:06' AS "Date + Time";
13 SELECT date '1991-02-03' + time with time zone '04:05:06 PST' AS "Date + Time PST";
14 SELECT date '2001-02-03' + time with time zone '04:05:06 UTC' AS "Date + Time UTC";
15 SELECT date '1991-02-03' + interval '2 years' AS "Add Two Years";
16 SELECT date '2001-12-13' - interval '2 years' AS "Subtract Two Years";
17 -- subtract time from date should not make sense; use interval instead
18 SELECT date '1991-02-03' - time '04:05:06' AS "Subtract Time";
19 SELECT date '1991-02-03' - time with time zone '04:05:06 UTC' AS "Subtract Time UTC";
22 -- timestamp, interval arithmetic
25 SELECT timestamp without time zone '1996-03-01' - interval '1 second' AS "Feb 29";
26 SELECT timestamp without time zone '1999-03-01' - interval '1 second' AS "Feb 28";
27 SELECT timestamp without time zone '2000-03-01' - interval '1 second' AS "Feb 29";
28 SELECT timestamp without time zone '1999-12-01' + interval '1 month - 1 second' AS "Dec 31";
31 -- Not directly usable for regression testing since these are not constants.
32 -- So, just try to test parser and hope for the best - thomas 97/04/26
33 SELECT (timestamp without time zone 'today' = (timestamp without time zone 'yesterday' + interval '1 day')) as "True";
34 SELECT (timestamp without time zone 'today' = (timestamp without time zone 'tomorrow' - interval '1 day')) as "True";
35 SELECT (timestamp without time zone 'tomorrow' = (timestamp without time zone 'yesterday' + interval '2 days')) as "True";
36 SELECT (timestamp without time zone 'tomorrow' > 'now') as "True";
38 -- Convert from date and time to timestamp
39 -- This test used to be timestamp(date,time) but no longer allowed by grammar
40 -- to enable support for SQL99 timestamp type syntax.
41 SELECT date '1994-01-01' + time '11:00' AS "Jan_01_1994_11am";
42 SELECT date '1994-01-01' + time '10:00' AS "Jan_01_1994_10am";
43 SELECT date '1994-01-01' + time '11:00-5' AS "Jan_01_1994_8am";
44 SELECT "timestamp"(date '1994-01-01', time with time zone '11:00-5') AS "Jan_01_1994_11am";
46 SELECT '' AS "64", d1 + interval '1 year' AS one_year FROM TIMESTAMP_TBL;
47 SELECT '' AS "64", d1 - interval '1 year' AS one_year FROM TIMESTAMP_TBL;
49 SELECT timestamp with time zone '1996-03-01' - interval '1 second' AS "Feb 29";
50 SELECT timestamp with time zone '1999-03-01' - interval '1 second' AS "Feb 28";
51 SELECT timestamp with time zone '2000-03-01' - interval '1 second' AS "Feb 29";
52 SELECT timestamp with time zone '1999-12-01' + interval '1 month - 1 second' AS "Dec 31";
54 SELECT (timestamp with time zone 'today' = (timestamp with time zone 'yesterday' + interval '1 day')) as "True";
55 SELECT (timestamp with time zone 'today' = (timestamp with time zone 'tomorrow' - interval '1 day')) as "True";
56 SELECT (timestamp with time zone 'tomorrow' = (timestamp with time zone 'yesterday' + interval '2 days')) as "True";
57 SELECT (timestamp with time zone 'tomorrow' > 'now') as "True";
59 SELECT timestamptz(date '1994-01-01', time '11:00') AS "Jan_01_1994_10am";
60 SELECT timestamptz(date '1994-01-01', time '10:00') AS "Jan_01_1994_9am";
61 SELECT timestamptz(date '1994-01-01', time with time zone '11:00-8') AS "Jan_01_1994_11am";
62 SELECT timestamptz(date '1994-01-01', time with time zone '10:00-8') AS "Jan_01_1994_10am";
63 SELECT timestamptz(date '1994-01-01', time with time zone '11:00-5') AS "Jan_01_1994_8am";
65 SELECT '' AS "64", d1 + interval '1 year' AS one_year FROM TIMESTAMPTZ_TBL;
66 SELECT '' AS "64", d1 - interval '1 year' AS one_year FROM TIMESTAMPTZ_TBL;
69 -- time, interval arithmetic
72 SELECT CAST(time '01:02' AS interval) AS "+01:02";
73 SELECT CAST(interval '02:03' AS time) AS "02:03:00";
74 SELECT time '01:30' + interval '02:01' AS "03:31:00";
75 SELECT time '01:30' - interval '02:01' AS "23:29:00";
76 SELECT time '02:30' + interval '36:01' AS "14:31:00";
77 SELECT time '03:30' + interval '1 month 04:01' AS "07:31:00";
78 SELECT interval '04:30' - time '01:02' AS "20:32:00";
79 SELECT CAST(time with time zone '01:02-08' AS interval) AS "+00:01";
80 SELECT CAST(interval '02:03' AS time with time zone) AS "02:03:00-08";
81 SELECT time with time zone '01:30-08' - interval '02:01' AS "23:29:00-08";
82 SELECT time with time zone '02:30-08' + interval '36:01' AS "14:31:00-08";
84 -- These two tests cannot be used because they default to current timezone,
85 -- which may be either -08 or -07 depending on the time of year.
86 -- SELECT time with time zone '01:30' + interval '02:01' AS "03:31:00-08";
87 -- SELECT time with time zone '03:30' + interval '1 month 04:01' AS "07:31:00-08";
88 -- Try the following two tests instead, as a poor substitute
90 SELECT CAST(CAST(date 'today' + time with time zone '01:30'
91 + interval '02:01' AS time with time zone) AS time) AS "03:31:00";
93 SELECT CAST(cast(date 'today' + time with time zone '03:30-08'
94 + interval '1 month 04:01' as timestamp without time zone) AS time) AS "07:31:00";
96 SELECT interval '04:30' - time with time zone '01:02-05' AS "20:32:00-05";
98 SELECT t.d1 + i.f1 AS "102" FROM TIMESTAMP_TBL t, INTERVAL_TBL i
99 WHERE t.d1 BETWEEN '1990-01-01' AND '2001-01-01'
100 AND i.f1 BETWEEN '00:00' AND '23:00';
102 SELECT t.d1 - i.f1 AS "102" FROM TIMESTAMP_TBL t, INTERVAL_TBL i
103 WHERE t.d1 BETWEEN '1990-01-01' AND '2001-01-01'
104 AND i.f1 BETWEEN '00:00' AND '23:00';
106 SELECT t.f1 + i.f1 AS "80" FROM TIME_TBL t, INTERVAL_TBL i;
107 SELECT t.f1 - i.f1 AS "80" FROM TIME_TBL t, INTERVAL_TBL i;
109 SELECT t.f1 + i.f1 AS "100" FROM TIMETZ_TBL t, INTERVAL_TBL i;
110 SELECT t.f1 - i.f1 AS "100" FROM TIMETZ_TBL t, INTERVAL_TBL i;
112 -- SQL9x OVERLAPS operator
113 -- test with time zone
114 SELECT (timestamp with time zone '2000-11-27', timestamp with time zone '2000-11-28')
115 OVERLAPS (timestamp with time zone '2000-11-27 12:00', timestamp with time zone '2000-11-30') AS "True";
117 SELECT (timestamp with time zone '2000-11-26', timestamp with time zone '2000-11-27')
118 OVERLAPS (timestamp with time zone '2000-11-27 12:00', timestamp with time zone '2000-11-30') AS "False";
120 SELECT (timestamp with time zone '2000-11-27', timestamp with time zone '2000-11-28')
121 OVERLAPS (timestamp with time zone '2000-11-27 12:00', interval '1 day') AS "True";
123 SELECT (timestamp with time zone '2000-11-27', interval '12 hours')
124 OVERLAPS (timestamp with time zone '2000-11-27 12:00', timestamp with time zone '2000-11-30') AS "False";
126 SELECT (timestamp with time zone '2000-11-27', interval '12 hours')
127 OVERLAPS (timestamp with time zone '2000-11-27', interval '12 hours') AS "True";
129 SELECT (timestamp with time zone '2000-11-27', interval '12 hours')
130 OVERLAPS (timestamp with time zone '2000-11-27 12:00', interval '12 hours') AS "False";
132 -- test without time zone
133 SELECT (timestamp without time zone '2000-11-27', timestamp without time zone '2000-11-28')
134 OVERLAPS (timestamp without time zone '2000-11-27 12:00', timestamp without time zone '2000-11-30') AS "True";
136 SELECT (timestamp without time zone '2000-11-26', timestamp without time zone '2000-11-27')
137 OVERLAPS (timestamp without time zone '2000-11-27 12:00', timestamp without time zone '2000-11-30') AS "False";
139 SELECT (timestamp without time zone '2000-11-27', timestamp without time zone '2000-11-28')
140 OVERLAPS (timestamp without time zone '2000-11-27 12:00', interval '1 day') AS "True";
142 SELECT (timestamp without time zone '2000-11-27', interval '12 hours')
143 OVERLAPS (timestamp without time zone '2000-11-27 12:00', timestamp without time zone '2000-11-30') AS "False";
145 SELECT (timestamp without time zone '2000-11-27', interval '12 hours')
146 OVERLAPS (timestamp without time zone '2000-11-27', interval '12 hours') AS "True";
148 SELECT (timestamp without time zone '2000-11-27', interval '12 hours')
149 OVERLAPS (timestamp without time zone '2000-11-27 12:00', interval '12 hours') AS "False";
151 -- test time and interval
152 SELECT (time '00:00', time '01:00')
153 OVERLAPS (time '00:30', time '01:30') AS "True";
155 SELECT (time '00:00', interval '1 hour')
156 OVERLAPS (time '00:30', interval '1 hour') AS "True";
158 SELECT (time '00:00', interval '1 hour')
159 OVERLAPS (time '01:30', interval '1 hour') AS "False";
161 -- SQL99 seems to want this to be false (and we conform to the spec).
162 -- istm that this *should* return true, on the theory that time
163 -- intervals can wrap around the day boundary - thomas 2001-09-25
164 SELECT (time '00:00', interval '1 hour')
165 OVERLAPS (time '01:30', interval '1 day') AS "False";
167 CREATE TABLE TEMP_TIMESTAMP (f1 timestamp);
169 -- get some candidate input values
171 INSERT INTO TEMP_TIMESTAMP (f1)
172 SELECT d1 FROM TIMESTAMP_TBL
173 WHERE d1 BETWEEN '13-jun-1957' AND '1-jan-1997'
174 OR d1 BETWEEN '1-jan-1999' AND '1-jan-2010';
176 SELECT '' AS "16", f1 AS "timestamp"
178 ORDER BY "timestamp";
180 SELECT '' AS "160", d.f1 AS "timestamp", t.f1 AS "interval", d.f1 + t.f1 AS plus
181 FROM TEMP_TIMESTAMP d, INTERVAL_TBL t
182 ORDER BY plus, "timestamp", "interval";
184 SELECT '' AS "160", d.f1 AS "timestamp", t.f1 AS "interval", d.f1 - t.f1 AS minus
185 FROM TEMP_TIMESTAMP d, INTERVAL_TBL t
187 ORDER BY minus, "timestamp", "interval";
189 SELECT '' AS "16", d.f1 AS "timestamp", timestamp '1980-01-06 00:00 GMT' AS gpstime_zero,
190 d.f1 - timestamp '1980-01-06 00:00 GMT' AS difference
191 FROM TEMP_TIMESTAMP d
194 SELECT '' AS "226", d1.f1 AS timestamp1, d2.f1 AS timestamp2, d1.f1 - d2.f1 AS difference
195 FROM TEMP_TIMESTAMP d1, TEMP_TIMESTAMP d2
196 ORDER BY timestamp1, timestamp2, difference;
199 -- abstime, reltime arithmetic
202 SELECT '' AS ten, ABSTIME_TBL.f1 AS abstime, RELTIME_TBL.f1 AS reltime
203 WHERE (ABSTIME_TBL.f1 + RELTIME_TBL.f1) < abstime 'Jan 14 14:00:00 1971'
204 ORDER BY abstime, reltime;
206 -- these four queries should return the same answer
207 -- the "infinity" and "-infinity" tuples in ABSTIME_TBL cannot be added and
208 -- therefore, should not show up in the results.
210 SELECT '' AS three, ABSTIME_TBL.*
211 WHERE (ABSTIME_TBL.f1 + reltime '@ 3 year') -- +3 years
212 < abstime 'Jan 14 14:00:00 1977';
214 SELECT '' AS three, ABSTIME_TBL.*
215 WHERE (ABSTIME_TBL.f1 + reltime '@ 3 year ago') -- -3 years
216 < abstime 'Jan 14 14:00:00 1971';
218 SELECT '' AS three, ABSTIME_TBL.*
219 WHERE (ABSTIME_TBL.f1 - reltime '@ 3 year') -- -(+3) years
220 < abstime 'Jan 14 14:00:00 1971';
222 SELECT '' AS three, ABSTIME_TBL.*
223 WHERE (ABSTIME_TBL.f1 - reltime '@ 3 year ago') -- -(-3) years
224 < abstime 'Jan 14 14:00:00 1977';
230 SELECT '' AS "16", f1 AS "timestamp", date(f1) AS date
232 WHERE f1 <> timestamp 'current'
233 ORDER BY date, "timestamp";
235 SELECT '' AS "16", f1 AS "timestamp", abstime(f1) AS abstime
239 SELECT '' AS four, f1 AS abstime, date(f1) AS date
241 WHERE isfinite(f1) AND f1 <> abstime 'current'
242 ORDER BY date, abstime;
244 SELECT '' AS two, d1 AS "timestamp", abstime(d1) AS abstime
245 FROM TIMESTAMP_TBL WHERE NOT isfinite(d1);
247 SELECT '' AS three, f1 as abstime, cast(f1 as timestamp) AS "timestamp"
248 FROM ABSTIME_TBL WHERE NOT isfinite(f1);
250 SELECT '' AS ten, f1 AS interval, reltime(f1) AS reltime
253 SELECT '' AS six, f1 as reltime, CAST(f1 AS interval) AS interval
256 DROP TABLE TEMP_TIMESTAMP;
262 SET DateStyle TO 'US,Postgres';
266 SELECT '' AS "64", d1 AS us_postgres FROM TIMESTAMP_TBL;
268 SELECT '' AS seven, f1 AS us_postgres FROM ABSTIME_TBL;
270 SET DateStyle TO 'US,ISO';
272 SELECT '' AS "64", d1 AS us_iso FROM TIMESTAMP_TBL;
274 SELECT '' AS seven, f1 AS us_iso FROM ABSTIME_TBL;
276 SET DateStyle TO 'US,SQL';
280 SELECT '' AS "64", d1 AS us_sql FROM TIMESTAMP_TBL;
282 SELECT '' AS seven, f1 AS us_sql FROM ABSTIME_TBL;
284 SET DateStyle TO 'European,Postgres';
288 INSERT INTO TIMESTAMP_TBL VALUES('13/06/1957');
290 SELECT count(*) as one FROM TIMESTAMP_TBL WHERE d1 = 'Jun 13 1957';
292 SELECT '' AS "65", d1 AS european_postgres FROM TIMESTAMP_TBL;
294 SELECT '' AS seven, f1 AS european_postgres FROM ABSTIME_TBL;
296 SET DateStyle TO 'European,ISO';
300 SELECT '' AS "65", d1 AS european_iso FROM TIMESTAMP_TBL;
302 SELECT '' AS seven, f1 AS european_iso FROM ABSTIME_TBL;
304 SET DateStyle TO 'European,SQL';
308 SELECT '' AS "65", d1 AS european_sql FROM TIMESTAMP_TBL;
310 SELECT '' AS seven, f1 AS european_sql FROM ABSTIME_TBL;