timestamptz.out 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. one
  16. -----
  17. 1
  18. (1 row)
  19. SELECT count(*) AS One FROM TIMESTAMPTZ_TBL WHERE d1 = timestamp with time zone 'tomorrow';
  20. one
  21. -----
  22. 1
  23. (1 row)
  24. SELECT count(*) AS One FROM TIMESTAMPTZ_TBL WHERE d1 = timestamp with time zone 'yesterday';
  25. one
  26. -----
  27. 1
  28. (1 row)
  29. COMMIT;
  30. -- Verify that 'now' *does* change over a reasonable interval such as 100 msec,
  31. -- and that it doesn't change over the same interval within a transaction block
  32. INSERT INTO TIMESTAMPTZ_TBL VALUES ('now');
  33. SELECT pg_sleep(0.1);
  34. pg_sleep
  35. ----------
  36. (1 row)
  37. BEGIN;
  38. INSERT INTO TIMESTAMPTZ_TBL VALUES ('now');
  39. SELECT pg_sleep(0.1);
  40. pg_sleep
  41. ----------
  42. (1 row)
  43. INSERT INTO TIMESTAMPTZ_TBL VALUES ('now');
  44. SELECT pg_sleep(0.1);
  45. pg_sleep
  46. ----------
  47. (1 row)
  48. COMMIT;
  49. -- Special values
  50. INSERT INTO TIMESTAMPTZ_TBL VALUES ('-infinity');
  51. INSERT INTO TIMESTAMPTZ_TBL VALUES ('infinity');
  52. INSERT INTO TIMESTAMPTZ_TBL VALUES ('epoch');
  53. -- ISO 8601 format
  54. INSERT INTO TIMESTAMPTZ_TBL VALUES ('1997-01-02');
  55. INSERT INTO TIMESTAMPTZ_TBL VALUES ('1997-01-02 03:04:05');
  56. INSERT INTO TIMESTAMPTZ_TBL VALUES ('1997-02-10 17:32:01-08');
  57. INSERT INTO TIMESTAMPTZ_TBL VALUES ('1997-02-10 17:32:01-0800');
  58. INSERT INTO TIMESTAMPTZ_TBL VALUES ('1997-02-10 17:32:01 -08:00');
  59. INSERT INTO TIMESTAMPTZ_TBL VALUES ('19970210 173201 -0800');
  60. INSERT INTO TIMESTAMPTZ_TBL VALUES ('1997-06-10 17:32:01 -07:00');
  61. INSERT INTO TIMESTAMPTZ_TBL VALUES ('2001-09-22T18:19:20');
  62. -- POSIX format (note that the timezone abbrev is just decoration here)
  63. INSERT INTO TIMESTAMPTZ_TBL VALUES ('2000-03-15 08:14:01 GMT+8');
  64. INSERT INTO TIMESTAMPTZ_TBL VALUES ('2000-03-15 13:14:02 GMT-1');
  65. INSERT INTO TIMESTAMPTZ_TBL VALUES ('2000-03-15 12:14:03 GMT-2');
  66. INSERT INTO TIMESTAMPTZ_TBL VALUES ('2000-03-15 03:14:04 PST+8');
  67. INSERT INTO TIMESTAMPTZ_TBL VALUES ('2000-03-15 02:14:05 MST+7:00');
  68. -- Variations for acceptable input formats
  69. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 10 17:32:01 1997 -0800');
  70. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 10 17:32:01 1997');
  71. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 10 5:32PM 1997');
  72. INSERT INTO TIMESTAMPTZ_TBL VALUES ('1997/02/10 17:32:01-0800');
  73. set datestyle to ymd;
  74. reset datestyle;
  75. INSERT INTO TIMESTAMPTZ_TBL VALUES ('19970710 173201 America/Does_not_exist');
  76. ERROR: time zone "america/does_not_exist" not recognized
  77. LINE 1: INSERT INTO TIMESTAMPTZ_TBL VALUES ('19970710 173201 America...
  78. ^
  79. SELECT '19970710 173201' AT TIME ZONE 'America/Does_not_exist';
  80. ERROR: time zone "America/Does_not_exist" not recognized
  81. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 10 17:32:01 1997');
  82. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 11 17:32:01 1997');
  83. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 12 17:32:01 1997');
  84. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 13 17:32:01 1997');
  85. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 14 17:32:01 1997');
  86. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 15 17:32:01 1997');
  87. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 1997');
  88. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 0097 BC');
  89. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 0097');
  90. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 0597');
  91. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 1097');
  92. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 1697');
  93. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 1797');
  94. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 1897');
  95. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 1997');
  96. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 2097');
  97. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 28 17:32:01 1996');
  98. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 29 17:32:01 1996');
  99. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Mar 01 17:32:01 1996');
  100. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Dec 30 17:32:01 1996');
  101. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Dec 31 17:32:01 1996');
  102. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Jan 01 17:32:01 1997');
  103. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 28 17:32:01 1997');
  104. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 29 17:32:01 1997');
  105. ERROR: date/time field value out of range: "Feb 29 17:32:01 1997"
  106. LINE 1: INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 29 17:32:01 1997');
  107. ^
  108. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Mar 01 17:32:01 1997');
  109. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Dec 30 17:32:01 1997');
  110. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Dec 31 17:32:01 1997');
  111. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Dec 31 17:32:01 1999');
  112. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Jan 01 17:32:01 2000');
  113. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Dec 31 17:32:01 2000');
  114. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Jan 01 17:32:01 2001');
  115. -- Currently unsupported syntax and ranges
  116. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 -0097');
  117. ERROR: time zone displacement out of range: "Feb 16 17:32:01 -0097"
  118. LINE 1: INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 -0097')...
  119. ^
  120. INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 5097 BC');
  121. ERROR: timestamp out of range: "Feb 16 17:32:01 5097 BC"
  122. LINE 1: INSERT INTO TIMESTAMPTZ_TBL VALUES ('Feb 16 17:32:01 5097 BC...
  123. ^
  124. SELECT '4714-11-23 23:59:59+00 BC'::timestamptz; -- out of range
  125. ERROR: timestamp out of range: "4714-11-23 23:59:59+00 BC"
  126. LINE 1: SELECT '4714-11-23 23:59:59+00 BC'::timestamptz;
  127. ^
  128. SELECT '294277-01-01 00:00:00+00'::timestamptz; -- out of range
  129. ERROR: timestamp out of range: "294277-01-01 00:00:00+00"
  130. LINE 1: SELECT '294277-01-01 00:00:00+00'::timestamptz;
  131. ^
  132. SELECT '294277-12-31 16:00:00-08'::timestamptz; -- out of range
  133. ERROR: timestamp out of range: "294277-12-31 16:00:00-08"
  134. LINE 1: SELECT '294277-12-31 16:00:00-08'::timestamptz;
  135. ^
  136. -- disallow intervals with months or years
  137. 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');
  138. ERROR: timestamps cannot be binned into intervals containing months or years
  139. 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');
  140. ERROR: timestamps cannot be binned into intervals containing months or years
  141. -- disallow zero intervals
  142. 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');
  143. ERROR: stride must be greater than zero
  144. -- disallow negative intervals
  145. 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');
  146. ERROR: stride must be greater than zero
  147. -- value near upper bound uses special case in code
  148. SELECT date_part('epoch', '294270-01-01 00:00:00+00'::timestamptz);
  149. date_part
  150. ---------------
  151. 9224097091200
  152. (1 row)
  153. SELECT extract(epoch from '294270-01-01 00:00:00+00'::timestamptz);
  154. extract
  155. ----------------------
  156. 9224097091200.000000
  157. (1 row)
  158. -- another internal overflow test case
  159. SELECT extract(epoch from '5000-01-01 00:00:00+00'::timestamptz);
  160. extract
  161. --------------------
  162. 95617584000.000000
  163. (1 row)
  164. SELECT to_char(d, 'FF1 FF2 FF3 FF4 FF5 FF6 ff1 ff2 ff3 ff4 ff5 ff6 MS US')
  165. FROM (VALUES
  166. ('2018-11-02 12:34:56'::timestamptz),
  167. ('2018-11-02 12:34:56.78'),
  168. ('2018-11-02 12:34:56.78901'),
  169. ('2018-11-02 12:34:56.78901234')
  170. ) d(d);
  171. to_char
  172. --------------------------------------------------------------------
  173. 0 00 000 0000 00000 000000 0 00 000 0000 00000 000000 000 000000
  174. 7 78 780 7800 78000 780000 7 78 780 7800 78000 780000 780 780000
  175. 7 78 789 7890 78901 789010 7 78 789 7890 78901 789010 789 789010
  176. 7 78 789 7890 78901 789012 7 78 789 7890 78901 789012 789 789012
  177. (4 rows)
  178. -- Check OF, TZH, TZM with various zone offsets, particularly fractional hours
  179. SET timezone = '00:00';
  180. SELECT to_char(now(), 'OF') as "OF", to_char(now(), 'TZH:TZM') as "TZH:TZM";
  181. OF | TZH:TZM
  182. -----+---------
  183. +00 | +00:00
  184. (1 row)
  185. SET timezone = '+02:00';
  186. SET timezone = '-13:00';
  187. SET timezone = '-00:30';
  188. SET timezone = '00:30';
  189. SET timezone = '-04:30';
  190. SET timezone = '04:30';
  191. SET timezone = '-04:15';
  192. SET timezone = '04:15';
  193. RESET timezone;
  194. CREATE TABLE TIMESTAMPTZ_TST (a int , b timestamptz);
  195. --Cleanup
  196. DROP TABLE TIMESTAMPTZ_TST;
  197. -- test timestamptz constructors
  198. set TimeZone to 'America/New_York';
  199. -- these should fail
  200. SELECT make_timestamptz(1973, 07, 15, 08, 15, 55.33, '2');
  201. ERROR: invalid input syntax for type numeric time zone: "2"
  202. HINT: Numeric time zones must have "-" or "+" as first character.
  203. SELECT make_timestamptz(2014, 12, 10, 10, 10, 10, '+16');
  204. ERROR: numeric time zone "+16" out of range
  205. SELECT make_timestamptz(2014, 12, 10, 10, 10, 10, '-16');
  206. ERROR: numeric time zone "-16" out of range
  207. -- should be true
  208. SELECT make_timestamptz(1973, 07, 15, 08, 15, 55.33, '+2') = '1973-07-15 08:15:55.33+02'::timestamptz;
  209. ?column?
  210. ----------
  211. t
  212. (1 row)
  213. SELECT make_timestamptz(1910, 12, 24, 0, 0, 0, 'Nehwon/Lankhmar');
  214. ERROR: time zone "Nehwon/Lankhmar" not recognized
  215. RESET TimeZone;
  216. -- errors
  217. select * from generate_series('2020-01-01 00:00'::timestamptz,
  218. '2020-01-02 03:00'::timestamptz,
  219. '0 hour'::interval);
  220. ERROR: step size cannot equal zero
  221. --
  222. -- Test behavior with a dynamic (time-varying) timezone abbreviation.
  223. -- These tests rely on the knowledge that MSK (Europe/Moscow standard time)
  224. -- moved forwards in Mar 2011 and backwards again in Oct 2014.
  225. --
  226. SET TimeZone to 'UTC';
  227. -- upper limit varies between integer and float timestamps, so hard to test
  228. -- nonfinite values
  229. SELECT to_timestamp(' Infinity'::float);
  230. to_timestamp
  231. --------------
  232. infinity
  233. (1 row)
  234. SELECT to_timestamp('-Infinity'::float);
  235. to_timestamp
  236. --------------
  237. -infinity
  238. (1 row)
  239. SELECT to_timestamp('NaN'::float);
  240. ERROR: timestamp cannot be NaN
  241. SET TimeZone to 'Europe/Moscow';
  242. RESET TimeZone;
  243. --
  244. -- Test that AT TIME ZONE isn't misoptimized when using an index (bug #14504)
  245. --
  246. create temp table tmptz (f1 timestamptz primary key);
  247. insert into tmptz values ('2017-01-18 00:00+00');