horology.sql 12.3 KB
Newer Older
1
--
2
-- HOROLOGY
3
--
4 5 6 7
-- needed so tests pass even in Australia
SET australian_timezones = 'off';
SET DateStyle = 'Postgres,US';

8 9 10 11 12 13 14 15 16
--
-- date, time arithmetic
--

SELECT date '1981-02-03' + time '04:05:06' AS "Date + Time";
SELECT date '1991-02-03' + time with time zone '04:05:06 PST' AS "Date + Time PST";
SELECT date '2001-02-03' + time with time zone '04:05:06 UTC' AS "Date + Time UTC";
SELECT date '1991-02-03' + interval '2 years' AS "Add Two Years";
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 19 20
SELECT date '1991-02-03' - time '04:05:06' AS "Subtract Time";
SELECT date '1991-02-03' - time with time zone '04:05:06 UTC' AS "Subtract Time UTC";

21
--
22
-- timestamp, interval arithmetic
23 24
--

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
SELECT timestamp without time zone '1996-03-01' - interval '1 second' AS "Feb 29";
SELECT timestamp without time zone '1999-03-01' - interval '1 second' AS "Feb 28";
SELECT timestamp without time zone '2000-03-01' - interval '1 second' AS "Feb 29";
SELECT timestamp without time zone '1999-12-01' + interval '1 month - 1 second' AS "Dec 31";

-- Shorthand values
-- Not directly usable for regression testing since these are not constants.
-- So, just try to test parser and hope for the best - thomas 97/04/26
SELECT (timestamp without time zone 'today' = (timestamp without time zone 'yesterday' + interval '1 day')) as "True";
SELECT (timestamp without time zone 'today' = (timestamp without time zone 'tomorrow' - interval '1 day')) as "True";
SELECT (timestamp without time zone 'tomorrow' = (timestamp without time zone 'yesterday' + interval '2 days')) as "True";
SELECT (timestamp without time zone 'tomorrow' > 'now') as "True";

SELECT timestamp(date '1994-01-01', time '11:00') AS "Jan_01_1994_11am";
SELECT timestamp(date '1994-01-01', time '10:00') AS "Jan_01_1994_10am";
SELECT timestamp(date '1994-01-01', time '11:00-5') AS "Jan_01_1994_8am";
SELECT timestamp(date '1994-01-01', time with time zone '11:00-5') AS "Jan_01_1994_11am";

SELECT '' AS "64", d1 + interval '1 year' AS one_year FROM TIMESTAMP_TBL;
SELECT '' AS "64", d1 - interval '1 year' AS one_year FROM TIMESTAMP_TBL;

SELECT timestamp with time zone '1996-03-01' - interval '1 second' AS "Feb 29";
SELECT timestamp with time zone '1999-03-01' - interval '1 second' AS "Feb 28";
SELECT timestamp with time zone '2000-03-01' - interval '1 second' AS "Feb 29";
SELECT timestamp with time zone '1999-12-01' + interval '1 month - 1 second' AS "Dec 31";

SELECT (timestamp with time zone 'today' = (timestamp with time zone 'yesterday' + interval '1 day')) as "True";
SELECT (timestamp with time zone 'today' = (timestamp with time zone 'tomorrow' - interval '1 day')) as "True";
SELECT (timestamp with time zone 'tomorrow' = (timestamp with time zone 'yesterday' + interval '2 days')) as "True";
SELECT (timestamp with time zone 'tomorrow' > 'now') as "True";

SELECT timestamptz(date '1994-01-01', time '11:00') AS "Jan_01_1994_10am";
SELECT timestamptz(date '1994-01-01', time '10:00') AS "Jan_01_1994_9am";
SELECT timestamptz(date '1994-01-01', time with time zone '11:00-8') AS "Jan_01_1994_11am";
SELECT timestamptz(date '1994-01-01', time with time zone '10:00-8') AS "Jan_01_1994_10am";
SELECT timestamptz(date '1994-01-01', time with time zone '11:00-5') AS "Jan_01_1994_8am";

SELECT '' AS "64", d1 + interval '1 year' AS one_year FROM TIMESTAMPTZ_TBL;
SELECT '' AS "64", d1 - interval '1 year' AS one_year FROM TIMESTAMPTZ_TBL;
64

65 66 67 68 69 70 71 72 73 74
--
-- time, interval arithmetic
--

