6 SET IntervalStyle to postgres;
8 -- check acceptance of "time zone style"
9 SELECT INTERVAL '01:00' AS "One hour";
10 SELECT INTERVAL '+02:00' AS "Two hours";
11 SELECT INTERVAL '-08:00' AS "Eight hours";
12 SELECT INTERVAL '-1 +02:03' AS "22 hours ago...";
13 SELECT INTERVAL '-1 days +02:03' AS "22 hours ago...";
14 SELECT INTERVAL '1.5 weeks' AS "Ten days twelve hours";
15 SELECT INTERVAL '1.5 months' AS "One month 15 days";
16 SELECT INTERVAL '10 years -11 month -12 days +13:14' AS "9 years...";
18 CREATE TABLE INTERVAL_TBL (f1 interval);
20 INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 1 minute');
21 INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 5 hour');
22 INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 10 day');
23 INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 34 year');
24 INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 3 months');
25 INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 14 seconds ago');
26 INSERT INTO INTERVAL_TBL (f1) VALUES ('1 day 2 hours 3 minutes 4 seconds');
27 INSERT INTO INTERVAL_TBL (f1) VALUES ('6 years');
28 INSERT INTO INTERVAL_TBL (f1) VALUES ('5 months');
29 INSERT INTO INTERVAL_TBL (f1) VALUES ('5 months 12 hours');
31 -- badly formatted interval
32 INSERT INTO INTERVAL_TBL (f1) VALUES ('badly formatted interval');
33 INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 30 eons ago');
35 -- test interval operators
37 SELECT '' AS ten, * FROM INTERVAL_TBL;
39 SELECT '' AS nine, * FROM INTERVAL_TBL
40 WHERE INTERVAL_TBL.f1 <> interval '@ 10 days';
42 SELECT '' AS three, * FROM INTERVAL_TBL
43 WHERE INTERVAL_TBL.f1 <= interval '@ 5 hours';
45 SELECT '' AS three, * FROM INTERVAL_TBL
46 WHERE INTERVAL_TBL.f1 < interval '@ 1 day';
48 SELECT '' AS one, * FROM INTERVAL_TBL
49 WHERE INTERVAL_TBL.f1 = interval '@ 34 years';
51 SELECT '' AS five, * FROM INTERVAL_TBL
52 WHERE INTERVAL_TBL.f1 >= interval '@ 1 month';
54 SELECT '' AS nine, * FROM INTERVAL_TBL
55 WHERE INTERVAL_TBL.f1 > interval '@ 3 seconds ago';
57 SELECT '' AS fortyfive, r1.*, r2.*
58 FROM INTERVAL_TBL r1, INTERVAL_TBL r2
60 ORDER BY r1.f1, r2.f1;
63 -- Test multiplication and division with intervals.
64 -- Floating point arithmetic rounding errors can lead to unexpected results,
65 -- though the code attempts to do the right thing and round up to days and
66 -- minutes to avoid results such as '3 days 24:00 hours' or '14:20:60'.
67 -- Note that it is expected for some day components to be greater than 29 and
68 -- some time components be greater than 23:59:59 due to how intervals are
71 CREATE TABLE INTERVAL_MULDIV_TBL (span interval);
72 COPY INTERVAL_MULDIV_TBL FROM STDIN;
74 -41 mon -12 days +360:00
76 9 mon -27 days 12:34:56
77 -3 years 482 days 76:54:32.189
83 SELECT span * 0.3 AS product
84 FROM INTERVAL_MULDIV_TBL;
86 SELECT span * 8.2 AS product
87 FROM INTERVAL_MULDIV_TBL;
89 SELECT span / 10 AS quotient
90 FROM INTERVAL_MULDIV_TBL;
92 SELECT span / 100 AS quotient
93 FROM INTERVAL_MULDIV_TBL;
95 DROP TABLE INTERVAL_MULDIV_TBL;
97 SET DATESTYLE = 'postgres';
98 SET IntervalStyle to postgres_verbose;
100 SELECT '' AS ten, * FROM INTERVAL_TBL;
102 -- test avg(interval), which is somewhat fragile since people have been
103 -- known to change the allowed input syntax for type interval without
104 -- updating pg_aggregate.agginitval
106 select avg(f1) from interval_tbl;
108 -- test long interval input
109 select '4 millenniums 5 centuries 4 decades 1 year 4 months 4 days 17 minutes 31 seconds'::interval;
112 -- test justify_hours() and justify_days()
114 SELECT justify_hours(interval '6 months 3 days 52 hours 3 minutes 2 seconds') as "6 mons 5 days 4 hours 3 mins 2 seconds";
115 SELECT justify_days(interval '6 months 36 days 5 hours 4 minutes 3 seconds') as "7 mons 6 days 5 hours 4 mins 3 seconds";
117 -- test justify_interval()
119 SELECT justify_interval(interval '1 month -1 hour') as "1 month -1 hour";
121 -- test fractional second input, and detection of duplicate units
122 SET DATESTYLE = 'ISO';
123 SET IntervalStyle TO postgres;
125 SELECT '1 millisecond'::interval, '1 microsecond'::interval,
126 '500 seconds 99 milliseconds 51 microseconds'::interval;
127 SELECT '3 days 5 milliseconds'::interval;
129 SELECT '1 second 2 seconds'::interval; -- error
130 SELECT '10 milliseconds 20 milliseconds'::interval; -- error
131 SELECT '5.5 seconds 3 milliseconds'::interval; -- error
132 SELECT '1:20:05 5 microseconds'::interval; -- error
133 SELECT interval '1-2'; -- SQL year-month literal
135 -- test SQL-spec syntaxes for restricted field sets
136 SELECT interval '1' year;
137 SELECT interval '2' month;
138 SELECT interval '3' day;
139 SELECT interval '4' hour;
140 SELECT interval '5' minute;
141 SELECT interval '6' second;
142 SELECT interval '1' year to month;
143 SELECT interval '1-2' year to month;
144 SELECT interval '1 2' day to hour;
145 SELECT interval '1 2:03' day to hour;
146 SELECT interval '1 2:03:04' day to hour;
147 SELECT interval '1 2' day to minute;
148 SELECT interval '1 2:03' day to minute;
149 SELECT interval '1 2:03:04' day to minute;
150 SELECT interval '1 2' day to second;
151 SELECT interval '1 2:03' day to second;
152 SELECT interval '1 2:03:04' day to second;
153 SELECT interval '1 2' hour to minute;
154 SELECT interval '1 2:03' hour to minute;
155 SELECT interval '1 2:03:04' hour to minute;
156 SELECT interval '1 2' hour to second;
157 SELECT interval '1 2:03' hour to second;
158 SELECT interval '1 2:03:04' hour to second;
159 SELECT interval '1 2' minute to second;
160 SELECT interval '1 2:03' minute to second;
161 SELECT interval '1 2:03:04' minute to second;
163 -- test syntaxes for restricted precision
164 SELECT interval(0) '1 day 01:23:45.6789';
165 SELECT interval(2) '1 day 01:23:45.6789';
166 SELECT interval '12:34.5678' minute to second(2); -- per SQL spec
167 SELECT interval(2) '12:34.5678' minute to second; -- historical PG
168 SELECT interval(2) '12:34.5678' minute to second(2); -- syntax error
169 SELECT interval '1.234' second;
170 SELECT interval '1.234' second(2);
171 SELECT interval '1 2.345' day to second(2);
172 SELECT interval '1 2:03' day to second(2);
173 SELECT interval '1 2:03.4567' day to second(2);
174 SELECT interval '1 2:03:04.5678' day to second(2);
175 SELECT interval '1 2.345' hour to second(2);
176 SELECT interval '1 2:03.45678' hour to second(2);
177 SELECT interval '1 2:03:04.5678' hour to second(2);
178 SELECT interval '1 2.3456' minute to second(2);
179 SELECT interval '1 2:03.5678' minute to second(2);
180 SELECT interval '1 2:03:04.5678' minute to second(2);
182 -- test inputting and outputting SQL standard interval literals
183 SET IntervalStyle TO sql_standard;
184 SELECT interval '0' AS "zero",
185 interval '1-2' year to month AS "year-month",
186 interval '1 2:03:04' day to second AS "day-time",
187 - interval '1-2' AS "negative year-month",
188 - interval '1 2:03:04' AS "negative day-time";
190 -- test input of some not-quite-standard interval values in the sql style
191 SET IntervalStyle TO postgres;
192 SELECT interval '+1 -1:00:00',
193 interval '-1 +1:00:00',
194 interval '+1-2 -3 +4:05:06.789',
195 interval '-1-2 +3 -4:05:06.789';
197 -- test output of couple non-standard interval values in the sql style
198 SET IntervalStyle TO sql_standard;
199 SELECT interval '1 day -1 hours',
200 interval '-1 days +1 hours',
201 interval '1 years 2 months -3 days 4 hours 5 minutes 6.789 seconds',
202 - interval '1 years 2 months -3 days 4 hours 5 minutes 6.789 seconds';
204 -- test outputting iso8601 intervals
205 SET IntervalStyle to iso_8601;
206 select interval '0' AS "zero",
207 interval '1-2' AS "a year 2 months",
208 interval '1 2:03:04' AS "a bit over a day",
209 interval '2:03:04.45679' AS "a bit over 2 hours",
210 (interval '1-2' + interval '3 4:05:06.7') AS "all fields",
211 (interval '1-2' - interval '3 4:05:06.7') AS "mixed sign",
212 (- interval '1-2' + interval '3 4:05:06.7') AS "negative";
214 -- test inputting ISO 8601 4.4.2.1 "Format With Time Unit Designators"
215 SET IntervalStyle to sql_standard;
216 select interval 'P0Y' AS "zero",
217 interval 'P1Y2M' AS "a year 2 months",
218 interval 'P1W' AS "a week",
219 interval 'P1DT2H3M4S' AS "a bit over a day",
220 interval 'P1Y2M3DT4H5M6.7S' AS "all fields",
221 interval 'P-1Y-2M-3DT-4H-5M-6.7S' AS "negative",
222 interval 'PT-0.1S' AS "fractional second";
224 -- test inputting ISO 8601 4.4.2.2 "Alternative Format"
225 SET IntervalStyle to postgres;
226 select interval 'P00021015T103020' AS "ISO8601 Basic Format",
227 interval 'P0002-10-15T10:30:20' AS "ISO8601 Extended Format";
229 -- Make sure optional ISO8601 alternative format fields are optional.
230 select interval 'P0002' AS "year only",
231 interval 'P0002-10' AS "year month",
232 interval 'P0002-10-15' AS "year month day",
233 interval 'P0002T1S' AS "year only plus time",
234 interval 'P0002-10T1S' AS "year month plus time",
235 interval 'P0002-10-15T1S' AS "year month day plus time",
236 interval 'PT10' AS "hour only",
237 interval 'PT10:30' AS "hour minute";
239 -- test a couple rounding cases that changed since 8.3 w/ HAVE_INT64_TIMESTAMP.
240 SET IntervalStyle to postgres_verbose;
241 select interval '-10 mons -3 days +03:55:06.70';
242 select interval '1 year 2 mons 3 days 04:05:06.699999';
243 select interval '0:0:0.7', interval '@ 0.70 secs', interval '0.7 seconds';
245 -- check that '30 days' equals '1 month' according to the hash function
246 select '30 days'::interval = '1 month'::interval as t;
247 select interval_hash('30 days'::interval) = interval_hash('1 month'::interval) as t;