-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
timestamp
487 lines (392 loc) · 15.4 KB
/
timestamp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
query T
SELECT '2000-05-05 10:00:00+03':::TIMESTAMP
----
2000-05-05 10:00:00 +0000 +0000
statement ok
CREATE TABLE a (a int); INSERT INTO a VALUES(1)
# Ensure that timestamp serialization doesn't break even if the computation is
# distributed: #28110.
query T
SELECT '2000-05-05 10:00:00+03':::TIMESTAMP FROM a
----
2000-05-05 10:00:00 +0000 +0000
query T
select '1-1-18 1:00:00.001-8':::TIMESTAMPTZ
----
2001-01-18 09:00:00.001 +0000 UTC
# Test timezone() and ... AT TIME ZONE functions.
subtest timezone
statement ok
SET TIME ZONE 'PST8PDT'
query TT
SELECT TIMESTAMP '2001-02-16 20:38:40' AT TIME ZONE 'MST', timezone('MST', TIMESTAMP '2001-02-16 20:38:40')
----
2001-02-16 19:38:40 -0800 PST 2001-02-16 19:38:40 -0800 PST
query TT
SELECT TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-05' AT TIME ZONE 'MST', timezone('MST', TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-05')
----
2001-02-16 18:38:40 +0000 +0000 2001-02-16 18:38:40 +0000 +0000
query TT
SELECT TIMESTAMP '2001-02-16 20:38:40' AT TIME ZONE 'MST', timezone('MST', TIMESTAMP '2001-02-16 20:38:40')
----
2001-02-16 19:38:40 -0800 PST 2001-02-16 19:38:40 -0800 PST
# Test timestamp precisions
subtest timestamp_precision
query error precision 7 out of range
select '1-1-18 1:00:00.001':::TIMESTAMP(7)
query error precision 7 out of range
select '1-1-18 1:00:00.001':::TIMESTAMPTZ(7)
query T
select '1-1-18 1:00:00.001':::TIMESTAMP(0)
----
2001-01-18 01:00:00 +0000 +0000
query T
select '1-1-18 1:00:00.001':::TIMESTAMP(6)
----
2001-01-18 01:00:00.001 +0000 +0000
query T
select '1-1-18 1:00:00.001':::TIMESTAMP
----
2001-01-18 01:00:00.001 +0000 +0000
query T
select '1-1-18 1:00:00.001-8':::TIMESTAMPTZ(0)
----
2001-01-18 01:00:00 -0800 PST
query T
select '1-1-18 1:00:00.001-8':::TIMESTAMPTZ(6)
----
2001-01-18 01:00:00.001 -0800 PST
query B
select current_timestamp(3) - current_timestamp <= '1ms'::interval
----
true
statement ok
CREATE TABLE timestamp_test (
id integer PRIMARY KEY,
t TIMESTAMP(5),
ttz TIMESTAMPTZ(4)
)
statement ok
INSERT INTO timestamp_test VALUES
(1, '2001-01-01 12:00:00.123456', '2001-01-01 12:00:00.123456+4'),
(2, '2001-01-01 12:00:00.12345', '2001-01-01 12:00:00.12345+4'),
(3, '2001-01-01 12:00:00.1234', '2001-01-01 12:00:00.1234+4'),
(4, '2001-01-01 12:00:00.123', '2001-01-01 12:00:00.123+4'),
(5, '2001-01-01 12:00:00.12', '2001-01-01 12:00:00.12+4'),
(6, '2001-01-01 12:00:00.1', '2001-01-01 12:00:00.1+4'),
(7, '2001-01-01 12:00:00', '2001-01-01 12:00:00+4')
query ITT
SELECT * FROM timestamp_test ORDER BY id ASC
----
1 2001-01-01 12:00:00.12346 +0000 +0000 2001-01-01 00:00:00.1235 -0800 PST
2 2001-01-01 12:00:00.12345 +0000 +0000 2001-01-01 00:00:00.1235 -0800 PST
3 2001-01-01 12:00:00.1234 +0000 +0000 2001-01-01 00:00:00.1234 -0800 PST
4 2001-01-01 12:00:00.123 +0000 +0000 2001-01-01 00:00:00.123 -0800 PST
5 2001-01-01 12:00:00.12 +0000 +0000 2001-01-01 00:00:00.12 -0800 PST
6 2001-01-01 12:00:00.1 +0000 +0000 2001-01-01 00:00:00.1 -0800 PST
7 2001-01-01 12:00:00 +0000 +0000 2001-01-01 00:00:00 -0800 PST
query TT
select column_name, data_type FROM [SHOW COLUMNS FROM timestamp_test] ORDER BY column_name
----
id INT8
t TIMESTAMP(5)
ttz TIMESTAMPTZ(4)
query ITTTT
SELECT id, t::timestamp(0), t::timestamp(3), ttz::timestamptz(0), ttz::timestamptz(3) FROM timestamp_test ORDER BY id
----
1 2001-01-01 12:00:00 +0000 +0000 2001-01-01 12:00:00.123 +0000 +0000 2001-01-01 00:00:00 -0800 PST 2001-01-01 00:00:00.124 -0800 PST
2 2001-01-01 12:00:00 +0000 +0000 2001-01-01 12:00:00.123 +0000 +0000 2001-01-01 00:00:00 -0800 PST 2001-01-01 00:00:00.124 -0800 PST
3 2001-01-01 12:00:00 +0000 +0000 2001-01-01 12:00:00.123 +0000 +0000 2001-01-01 00:00:00 -0800 PST 2001-01-01 00:00:00.123 -0800 PST
4 2001-01-01 12:00:00 +0000 +0000 2001-01-01 12:00:00.123 +0000 +0000 2001-01-01 00:00:00 -0800 PST 2001-01-01 00:00:00.123 -0800 PST
5 2001-01-01 12:00:00 +0000 +0000 2001-01-01 12:00:00.12 +0000 +0000 2001-01-01 00:00:00 -0800 PST 2001-01-01 00:00:00.12 -0800 PST
6 2001-01-01 12:00:00 +0000 +0000 2001-01-01 12:00:00.1 +0000 +0000 2001-01-01 00:00:00 -0800 PST 2001-01-01 00:00:00.1 -0800 PST
7 2001-01-01 12:00:00 +0000 +0000 2001-01-01 12:00:00 +0000 +0000 2001-01-01 00:00:00 -0800 PST 2001-01-01 00:00:00 -0800 PST
# Altering type to more units of precision should work.
statement ok
ALTER TABLE timestamp_test ALTER COLUMN t TYPE timestamp
statement ok
ALTER TABLE timestamp_test ALTER COLUMN ttz TYPE timestamptz(5)
statement ok
INSERT INTO timestamp_test VALUES
(100, '2001-01-01 12:00:00.123456', '2001-01-01 12:00:00.123456+4')
query ITT
SELECT * FROM timestamp_test ORDER BY id ASC
----
1 2001-01-01 12:00:00.12346 +0000 +0000 2001-01-01 00:00:00.1235 -0800 PST
2 2001-01-01 12:00:00.12345 +0000 +0000 2001-01-01 00:00:00.1235 -0800 PST
3 2001-01-01 12:00:00.1234 +0000 +0000 2001-01-01 00:00:00.1234 -0800 PST
4 2001-01-01 12:00:00.123 +0000 +0000 2001-01-01 00:00:00.123 -0800 PST
5 2001-01-01 12:00:00.12 +0000 +0000 2001-01-01 00:00:00.12 -0800 PST
6 2001-01-01 12:00:00.1 +0000 +0000 2001-01-01 00:00:00.1 -0800 PST
7 2001-01-01 12:00:00 +0000 +0000 2001-01-01 00:00:00 -0800 PST
100 2001-01-01 12:00:00.123456 +0000 +0000 2001-01-01 00:00:00.12346 -0800 PST
query TT
select column_name, data_type FROM [SHOW COLUMNS FROM timestamp_test] ORDER BY column_name
----
id INT8
t TIMESTAMP
ttz TIMESTAMPTZ(5)
subtest regression_timestamp_comparison
statement ok
SET TIME ZONE -5
query B
SELECT '2001-01-01'::date = '2001-01-01 00:00:00'::timestamp
----
true
query B
SELECT '2001-01-01'::date = '2001-01-01 00:00:00-5'::timestamptz
----
true
query B
SELECT '2001-01-01 00:00:00'::timestamp = '2001-01-01 01:00:00-4'::timestamptz
----
true
subtest regression_django-cockroachdb_47
statement ok
SET TIME ZONE -3
query R
SELECT extract(hour FROM '2001-01-01 13:00:00+01'::timestamptz)
----
9
query R
SELECT extract(hour FROM '2001-01-01 13:00:00'::timestamp)
----
13
query R
SELECT extract(timezone FROM '2001-01-01 13:00:00+01:15'::timestamptz)
----
-10800
statement ok
SET TIME ZONE +3
query R
SELECT extract(hour FROM '2001-01-01 13:00:00+01'::timestamptz)
----
15
query R
SELECT extract(hour FROM '2001-01-01 13:00:00'::timestamp)
----
13
query R
SELECT extract(timezone FROM '2001-01-01 13:00:00+01:15'::timestamptz)
----
10800
subtest regression_41776
statement ok
SET TIME ZONE 'GMT+1'
query T
SELECT '2001-01-01 00:00:00'::TIMESTAMP::TIMESTAMPTZ
----
2001-01-01 00:00:00 -0100 -0100
statement ok
SET TIME ZONE '+1:00'
query T
SELECT '2001-01-01 00:00:00'::TIMESTAMP::TIMESTAMPTZ
----
2001-01-01 00:00:00 -0100 -0100
# test that current_timestamp is correct in different timezones.
subtest current_timestamp_correct_in_timezone
statement ok
set time zone +3
statement ok
create table current_timestamp_test (a timestamp, b timestamptz)
statement ok
insert into current_timestamp_test values (current_timestamp, current_timestamp)
statement ok
set time zone 0
# a was written at an interval 3 hours ahead, and should persist that way.
# b will remember the timezone, so should be "constant" for comparison's sake.
query TT
select * from current_timestamp_test WHERE a - interval '3h' <> b
----
subtest localtimestamp_test
query TTTT
select pg_typeof(localtimestamp), pg_typeof(current_timestamp), pg_typeof(localtimestamp(3)), pg_typeof(current_timestamp(3))
----
timestamp without time zone timestamp with time zone timestamp without time zone timestamp with time zone
query B
select localtimestamp(3) - localtimestamp <= '1ms'::interval
----
true
# When doing daylight savings comparisons, ensure they compare correctly.
# Test day before and after DST.
subtest regression_django-cockroachdb_120
statement ok
SET TIME ZONE 'America/Chicago'
query T
SELECT '1882-05-23T00:00:00-05:51'::timestamptz::text
----
1882-05-23 00:00:24-05:50:24
query B
SELECT '2011-03-13'::date = '2011-03-13'::timestamp
----
true
query B
SELECT '2011-03-13'::date = '2011-03-13'::timestamptz
----
true
query B
SELECT '2011-03-13'::timestamp = '2011-03-13'::timestamptz
----
true
query B
SELECT '2011-03-14'::date = '2011-03-14'::timestamp
----
true
query B
SELECT '2011-03-14'::date = '2011-03-14'::timestamptz
----
true
query B
SELECT '2011-03-14'::timestamp = '2011-03-14'::timestamptz
----
true
statement ok
SET TIME ZONE '-00:10:15'
query T
SELECT '1882-05-23T00:00:00-05:51'::timestamptz::text
----
1882-05-23 06:01:15+00:10:15
statement ok
SET TIME ZONE 0
# Check default types and expressions get truncated on insert / update.
subtest regression_44774
statement ok
CREATE TABLE regression_44774 (
a timestamp(3) DEFAULT '1970-02-03 12:13:14.123456',
b timestamptz(3) DEFAULT '1970-02-03 12:13:14.123456'
)
statement ok
INSERT INTO regression_44774 VALUES (default, default), ('2020-02-05 19:21:57.261286', '2020-02-05 19:21:57.261286')
query TT
SELECT a, b FROM regression_44774 ORDER BY a
----
1970-02-03 12:13:14.123 +0000 +0000 1970-02-03 12:13:14.123 +0000 +0000
2020-02-05 19:21:57.261 +0000 +0000 2020-02-05 19:21:57.261 +0000 +0000
statement ok
UPDATE regression_44774
SET a = '1970-03-04 13:14:15.123456'::timestamp + '1 sec'::interval, b = '1970-03-04 13:14:15.123456'::timestamptz + '1 sec'::interval
WHERE 1 = 1
query TT
SELECT a, b FROM regression_44774 ORDER BY a
----
1970-03-04 13:14:16.123 +0000 +0000 1970-03-04 13:14:16.123 +0000 +0000
1970-03-04 13:14:16.123 +0000 +0000 1970-03-04 13:14:16.123 +0000 +0000
statement ok
DROP TABLE regression_44774
# Test for timestamptz math with interval involving DST.
subtest regression-cockroachdb/django-cockroachdb_57
statement ok
SET TIME ZONE 'America/Chicago'
query T
WITH a(a) AS ( VALUES
('2010-11-06 23:59:00'::timestamptz + '24 hours'::interval), -- no offset specified
('2010-11-06 23:59:00'::timestamptz + '1 day'::interval),
('2010-11-06 23:59:00'::timestamptz + '1 month'::interval),
('2010-11-07 23:59:00'::timestamptz - '24 hours'::interval),
('2010-11-07 23:59:00'::timestamptz - '1 day'::interval),
('2010-11-07 23:59:00'::timestamptz - '1 month'::interval),
('2010-11-06 23:59:00-05'::timestamptz + '24 hours'::interval), -- offset at time zone
('2010-11-06 23:59:00-05'::timestamptz + '1 day'::interval),
('2010-11-06 23:59:00-05'::timestamptz + '1 month'::interval),
('2010-11-07 23:59:00-06'::timestamptz - '24 hours'::interval),
('2010-11-07 23:59:00-06'::timestamptz - '1 day'::interval),
('2010-11-07 23:59:00-06'::timestamptz - '1 month'::interval),
('2010-11-06 23:59:00-04'::timestamptz + '24 hours'::interval), -- different offset
('2010-11-06 23:59:00-04'::timestamptz + '1 day'::interval),
('2010-11-06 23:59:00-04'::timestamptz + '1 month'::interval),
('2010-11-07 23:59:00-04'::timestamptz - '24 hours'::interval),
('2010-11-07 23:59:00-04'::timestamptz - '1 day'::interval),
('2010-11-07 23:59:00-04'::timestamptz - '1 month'::interval)
) select * from a;
----
2010-11-07 22:59:00 -0600 CST
2010-11-07 23:59:00 -0600 CST
2010-12-06 23:59:00 -0600 CST
2010-11-07 00:59:00 -0500 CDT
2010-11-06 23:59:00 -0500 CDT
2010-10-07 23:59:00 -0500 CDT
2010-11-07 22:59:00 -0600 CST
2010-11-07 23:59:00 -0600 CST
2010-12-06 23:59:00 -0600 CST
2010-11-07 00:59:00 -0500 CDT
2010-11-06 23:59:00 -0500 CDT
2010-10-07 23:59:00 -0500 CDT
2010-11-07 21:59:00 -0600 CST
2010-11-07 22:59:00 -0600 CST
2010-12-06 22:59:00 -0600 CST
2010-11-06 22:59:00 -0500 CDT
2010-11-06 21:59:00 -0500 CDT
2010-10-07 21:59:00 -0500 CDT
statement ok
CREATE TABLE example (a timestamptz)
statement ok
INSERT INTO example VALUES
('2010-11-06 23:59:00'),
('2010-11-07 23:59:00')
query TTTTTTTTT
SELECT
a + '24 hours'::interval, a + '1 day'::interval, a + '1 month'::interval,
a - '24 hours'::interval, a - '1 day'::interval, a - '1 month'::interval,
a - '2010-11-06 23:59:00'::timestamptz,
a - '2010-11-07 23:59:00'::timestamptz,
a::string
FROM example
ORDER BY a
----
2010-11-07 22:59:00 -0600 CST 2010-11-07 23:59:00 -0600 CST 2010-12-06 23:59:00 -0600 CST 2010-11-05 23:59:00 -0500 CDT 2010-11-05 23:59:00 -0500 CDT 2010-10-06 23:59:00 -0500 CDT 00:00:00 -1 days -01:00:00 2010-11-06 23:59:00-05:00
2010-11-08 23:59:00 -0600 CST 2010-11-08 23:59:00 -0600 CST 2010-12-07 23:59:00 -0600 CST 2010-11-07 00:59:00 -0500 CDT 2010-11-06 23:59:00 -0500 CDT 2010-10-07 23:59:00 -0500 CDT 1 day 01:00:00 00:00:00 2010-11-07 23:59:00-06:00
statement ok
DROP TABLE example
statement ok
SET TIME ZONE 0
subtest regression_46973
statement ok
CREATE TABLE regression_46973(c0 TIMESTAMP UNIQUE, c1 TIMESTAMPTZ UNIQUE)
statement ok
INSERT INTO regression_46973 VALUES ('1970-01-01 00:00:00', '1970-01-01 00:00:00')
statement error "292277026596-12-04T15:30:08Z" exceeds supported timestamp bounds
SELECT * FROM regression_46973 WHERE (-9223372036854775808)::TIMESTAMP!=regression_46973.c0
statement error "292277026596-12-04T15:30:08Z" exceeds supported timestamp bounds
SELECT * FROM regression_46973 WHERE (-9223372036854775808)::TIMESTAMPTZ!=regression_46973.c1
statement error "294277-01-01T00:00:00Z" exceeds supported timestamp bounds
SELECT '294276-12-31 23:59:59.999999'::TIMESTAMP(0)
statement ok
DROP TABLE regression_46973
subtest regression_extract_epoch_timestamptz
query R
set time zone 'Europe/Berlin'; select extract(epoch from TIMESTAMP WITH TIME ZONE '2010-11-06 23:59:00-05:00')
----
1.28910594e+09
query R
set time zone 'UTC'; select extract(epoch from TIMESTAMP WITH TIME ZONE '2010-11-06 23:59:00-05:00')
----
1.28910594e+09
query TTTTTT
SET TIME ZONE 'Europe/Berlin'; SELECT
age(a::timestamptz, b::timestamptz),
age(b::timestamptz, a::timestamptz),
a::timestamptz - b::timestamptz,
b::timestamptz - a::timestamptz,
a::timestamp - b::timestamp,
b::timestamp - a::timestamp
FROM (VALUES
('2020-05-06 11:12:13', '2015-06-07 13:14:15'),
('2020-05-06 15:00:00', '2020-04-03 16:00:00'),
('2020-02-29 00:02:05', '2019-02-28 18:19:01'),
('2020-02-29 00:02:05', '2020-01-28 18:19:01'),
('2020-02-29 00:02:05', '2020-03-28 18:19:01'),
('2021-02-27 00:02:05', '2019-02-28 18:19:01'),
('2021-02-27 00:02:05', '2021-01-28 18:19:01'),
('2021-02-27 00:02:05', '2021-03-28 18:19:01'),
('2020-02-28 00:02:05', '2020-02-28 18:19:01')
) regression_age_tests(a, b)
----
4 years 10 mons 28 days 21:57:58 -4 years -10 mons -28 days -21:57:58 1794 days 21:57:58 -1794 days -21:57:58 1794 days 21:57:58 -1794 days -21:57:58
1 mon 2 days 23:00:00 -1 mons -2 days -23:00:00 32 days 23:00:00 -32 days -23:00:00 32 days 23:00:00 -32 days -23:00:00
1 year 05:43:04 -1 years -05:43:04 365 days 05:43:04 -365 days -05:43:04 365 days 05:43:04 -365 days -05:43:04
1 mon 05:43:04 -1 mons -05:43:04 31 days 05:43:04 -31 days -05:43:04 31 days 05:43:04 -31 days -05:43:04
-28 days -18:16:56 28 days 18:16:56 -28 days -18:16:56 28 days 18:16:56 -28 days -18:16:56 28 days 18:16:56
1 year 11 mons 26 days 05:43:04 -1 years -11 mons -26 days -05:43:04 729 days 05:43:04 -729 days -05:43:04 729 days 05:43:04 -729 days -05:43:04
29 days 05:43:04 -29 days -05:43:04 29 days 05:43:04 -29 days -05:43:04 29 days 05:43:04 -29 days -05:43:04
-1 mons -1 days -17:16:56 1 mon 1 day 17:16:56 -29 days -17:16:56 29 days 17:16:56 -29 days -18:16:56 29 days 18:16:56
-18:16:56 18:16:56 -18:16:56 18:16:56 -18:16:56 18:16:56