SELECT CAST(time '01:02' AS interval) AS "+01:02";
SELECT CAST(interval '02:03' AS time) AS "02:03:00";
SELECT time '01:30' + interval '02:01' AS "03:31:00";
SELECT time '01:30' - interval '02:01' AS "23:29:00";
SELECT time '02:30' + interval '36:01' AS "14:31:00";
SELECT time '03:30' + interval '1 month 04:01' AS "07:31:00";
75
SELECT interval '04:30' - time '01:02' AS "20:32:00";
76 77 78 79 80
SELECT CAST(time with time zone '01:02-08' AS interval) AS "+00:01";
SELECT CAST(interval '02:03' AS time with time zone) AS "02:03:00-08";
SELECT time with time zone '01:30-08' - interval '02:01' AS "23:29:00-08";
SELECT time with time zone '02:30-08' + interval '36:01' AS "14:31:00-08";

81 82 83 84
-- These two tests cannot be used because they default to current timezone,
-- which may be either -08 or -07 depending on the time of year.
-- SELECT time with time zone '01:30' + interval '02:01' AS "03:31:00-08";
-- SELECT time with time zone '03:30' + interval '1 month 04:01' AS "07:31:00-08";
85 86
-- Try the following two tests instead, as a poor substitute

87 88
SELECT CAST(CAST(date 'today' + time with time zone '01:30'
            + interval '02:01' AS time with time zone) AS time) AS "03:31:00";
89

90 91
SELECT CAST(CAST(date 'today' + time with time zone '03:30'
            + interval '1 month 04:01' AS time with time zone) AS time) AS "07:31:00";
92

93
SELECT interval '04:30' - time with time zone '01:02-05' AS "20:32:00-05";
94 95 96 97 98 99 100 101 102 103 104 105

SELECT t.d1 + i.f1 AS "102" FROM TIMESTAMP_TBL t, INTERVAL_TBL i
  WHERE t.d1 BETWEEN '1990-01-01' AND '2001-01-01'
    AND i.f1 BETWEEN '00:00' AND '23:00';

SELECT t.d1 - i.f1 AS "102" FROM TIMESTAMP_TBL t, INTERVAL_TBL i
  WHERE t.d1 BETWEEN '1990-01-01' AND '2001-01-01'
    AND i.f1 BETWEEN '00:00' AND '23:00';

SELECT t.f1 + i.f1 AS "80" FROM TIME_TBL t, INTERVAL_TBL i;
SELECT t.f1 - i.f1 AS "80" FROM TIME_TBL t, INTERVAL_TBL i;

106 107
SELECT t.f1 + i.f1 AS "100" FROM TIMETZ_TBL t, INTERVAL_TBL i;
SELECT t.f1 - i.f1 AS "100" FROM TIMETZ_TBL t, INTERVAL_TBL i;
108 109

-- SQL9x OVERLAPS operator
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
-- test with time zone
SELECT (timestamp with time zone '2000-11-27', timestamp with time zone '2000-11-28')
  OVERLAPS (timestamp with time zone '2000-11-27 12:00', timestamp with time zone '2000-11-30') AS "True";

SELECT (timestamp with time zone '2000-11-26', timestamp with time zone '2000-11-27')
  OVERLAPS (timestamp with time zone '2000-11-27 12:00', timestamp with time zone '2000-11-30') AS "False";

SELECT (timestamp with time zone '2000-11-27', timestamp with time zone '2000-11-28')
  OVERLAPS (timestamp with time zone '2000-11-27 12:00', interval '1 day') AS "True";

SELECT (timestamp with time zone '2000-11-27', interval '12 hours')
  OVERLAPS (timestamp with time zone '2000-11-27 12:00', timestamp with time zone '2000-11-30') AS "False";

SELECT (timestamp with time zone '2000-11-27', interval '12 hours')
  OVERLAPS (timestamp with time zone '2000-11-27', interval '12 hours') AS "True";

SELECT (timestamp with time zone '2000-11-27', interval '12 hours')
  OVERLAPS (timestamp with time zone '2000-11-27 12:00', interval '12 hours') AS "False";
128

129 130 131
-- test without time zone
SELECT (timestamp without time zone '2000-11-27', timestamp without time zone '2000-11-28')
  OVERLAPS (timestamp without time zone '2000-11-27 12:00', timestamp without time zone '2000-11-30') AS "True";
132

