4 SET DateStyle = 'Postgres, MDY';
7 -- Test various input formats
9 SELECT timestamp with time zone '20011227 040506+08';
10 SELECT timestamp with time zone '20011227 040506-08';
11 SELECT timestamp with time zone '20011227 040506.789+08';
12 SELECT timestamp with time zone '20011227 040506.789-08';
13 SELECT timestamp with time zone '20011227T040506+08';
14 SELECT timestamp with time zone '20011227T040506-08';
15 SELECT timestamp with time zone '20011227T040506.789+08';
16 SELECT timestamp with time zone '20011227T040506.789-08';
17 SELECT timestamp with time zone '2001-12-27 04:05:06.789-08';
18 SELECT timestamp with time zone '2001.12.27 04:05:06.789-08';
19 SELECT timestamp with time zone '2001/12/27 04:05:06.789-08';
20 SELECT timestamp with time zone '12/27/2001 04:05:06.789-08';
21 -- should fail in mdy mode:
22 SELECT timestamp with time zone '27/12/2001 04:05:06.789-08';
24 SELECT timestamp with time zone '27/12/2001 04:05:06.789-08';
26 SELECT timestamp with time zone 'Y2001M12D27H04M05S06.789+08';
27 SELECT timestamp with time zone 'Y2001M12D27H04M05S06.789-08';
28 SELECT timestamp with time zone 'Y2001M12D27H04MM05S06.789+08';
29 SELECT timestamp with time zone 'Y2001M12D27H04MM05S06.789-08';
30 SELECT timestamp with time zone 'J2452271+08';
31 SELECT timestamp with time zone 'J2452271-08';
32 SELECT timestamp with time zone 'J2452271.5+08';
33 SELECT timestamp with time zone 'J2452271.5-08';
34 SELECT timestamp with time zone 'J2452271 04:05:06+08';
35 SELECT timestamp with time zone 'J2452271 04:05:06-08';
36 SELECT timestamp with time zone 'J2452271T040506+08';
37 SELECT timestamp with time zone 'J2452271T040506-08';
38 SELECT timestamp with time zone 'J2452271T040506.789+08';
39 SELECT timestamp with time zone 'J2452271T040506.789-08';
40 -- German/European-style dates with periods as delimiters
41 SELECT timestamp with time zone '12.27.2001 04:05:06.789+08';
42 SELECT timestamp with time zone '12.27.2001 04:05:06.789-08';
43 SET DateStyle = 'German';
44 SELECT timestamp with time zone '27.12.2001 04:05:06.789+08';
45 SELECT timestamp with time zone '27.12.2001 04:05:06.789-08';
46 SET DateStyle = 'ISO';
47 -- As of 7.4, allow time without time zone having a time zone specified
48 SELECT time without time zone '040506.789+08';
49 SELECT time without time zone '040506.789-08';
50 SELECT time without time zone 'T040506.789+08';
51 SELECT time without time zone 'T040506.789-08';
52 SELECT time with time zone '040506.789+08';
53 SELECT time with time zone '040506.789-08';
54 SELECT time with time zone 'T040506.789+08';
55 SELECT time with time zone 'T040506.789-08';
56 SELECT time with time zone 'T040506.789 +08';
57 SELECT time with time zone 'T040506.789 -08';
58 SET DateStyle = 'Postgres, MDY';
61 -- date, time arithmetic
64 SELECT date '1981-02-03' + time '04:05:06' AS "Date + Time";
65 SELECT date '1991-02-03' + time with time zone '04:05:06 PST' AS "Date + Time PST";
66 SELECT date '2001-02-03' + time with time zone '04:05:06 UTC' AS "Date + Time UTC";
67 SELECT date '1991-02-03' + interval '2 years' AS "Add Two Years";
68 SELECT date '2001-12-13' - interval '2 years' AS "Subtract Two Years";
69 -- subtract time from date should not make sense; use interval instead
70 SELECT date '1991-02-03' - time '04:05:06' AS "Subtract Time";
71 SELECT date '1991-02-03' - time with time zone '04:05:06 UTC' AS "Subtract Time UTC";
74 -- timestamp, interval arithmetic
77 SELECT timestamp without time zone '1996-03-01' - interval '1 second' AS "Feb 29";
78 SELECT timestamp without time zone '1999-03-01' - interval '1 second' AS "Feb 28";
79 SELECT timestamp without time zone '2000-03-01' - interval '1 second' AS "Feb 29";
80 SELECT timestamp without time zone '1999-12-01' + interval '1 month - 1 second' AS "Dec 31";
81 SELECT timestamp without time zone 'Jan 1, 4713 BC' + interval '106000000 days' AS "Feb 23, 285506";
82 SELECT timestamp without time zone 'Jan 1, 4713 BC' + interval '107000000 days' AS "Jan 20, 288244";
83 SELECT timestamp without time zone 'Jan 1, 4713 BC' + interval '109203489 days' AS "Dec 31, 294276";
84 SELECT timestamp without time zone '12/31/294276' - timestamp without time zone '12/23/1999' AS "106751991 Days";
87 -- Not directly usable for regression testing since these are not constants.
88 -- So, just try to test parser and hope for the best - thomas 97/04/26
89 SELECT (timestamp without time zone 'today' = (timestamp without time zone 'yesterday' + interval '1 day')) as "True";
90 SELECT (timestamp without time zone 'today' = (timestamp without time zone 'tomorrow' - interval '1 day')) as "True";
91 SELECT (timestamp without time zone 'tomorrow' = (timestamp without time zone 'yesterday' + interval '2 days')) as "True";
92 SELECT (timestamp without time zone 'tomorrow' > 'now') as "True";
94 -- Convert from date and time to timestamp
95 -- This test used to be timestamp(date,time) but no longer allowed by grammar
96 -- to enable support for SQL99 timestamp type syntax.
97 SELECT date '1994-01-01' + time '11:00' AS "Jan_01_1994_11am";
98 SELECT date '1994-01-01' + time '10:00' AS "Jan_01_1994_10am";
99 SELECT date '1994-01-01' + timetz '11:00-5' AS "Jan_01_1994_8am";
100 SELECT timestamptz(date '1994-01-01', time with time zone '11:00-5') AS "Jan_01_1994_8am";
102 SELECT '' AS "64", d1 + interval '1 year' AS one_year FROM TIMESTAMP_TBL;
103 SELECT '' AS "64", d1 - interval '1 year' AS one_year FROM TIMESTAMP_TBL;
105 SELECT timestamp with time zone '1996-03-01' - interval '1 second' AS "Feb 29";
106 SELECT timestamp with time zone '1999-03-01' - interval '1 second' AS "Feb 28";
107 SELECT timestamp with time zone '2000-03-01' - interval '1 second' AS "Feb 29";
108 SELECT timestamp with time zone '1999-12-01' + interval '1 month - 1 second' AS "Dec 31";
110 SELECT (timestamp with time zone 'today' = (timestamp with time zone 'yesterday' + interval '1 day')) as "True";
111 SELECT (timestamp with time zone 'today' = (timestamp with time zone 'tomorrow' - interval '1 day')) as "True";
112 SELECT (timestamp with time zone 'tomorrow' = (timestamp with time zone 'yesterday' + interval '2 days')) as "True";
113 SELECT (timestamp with time zone 'tomorrow' > 'now') as "True";
115 -- timestamp with time zone, interval arithmetic around DST change
116 SET TIME ZONE 'CST7CDT';
117 SELECT timestamp with time zone '2005-04-02 12:00-07' + interval '1 day' as "Apr 3, 12:00";
118 SELECT timestamp with time zone '2005-04-02 12:00-07' + interval '24 hours' as "Apr 3, 13:00";
119 SELECT timestamp with time zone '2005-04-03 12:00-06' - interval '1 day' as "Apr 2, 12:00";
120 SELECT timestamp with time zone '2005-04-03 12:00-06' - interval '24 hours' as "Apr 2, 11:00";
124 SELECT timestamptz(date '1994-01-01', time '11:00') AS "Jan_01_1994_10am";
125 SELECT timestamptz(date '1994-01-01', time '10:00') AS "Jan_01_1994_9am";
126 SELECT timestamptz(date '1994-01-01', time with time zone '11:00-8') AS "Jan_01_1994_11am";
127 SELECT timestamptz(date '1994-01-01', time with time zone '10:00-8') AS "Jan_01_1994_10am";
128 SELECT timestamptz(date '1994-01-01', time with time zone '11:00-5') AS "Jan_01_1994_8am";
130 SELECT '' AS "64", d1 + interval '1 year' AS one_year FROM TIMESTAMPTZ_TBL;
131 SELECT '' AS "64", d1 - interval '1 year' AS one_year FROM TIMESTAMPTZ_TBL;
134 -- time, interval arithmetic
137 SELECT CAST(time '01:02' AS interval) AS "+01:02";
138 SELECT CAST(interval '02:03' AS time) AS "02:03:00";
139 SELECT time '01:30' + interval '02:01' AS "03:31:00";
140 SELECT time '01:30' - interval '02:01' AS "23:29:00";
141 SELECT time '02:30' + interval '36:01' AS "14:31:00";
142 SELECT time '03:30' + interval '1 month 04:01' AS "07:31:00";
143 SELECT CAST(time with time zone '01:02-08' AS interval) AS "+00:01";
144 SELECT CAST(interval '02:03' AS time with time zone) AS "02:03:00-08";
145 SELECT time with time zone '01:30-08' - interval '02:01' AS "23:29:00-08";
146 SELECT time with time zone '02:30-08' + interval '36:01' AS "14:31:00-08";
148 -- These two tests cannot be used because they default to current timezone,
149 -- which may be either -08 or -07 depending on the time of year.
150 -- SELECT time with time zone '01:30' + interval '02:01' AS "03:31:00-08";
151 -- SELECT time with time zone '03:30' + interval '1 month 04:01' AS "07:31:00-08";
152 -- Try the following two tests instead, as a poor substitute
154 SELECT CAST(CAST(date 'today' + time with time zone '05:30'
155 + interval '02:01' AS time with time zone) AS time) AS "07:31:00";
157 SELECT CAST(cast(date 'today' + time with time zone '03:30'
158 + interval '1 month 04:01' as timestamp without time zone) AS time) AS "07:31:00";
160 SELECT t.d1 AS t, i.f1 AS i, t.d1 + i.f1 AS "add", t.d1 - i.f1 AS "subtract"
161 FROM TIMESTAMP_TBL t, INTERVAL_TBL i
162 WHERE t.d1 BETWEEN '1990-01-01' AND '2001-01-01'
163 AND i.f1 BETWEEN '00:00' AND '23:00'
166 SELECT t.f1 AS t, i.f1 AS i, t.f1 + i.f1 AS "add", t.f1 - i.f1 AS "subtract"
167 FROM TIME_TBL t, INTERVAL_TBL i
170 SELECT t.f1 AS t, i.f1 AS i, t.f1 + i.f1 AS "add", t.f1 - i.f1 AS "subtract"
171 FROM TIMETZ_TBL t, INTERVAL_TBL i
174 -- SQL9x OVERLAPS operator
175 -- test with time zone
176 SELECT (timestamp with time zone '2000-11-27', timestamp with time zone '2000-11-28')
177 OVERLAPS (timestamp with time zone '2000-11-27 12:00', timestamp with time zone '2000-11-30') AS "True";
179 SELECT (timestamp with time zone '2000-11-26', timestamp with time zone '2000-11-27')
180 OVERLAPS (timestamp with time zone '2000-11-27 12:00', timestamp with time zone '2000-11-30') AS "False";
182 SELECT (timestamp with time zone '2000-11-27', timestamp with time zone '2000-11-28')
183 OVERLAPS (timestamp with time zone '2000-11-27 12:00', interval '1 day') AS "True";
185 SELECT (timestamp with time zone '2000-11-27', interval '12 hours')
186 OVERLAPS (timestamp with time zone '2000-11-27 12:00', timestamp with time zone '2000-11-30') AS "False";
188 SELECT (timestamp with time zone '2000-11-27', interval '12 hours')
189 OVERLAPS (timestamp with time zone '2000-11-27', interval '12 hours') AS "True";
191 SELECT (timestamp with time zone '2000-11-27', interval '12 hours')
192 OVERLAPS (timestamp with time zone '2000-11-27 12:00', interval '12 hours') AS "False";
194 -- test without time zone
195 SELECT (timestamp without time zone '2000-11-27', timestamp without time zone '2000-11-28')
196 OVERLAPS (timestamp without time zone '2000-11-27 12:00', timestamp without time zone '2000-11-30') AS "True";
198 SELECT (timestamp without time zone '2000-11-26', timestamp without time zone '2000-11-27')
199 OVERLAPS (timestamp without time zone '2000-11-27 12:00', timestamp without time zone '2000-11-30') AS "False";
201 SELECT (timestamp without time zone '2000-11-27', timestamp without time zone '2000-11-28')
202 OVERLAPS (timestamp without time zone '2000-11-27 12:00', interval '1 day') AS "True";
204 SELECT (timestamp without time zone '2000-11-27', interval '12 hours')
205 OVERLAPS (timestamp without time zone '2000-11-27 12:00', timestamp without time zone '2000-11-30') AS "False";
207 SELECT (timestamp without time zone '2000-11-27', interval '12 hours')
208 OVERLAPS (timestamp without time zone '2000-11-27', interval '12 hours') AS "True";
210 SELECT (timestamp without time zone '2000-11-27', interval '12 hours')
211 OVERLAPS (timestamp without time zone '2000-11-27 12:00', interval '12 hours') AS "False";
213 -- test time and interval
214 SELECT (time '00:00', time '01:00')
215 OVERLAPS (time '00:30', time '01:30') AS "True";
217 SELECT (time '00:00', interval '1 hour')
218 OVERLAPS (time '00:30', interval '1 hour') AS "True";
220 SELECT (time '00:00', interval '1 hour')
221 OVERLAPS (time '01:30', interval '1 hour') AS "False";
223 -- SQL99 seems to want this to be false (and we conform to the spec).
224 -- istm that this *should* return true, on the theory that time
225 -- intervals can wrap around the day boundary - thomas 2001-09-25
226 SELECT (time '00:00', interval '1 hour')
227 OVERLAPS (time '01:30', interval '1 day') AS "False";
229 CREATE TABLE TEMP_TIMESTAMP (f1 timestamp with time zone);
231 -- get some candidate input values
233 INSERT INTO TEMP_TIMESTAMP (f1)
234 SELECT d1 FROM TIMESTAMP_TBL
235 WHERE d1 BETWEEN '13-jun-1957' AND '1-jan-1997'
236 OR d1 BETWEEN '1-jan-1999' AND '1-jan-2010';
238 SELECT '' AS "16", f1 AS "timestamp"
240 ORDER BY "timestamp";
242 SELECT '' AS "160", d.f1 AS "timestamp", t.f1 AS "interval", d.f1 + t.f1 AS plus
243 FROM TEMP_TIMESTAMP d, INTERVAL_TBL t
244 ORDER BY plus, "timestamp", "interval";
246 SELECT '' AS "160", d.f1 AS "timestamp", t.f1 AS "interval", d.f1 - t.f1 AS minus
247 FROM TEMP_TIMESTAMP d, INTERVAL_TBL t
249 ORDER BY minus, "timestamp", "interval";
251 SELECT '' AS "16", d.f1 AS "timestamp",
252 timestamp with time zone '1980-01-06 00:00 GMT' AS gpstime_zero,
253 d.f1 - timestamp with time zone '1980-01-06 00:00 GMT' AS difference
254 FROM TEMP_TIMESTAMP d
257 SELECT '' AS "226", d1.f1 AS timestamp1, d2.f1 AS timestamp2, d1.f1 - d2.f1 AS difference
258 FROM TEMP_TIMESTAMP d1, TEMP_TIMESTAMP d2
259 ORDER BY timestamp1, timestamp2, difference;
262 -- abstime, reltime arithmetic
265 SELECT '' AS ten, ABSTIME_TBL.f1 AS abstime, RELTIME_TBL.f1 AS reltime
266 FROM ABSTIME_TBL, RELTIME_TBL
267 WHERE (ABSTIME_TBL.f1 + RELTIME_TBL.f1) < abstime 'Jan 14 14:00:00 1971'
268 ORDER BY abstime, reltime;
270 -- these four queries should return the same answer
271 -- the "infinity" and "-infinity" tuples in ABSTIME_TBL cannot be added and
272 -- therefore, should not show up in the results.
274 SELECT '' AS three, * FROM ABSTIME_TBL
275 WHERE (ABSTIME_TBL.f1 + reltime '@ 3 year') -- +3 years
276 < abstime 'Jan 14 14:00:00 1977';
278 SELECT '' AS three, * FROM ABSTIME_TBL
279 WHERE (ABSTIME_TBL.f1 + reltime '@ 3 year ago') -- -3 years
280 < abstime 'Jan 14 14:00:00 1971';
282 SELECT '' AS three, * FROM ABSTIME_TBL
283 WHERE (ABSTIME_TBL.f1 - reltime '@ 3 year') -- -(+3) years
284 < abstime 'Jan 14 14:00:00 1971';
286 SELECT '' AS three, * FROM ABSTIME_TBL
287 WHERE (ABSTIME_TBL.f1 - reltime '@ 3 year ago') -- -(-3) years
288 < abstime 'Jan 14 14:00:00 1977';
294 SELECT '' AS "16", f1 AS "timestamp", date(f1) AS date
296 WHERE f1 <> timestamp 'now'
297 ORDER BY date, "timestamp";
299 SELECT '' AS "16", f1 AS "timestamp", abstime(f1) AS abstime
303 SELECT '' AS four, f1 AS abstime, date(f1) AS date
305 WHERE isfinite(f1) AND f1 <> abstime 'now'
306 ORDER BY date, abstime;
308 SELECT '' AS two, d1 AS "timestamp", abstime(d1) AS abstime
309 FROM TIMESTAMP_TBL WHERE NOT isfinite(d1);
311 SELECT '' AS three, f1 as abstime, cast(f1 as timestamp) AS "timestamp"
312 FROM ABSTIME_TBL WHERE NOT isfinite(f1);
314 SELECT '' AS ten, f1 AS interval, reltime(f1) AS reltime
317 SELECT '' AS six, f1 as reltime, CAST(f1 AS interval) AS interval
320 DROP TABLE TEMP_TIMESTAMP;
326 SET DateStyle TO 'US,Postgres';
330 SELECT '' AS "64", d1 AS us_postgres FROM TIMESTAMP_TBL;
332 SELECT '' AS seven, f1 AS us_postgres FROM ABSTIME_TBL;
334 SET DateStyle TO 'US,ISO';
336 SELECT '' AS "64", d1 AS us_iso FROM TIMESTAMP_TBL;
338 SELECT '' AS seven, f1 AS us_iso FROM ABSTIME_TBL;
340 SET DateStyle TO 'US,SQL';
344 SELECT '' AS "64", d1 AS us_sql FROM TIMESTAMP_TBL;
346 SELECT '' AS seven, f1 AS us_sql FROM ABSTIME_TBL;
348 SET DateStyle TO 'European,Postgres';
352 INSERT INTO TIMESTAMP_TBL VALUES('13/06/1957');
354 SELECT count(*) as one FROM TIMESTAMP_TBL WHERE d1 = 'Jun 13 1957';
356 SELECT '' AS "65", d1 AS european_postgres FROM TIMESTAMP_TBL;
358 SELECT '' AS seven, f1 AS european_postgres FROM ABSTIME_TBL;
360 SET DateStyle TO 'European,ISO';
364 SELECT '' AS "65", d1 AS european_iso FROM TIMESTAMP_TBL;
366 SELECT '' AS seven, f1 AS european_iso FROM ABSTIME_TBL;
368 SET DateStyle TO 'European,SQL';
372 SELECT '' AS "65", d1 AS european_sql FROM TIMESTAMP_TBL;
374 SELECT '' AS seven, f1 AS european_sql FROM ABSTIME_TBL;
382 SELECT to_timestamp('0097/Feb/16 --> 08:14:30', 'YYYY/Mon/DD --> HH:MI:SS');
384 SELECT to_timestamp('97/2/16 8:14:30', 'FMYYYY/FMMM/FMDD FMHH:FMMI:FMSS');
386 SELECT to_timestamp('1985 January 12', 'YYYY FMMonth DD');
388 SELECT to_timestamp('My birthday-> Year: 1976, Month: May, Day: 16',
389 '"My birthday-> Year" YYYY, "Month:" FMMonth, "Day:" DD');
391 SELECT to_timestamp('1,582nd VIII 21', 'Y,YYYth FMRM DD');
393 SELECT to_timestamp('15 "text between quote marks" 98 54 45',
394 E'HH24 "\\text between quote marks\\"" YY MI SS');
396 SELECT to_timestamp('05121445482000', 'MMDDHH24MISSYYYY');
398 SELECT to_timestamp('2000January09Sunday', 'YYYYFMMonthDDFMDay');
400 SELECT to_timestamp('97/Feb/16', 'YYMonDD');
402 SELECT to_timestamp('19971116', 'YYYYMMDD');
404 SELECT to_timestamp('20000-1116', 'YYYY-MMDD');
406 SELECT to_timestamp('9-1116', 'Y-MMDD');
408 SELECT to_timestamp('95-1116', 'YY-MMDD');
410 SELECT to_timestamp('995-1116', 'YYY-MMDD');
412 SELECT to_timestamp('2005426', 'YYYYWWD');
414 SELECT to_timestamp('2005300', 'YYYYDDD');
416 SELECT to_timestamp('2005527', 'IYYYIWID');
418 SELECT to_timestamp('005527', 'IYYIWID');
420 SELECT to_timestamp('05527', 'IYIWID');
422 SELECT to_timestamp('5527', 'IIWID');
424 SELECT to_timestamp('2005364', 'IYYYIDDD');
426 SELECT to_timestamp('20050302', 'YYYYMMDD');
428 SELECT to_timestamp('2005 03 02', 'YYYYMMDD');
430 SELECT to_timestamp(' 2005 03 02', 'YYYYMMDD');
432 SELECT to_timestamp(' 20050302', 'YYYYMMDD');
435 -- Check errors for some incorrect usages of to_timestamp()
438 -- Mixture of date conventions (ISO week and Gregorian):
439 SELECT to_timestamp('2005527', 'YYYYIWID');
441 -- Insufficient characters in the source string:
442 SELECT to_timestamp('19971', 'YYYYMMDD');
444 -- Insufficient digit characters for a single node:
445 SELECT to_timestamp('19971)24', 'YYYYMMDD');
448 SELECT to_timestamp('1997-11-Jan-16', 'YYYY-MM-Mon-DD');
450 -- Non-numeric input:
451 SELECT to_timestamp('199711xy', 'YYYYMMDD');
453 -- Input that doesn't fit in an int:
454 SELECT to_timestamp('10000000000', 'FMYYYY');