]> granicus.if.org Git - postgresql/blob - src/test/regress/sql/horology.sql
Accept an INTERVAL argument for SET TIME ZONE per SQL99.
[postgresql] / src / test / regress / sql / horology.sql
1 --
2 -- HOROLOGY
3 --
4 -- needed so tests pass even in Australia
5 SET australian_timezones = 'off';
6 SET DateStyle = 'Postgres,US';
7
8 --
9 -- date, time arithmetic
10 --
11
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";
20
21 --
22 -- timestamp, interval arithmetic
23 --
24
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";
29
30 -- Shorthand values
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";
37
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";
45
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;
48
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";
53
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";
58
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";
64
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;
67
68 --
69 -- time, interval arithmetic
70 --
71
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";
83
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
89
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";
92
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";
95
96 SELECT interval '04:30' - time with time zone '01:02-05' AS "20:32:00-05";
97
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';
101
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';
105
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;
108
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;
111
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";
116
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";
119
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";
122
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";
125
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";
128
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";
131
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";
135
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";
138
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";
141
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";
144
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";
147
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";
150
151 -- test time and interval
152 SELECT (time '00:00', time '01:00')
153   OVERLAPS (time '00:30', time '01:30') AS "True";
154
155 SELECT (time '00:00', interval '1 hour')
156   OVERLAPS (time '00:30', interval '1 hour') AS "True";
157
158 SELECT (time '00:00', interval '1 hour')
159   OVERLAPS (time '01:30', interval '1 hour') AS "False";
160
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";
166
167 CREATE TABLE TEMP_TIMESTAMP (f1 timestamp);
168
169 -- get some candidate input values
170
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';
175
176 SELECT '' AS "16", f1 AS "timestamp"
177   FROM TEMP_TIMESTAMP
178   ORDER BY "timestamp";
179
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";
183
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
186   WHERE isfinite(d.f1)
187   ORDER BY minus, "timestamp", "interval";
188
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
192   ORDER BY difference;
193
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;
197
198 --
199 -- abstime, reltime arithmetic
200 --
201
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;
205
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.
209
210 SELECT '' AS three, ABSTIME_TBL.*
211   WHERE  (ABSTIME_TBL.f1 + reltime '@ 3 year')         -- +3 years
212     < abstime 'Jan 14 14:00:00 1977';
213
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';
217
218 SELECT '' AS three, ABSTIME_TBL.*
219    WHERE  (ABSTIME_TBL.f1 - reltime '@ 3 year')        -- -(+3) years
220     < abstime 'Jan 14 14:00:00 1971';
221
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';
225
226 --
227 -- Conversions
228 --
229
230 SELECT '' AS "16", f1 AS "timestamp", date(f1) AS date
231   FROM TEMP_TIMESTAMP
232   WHERE f1 <> timestamp 'current'
233   ORDER BY date, "timestamp";
234
235 SELECT '' AS "16", f1 AS "timestamp", abstime(f1) AS abstime
236   FROM TEMP_TIMESTAMP
237   ORDER BY abstime;
238
239 SELECT '' AS four, f1 AS abstime, date(f1) AS date
240   FROM ABSTIME_TBL
241   WHERE isfinite(f1) AND f1 <> abstime 'current'
242   ORDER BY date, abstime;
243
244 SELECT '' AS two, d1 AS "timestamp", abstime(d1) AS abstime
245   FROM TIMESTAMP_TBL WHERE NOT isfinite(d1);
246
247 SELECT '' AS three, f1 as abstime, cast(f1 as timestamp) AS "timestamp"
248   FROM ABSTIME_TBL WHERE NOT isfinite(f1);
249
250 SELECT '' AS ten, f1 AS interval, reltime(f1) AS reltime
251   FROM INTERVAL_TBL;
252
253 SELECT '' AS six, f1 as reltime, CAST(f1 AS interval) AS interval
254   FROM RELTIME_TBL;
255
256 DROP TABLE TEMP_TIMESTAMP;
257
258 --
259 -- Formats
260 --
261
262 SET DateStyle TO 'US,Postgres';
263
264 SHOW DateStyle;
265
266 SELECT '' AS "64", d1 AS us_postgres FROM TIMESTAMP_TBL;
267
268 SELECT '' AS seven, f1 AS us_postgres FROM ABSTIME_TBL;
269
270 SET DateStyle TO 'US,ISO';
271
272 SELECT '' AS "64", d1 AS us_iso FROM TIMESTAMP_TBL;
273
274 SELECT '' AS seven, f1 AS us_iso FROM ABSTIME_TBL;
275
276 SET DateStyle TO 'US,SQL';
277
278 SHOW DateStyle;
279
280 SELECT '' AS "64", d1 AS us_sql FROM TIMESTAMP_TBL;
281
282 SELECT '' AS seven, f1 AS us_sql FROM ABSTIME_TBL;
283
284 SET DateStyle TO 'European,Postgres';
285
286 SHOW DateStyle;
287
288 INSERT INTO TIMESTAMP_TBL VALUES('13/06/1957');
289
290 SELECT count(*) as one FROM TIMESTAMP_TBL WHERE d1 = 'Jun 13 1957';
291
292 SELECT '' AS "65", d1 AS european_postgres FROM TIMESTAMP_TBL;
293
294 SELECT '' AS seven, f1 AS european_postgres FROM ABSTIME_TBL;
295
296 SET DateStyle TO 'European,ISO';
297
298 SHOW DateStyle;
299
300 SELECT '' AS "65", d1 AS european_iso FROM TIMESTAMP_TBL;
301
302 SELECT '' AS seven, f1 AS european_iso FROM ABSTIME_TBL;
303
304 SET DateStyle TO 'European,SQL';
305
306 SHOW DateStyle;
307
308 SELECT '' AS "65", d1 AS european_sql FROM TIMESTAMP_TBL;
309
310 SELECT '' AS seven, f1 AS european_sql FROM ABSTIME_TBL;
311
312 RESET DateStyle;