133 134
SELECT (timestamp without time zone '2000-11-26', timestamp without time zone '2000-11-27')
  OVERLAPS (timestamp without time zone '2000-11-27 12:00', timestamp without time zone '2000-11-30') AS "False";
135

136 137
SELECT (timestamp without time zone '2000-11-27', timestamp without time zone '2000-11-28')
  OVERLAPS (timestamp without time zone '2000-11-27 12:00', interval '1 day') AS "True";
138

139 140
SELECT (timestamp without time zone '2000-11-27', interval '12 hours')
  OVERLAPS (timestamp without time zone '2000-11-27 12:00', timestamp without time zone '2000-11-30') AS "False";
141

142 143
SELECT (timestamp without time zone '2000-11-27', interval '12 hours')
  OVERLAPS (timestamp without time zone '2000-11-27', interval '12 hours') AS "True";
144

145 146
SELECT (timestamp without time zone '2000-11-27', interval '12 hours')
  OVERLAPS (timestamp without time zone '2000-11-27 12:00', interval '12 hours') AS "False";
147

148
-- test time and interval
149 150 151 152 153 154 155 156 157
SELECT (time '00:00', time '01:00')
  OVERLAPS (time '00:30', time '01:30') AS "True";

SELECT (time '00:00', interval '1 hour')
  OVERLAPS (time '00:30', interval '1 hour') AS "True";

SELECT (time '00:00', interval '1 hour')
  OVERLAPS (time '01:30', interval '1 hour') AS "False";

158 159 160
-- SQL99 seems to want this to be false (and we conform to the spec).
-- istm that this *should* return true, on the theory that time
-- intervals can wrap around the day boundary - thomas 2001-09-25
161
SELECT (time '00:00', interval '1 hour')
162
  OVERLAPS (time '01:30', interval '1 day') AS "False";
163

164
CREATE TABLE TEMP_TIMESTAMP (f1 timestamp);
165 166 167

-- get some candidate input values

168 169
INSERT INTO TEMP_TIMESTAMP (f1)
  SELECT d1 FROM TIMESTAMP_TBL
170 171 172
  WHERE d1 BETWEEN '13-jun-1957' AND '1-jan-1997'
   OR d1 BETWEEN '1-jan-1999' AND '1-jan-2010';

173
SELECT '' AS "16", f1 AS timestamp
174 175
  FROM TEMP_TIMESTAMP
  ORDER BY timestamp;
176

177
SELECT '' AS "160", d.f1 AS timestamp, t.f1 AS interval, d.f1 + t.f1 AS plus
178 179
  FROM TEMP_TIMESTAMP d, INTERVAL_TBL t
  ORDER BY plus, timestamp, interval;
180

181
SELECT '' AS "160", d.f1 AS timestamp, t.f1 AS interval, d.f1 - t.f1 AS minus
182
  FROM TEMP_TIMESTAMP d, INTERVAL_TBL t
183
  WHERE isfinite(d.f1)
184
  ORDER BY minus, timestamp, interval;
185

186
SELECT '' AS "16", d.f1 AS timestamp, timestamp '1980-01-06 00:00 GMT' AS gpstime_zero,
187 188
   d.f1 - timestamp '1980-01-06 00:00 GMT' AS difference
  FROM TEMP_TIMESTAMP d
189 190
  ORDER BY difference;

191
SELECT '' AS "226", d1.f1 AS timestamp1, d2.f1 AS timestamp2, d1.f1 - d2.f1 AS difference
192 193
  FROM TEMP_TIMESTAMP d1, TEMP_TIMESTAMP d2
  ORDER BY timestamp1, timestamp2, difference;
194

195
SELECT '' as "55", d1 as timestamp,
196 197 198
  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
199
  FROM TIMESTAMP_TBL
200
  WHERE isfinite(d1) and d1 >= '1-jan-1900 GMT'
201
  ORDER BY timestamp;
202 203 204 205 206

--
-- abstime, reltime arithmetic
--

207
SELECT '' AS ten, ABSTIME_TBL.f1 AS abstime, RELTIME_TBL.f1 AS reltime
208
   WHERE (ABSTIME_TBL.f1 + RELTIME_TBL.f1) < abstime 'Jan 14 14:00:00 1971'
209 210 211 212 213 214 215 216
   ORDER BY abstime, reltime;

-- these four queries should return the same answer
-- the "infinity" and "-infinity" tuples in ABSTIME_TBL cannot be added and
-- therefore, should not show up in the results.

