timestamptz.sql 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. --
  2. -- TIMESTAMPTZ
  3. --
  4. CREATE TABLE TIMESTAMPTZ_TBL (d1 timestamp(2) with time zone);
  5. -- Test shorthand input values
  6. -- We can't just "select" the results since they aren't constants; test for
  7. -- equality instead. We can do that by running the test inside a transaction
  8. -- block, within which the value of 'now' shouldn't change, and so these
  9. -- related values shouldn't either.
  10. BEGIN;
  11. INSERT INTO TIMESTAMPTZ_TBL VALUES ('today');
  12. INSERT INTO TIMESTAMPTZ_TBL VALUES ('yesterday');
  13. INSERT INTO TIMESTAMPTZ_TBL VALUES ('tomorrow');
  14. SELECT count(*) AS One FROM TIMESTAMPTZ_TBL WHERE d1 = timestamp with time zone 'today';
  15. SELECT count(*) AS One FROM TIMESTAMPTZ_TBL WHERE d1 = timestamp with time zone 'tomorrow';
  16. SELECT count(*) AS One FROM TIMESTAMPTZ_TBL WHERE d1 = timestamp with time zone 'yesterday';
  17. COMMIT;
  18. -- Verify that 'now' *does* change over a reasonable interval such as 100 msec,
  19. -- and that it doesn't change over the same interval within a transaction block
  20. INSERT INTO TIMESTAMPTZ_TBL VALUES ('now');
  21. SELECT pg_sleep(0.1);
  22. BEGIN;
  23. INSERT INTO TIMESTAMPTZ_TBL VALUES ('now');
  24. SELECT pg_sleep(0.1);
  25. INSERT INTO TIMESTAMPTZ_TBL VALUES ('now');
  26. SELECT pg_sleep(0.1);
  27. COMMIT;
  28. -- Special values
  29. INSERT INTO TIMESTAMPTZ_TBL VALUES ('-infinity');
  30. INSERT INTO TIMESTAMPTZ_TBL VALUES ('infinity');
  31. INSERT INTO TIMESTAMPTZ_TBL VALUES ('epoch');
  32. -- ISO 8601 format
  33. INSERT INTO TIMESTAMPTZ_TBL VALUES ('1997-01-02');
  34. INSERT INTO TIMESTAMPTZ_TBL VALUES ('1997-01-02 03:04:05');
  35. INSERT INTO TIMESTAMPTZ_TBL VALUES ('1997-02-10 17:32:01-08');
  36. INSERT INTO TIMESTAMPTZ_TBL VALUES ('1997-02-10 17:32:01-0800');
  37. INSERT INTO TIMESTAMPTZ_TBL VALUES ('1997-02-10 17:32:01 -08:00');
  38. INSERT INTO TIMESTAMPTZ_TBL VALUES ('19970210 173201 -0800');
  39. INSERT INTO TIMESTAMPTZ_TBL VALUES ('1997-06-10 17:32:01 -07:00');
  40. INSERT INTO TIMESTAMPTZ_TBL VALUES ('2001-09-22T18:19:20');
  41. -- POSIX format (note that the timezone abbrev is just decoration here)
  42. INSERT INTO TIMESTAMPTZ_TBL VALUES ('2000-03-15 08:14:01 GMT+8');
  43. INSERT INTO TIMESTAMPTZ_TBL VALUES ('2000-03-15 13:14:02 GMT-1');
  44. INSERT INTO TIMESTAMPTZ_TBL VALUES ('2000-03-15 12:14:03 GMT-2');
  45. INSERT INTO TIMESTAMPTZ_TBL VALUES ('2000-03-15 03:14:04 PST+8');
  46. INSERT INTO TIMESTAMPTZ_TBL VALUES ('2000-03-15 02:14:05 MST+7:00');
  47. -- Variations for acceptable input formats
  48. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 10 17:32:01 1997 -0800');
  49. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 10 17:32:01 1997');
  50. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 10 5:32PM 1997');
  51. INSERT INTO TIMESTAMPTZ_TBL VALUES ('1997/02/10 17:32:01-0800');
  52. set datestyle to ymd;
  53. reset datestyle;
  54. INSERT INTO TIMESTAMPTZ_TBL VALUES ('19970710 173201 America/Does_not_exist');
  55. SELECT '19970710 173201' AT TIME ZONE 'America/Does_not_exist';
  56. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 10 17:32:01 1997');
  57. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 11 17:32:01 1997');
  58. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 12 17:32:01 1997');
  59. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 13 17:32:01 1997');
  60. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 14 17:32:01 1997');
  61. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 15 17:32:01 1997');
  62. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 1997');
  63. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 0097 BC');
  64. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 0097');
  65. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 0597');
  66. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 1097');
  67. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 1697');
  68. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 1797');
  69. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 1897');
  70. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 1997');
  71. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 2097');
  72. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 28 17:32:01 1996');
  73. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 29 17:32:01 1996');
  74. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Mar 01 17:32:01 1996');
  75. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Dec 30 17:32:01 1996');
  76. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Dec 31 17:32:01 1996');
  77. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Jan 01 17:32:01 1997');
  78. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 28 17:32:01 1997');
  79. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 29 17:32:01 1997');
  80. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Mar 01 17:32:01 1997');
  81. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Dec 30 17:32:01 1997');
  82. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Dec 31 17:32:01 1997');
  83. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Dec 31 17:32:01 1999');
  84. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Jan 01 17:32:01 2000');
  85. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Dec 31 17:32:01 2000');
  86. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Jan 01 17:32:01 2001');
  87. -- Currently unsupported syntax and ranges
  88. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 -0097');
  89. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 5097 BC');
  90. SELECT '4714-11-23 23:59:59+00 BC'::timestamptz; -- out of range
  91. SELECT '294277-01-01 00:00:00+00'::timestamptz; -- out of range
  92. SELECT '294277-12-31 16:00:00-08'::timestamptz; -- out of range
  93. -- disallow intervals with months or years
  94. SELECT date_bin('5 months'::interval, timestamp with time zone '2020-02-01 01:01:01+00', timestamp with time zone '2001-01-01+00');
  95. SELECT date_bin('5 years'::interval, timestamp with time zone '2020-02-01 01:01:01+00', timestamp with time zone '2001-01-01+00');
  96. -- disallow zero intervals
  97. SELECT date_bin('0 days'::interval, timestamp with time zone '1970-01-01 01:00:00+00' , timestamp with time zone '1970-01-01 00:00:00+00');
  98. -- disallow negative intervals
  99. SELECT date_bin('-2 days'::interval, timestamp with time zone '1970-01-01 01:00:00+00' , timestamp with time zone '1970-01-01 00:00:00+00');
  100. -- value near upper bound uses special case in code
  101. SELECT date_part('epoch', '294270-01-01 00:00:00+00'::timestamptz);
  102. SELECT extract(epoch from '294270-01-01 00:00:00+00'::timestamptz);
  103. -- another internal overflow test case
  104. SELECT extract(epoch from '5000-01-01 00:00:00+00'::timestamptz);
  105. SELECT to_char(d, 'FF1 FF2 FF3 FF4 FF5 FF6 ff1 ff2 ff3 ff4 ff5 ff6 MS US')
  106. FROM (VALUES
  107. ('2018-11-02 12:34:56'::timestamptz),
  108. ('2018-11-02 12:34:56.78'),
  109. ('2018-11-02 12:34:56.78901'),
  110. ('2018-11-02 12:34:56.78901234')
  111. ) d(d);
  112. -- Check OF, TZH, TZM with various zone offsets, particularly fractional hours
  113. SET timezone = '00:00';
  114. SELECT to_char(now(), 'OF') as "OF", to_char(now(), 'TZH:TZM') as "TZH:TZM";
  115. SET timezone = '+02:00';
  116. SET timezone = '-13:00';
  117. SET timezone = '-00:30';
  118. SET timezone = '00:30';
  119. SET timezone = '-04:30';
  120. SET timezone = '04:30';
  121. SET timezone = '-04:15';
  122. SET timezone = '04:15';
  123. RESET timezone;
  124. CREATE TABLE TIMESTAMPTZ_TST (a int , b timestamptz);
  125. --Cleanup
  126. DROP TABLE TIMESTAMPTZ_TST;
  127. -- test timestamptz constructors
  128. set TimeZone to 'America/New_York';
  129. -- these should fail
  130. SELECT make_timestamptz(1973, 07, 15, 08, 15, 55.33, '2');
  131. SELECT make_timestamptz(2014, 12, 10, 10, 10, 10, '+16');
  132. SELECT make_timestamptz(2014, 12, 10, 10, 10, 10, '-16');
  133. -- should be true
  134. SELECT make_timestamptz(1973, 07, 15, 08, 15, 55.33, '+2') = '1973-07-15 08:15:55.33+02'::timestamptz;
  135. SELECT make_timestamptz(1910, 12, 24, 0, 0, 0, 'Nehwon/Lankhmar');
  136. RESET TimeZone;
  137. -- errors
  138. select * from generate_series('2020-01-01 00:00'::timestamptz,
  139. '2020-01-02 03:00'::timestamptz,
  140. '0 hour'::interval);
  141. --
  142. -- Test behavior with a dynamic (time-varying) timezone abbreviation.
  143. -- These tests rely on the knowledge that MSK (Europe/Moscow standard time)
  144. -- moved forwards in Mar 2011 and backwards again in Oct 2014.
  145. --
  146. SET TimeZone to 'UTC';
  147. -- upper limit varies between integer and float timestamps, so hard to test
  148. -- nonfinite values
  149. SELECT to_timestamp(' Infinity'::float);
  150. SELECT to_timestamp('-Infinity'::float);
  151. SELECT to_timestamp('NaN'::float);
  152. SET TimeZone to 'Europe/Moscow';
  153. RESET TimeZone;
  154. --
  155. -- Test that AT TIME ZONE isn't misoptimized when using an index (bug #14504)
  156. --
  157. create temp table tmptz (f1 timestamptz primary key);
  158. insert into tmptz values ('2017-01-18 00:00+00');