SELECT '' AS three, ABSTIME_TBL.*
  WHERE  (ABSTIME_TBL.f1 + reltime '@ 3 year')         -- +3 years
217
    < abstime 'Jan 14 14:00:00 1977';
218 219 220

SELECT '' AS three, ABSTIME_TBL.*
   WHERE  (ABSTIME_TBL.f1 + reltime '@ 3 year ago')    -- -3 years
221
     < abstime 'Jan 14 14:00:00 1971';
222 223 224

SELECT '' AS three, ABSTIME_TBL.*
   WHERE  (ABSTIME_TBL.f1 - reltime '@ 3 year')        -- -(+3) years
225
    < abstime 'Jan 14 14:00:00 1971';
226 227 228

SELECT '' AS three, ABSTIME_TBL.*
   WHERE  (ABSTIME_TBL.f1 - reltime '@ 3 year ago')    -- -(-3) years
229
     < abstime 'Jan 14 14:00:00 1977';
230 231

--
232
-- Conversions
233 234
--

235
SELECT '' AS "16", f1 AS timestamp, date(f1) AS date
236 237
  FROM TEMP_TIMESTAMP
  WHERE f1 <> timestamp 'current'
238
  ORDER BY date, timestamp;
239

240
SELECT '' AS "16", f1 AS timestamp, abstime(f1) AS abstime
241
  FROM TEMP_TIMESTAMP
242 243
  ORDER BY abstime;

244
SELECT '' AS four, f1 AS abstime, date(f1) AS date
245
  FROM ABSTIME_TBL
246
  WHERE isfinite(f1) AND f1 <> abstime 'current'
247
  ORDER BY date, abstime;
248

249
SELECT '' AS two, d1 AS timestamp, abstime(d1) AS abstime
250
  FROM TIMESTAMP_TBL WHERE NOT isfinite(d1);
251

252
SELECT '' AS three, f1 as abstime, timestamp(f1) AS timestamp
253 254
  FROM ABSTIME_TBL WHERE NOT isfinite(f1);

255
SELECT '' AS ten, f1 AS interval, reltime(f1) AS reltime
256
  FROM INTERVAL_TBL;
257

258
SELECT '' AS six, f1 as reltime, interval(f1) AS interval
259 260
  FROM RELTIME_TBL;

261
DROP TABLE TEMP_TIMESTAMP;
262

263
--
264
-- Formats
265 266 267 268 269 270
--

SET DateStyle TO 'US,Postgres';

SHOW DateStyle;

271
SELECT '' AS "64", d1 AS us_postgres FROM TIMESTAMP_TBL;
272

273
SELECT '' AS seven, f1 AS us_postgres FROM ABSTIME_TBL;
274 275 276

SET DateStyle TO 'US,ISO';

277
SELECT '' AS "64", d1 AS us_iso FROM TIMESTAMP_TBL;
278

279
SELECT '' AS seven, f1 AS us_iso FROM ABSTIME_TBL;
280 281 282 283 284

SET DateStyle TO 'US,SQL';

SHOW DateStyle;

285
SELECT '' AS "64", d1 AS us_sql FROM TIMESTAMP_TBL;
286

287
SELECT '' AS seven, f1 AS us_sql FROM ABSTIME_TBL;
288 289 290 291 292

SET DateStyle TO 'European,Postgres';

SHOW DateStyle;

293
INSERT INTO TIMESTAMP_TBL VALUES('13/06/1957');
294

295
SELECT count(*) as one FROM TIMESTAMP_TBL WHERE d1 = 'Jun 13 1957';
296

297
SELECT '' AS "65", d1 AS european_postgres FROM TIMESTAMP_TBL;
298

299
SELECT '' AS seven, f1 AS european_postgres FROM ABSTIME_TBL;
300 301 302 303 304

SET DateStyle TO 'European,ISO';

SHOW DateStyle;

305
SELECT '' AS "65", d1 AS european_iso FROM TIMESTAMP_TBL;
306

307
SELECT '' AS seven, f1 AS european_iso FROM ABSTIME_TBL;
308 309 310 311 312

SET DateStyle TO 'European,SQL';

SHOW DateStyle;

313
SELECT '' AS "65", d1 AS european_sql FROM TIMESTAMP_TBL;
314

315
SELECT '' AS seven, f1 AS european_sql FROM ABSTIME_TBL;
316 317

RESET DateStyle;