horology.sql 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. --
  2. -- HOROLOGY
  3. --
  4. SET DateStyle = 'Postgres, MDY';
  5. -- should fail in mdy mode:
  6. SELECT timestamp with time zone '27/12/2001 04:05:06.789-08';
  7. set datestyle to dmy;
  8. reset datestyle;
  9. SET DateStyle = 'German';
  10. SET DateStyle = 'ISO';
  11. -- As of 7.4, allow time without time zone having a time zone specified
  12. SELECT time without time zone '040506.789+08';
  13. SELECT time without time zone '040506.789-08';
  14. SELECT time without time zone 'T040506.789+08';
  15. SELECT time without time zone 'T040506.789-08';
  16. SELECT time with time zone '040506.789+08';
  17. SELECT time with time zone '040506.789-08';
  18. SELECT time with time zone 'T040506.789+08';
  19. SELECT time with time zone 'T040506.789-08';
  20. SELECT time with time zone 'T040506.789 +08';
  21. SELECT time with time zone 'T040506.789 -08';
  22. SET DateStyle = 'Postgres, MDY';
  23. -- Shorthand values
  24. -- Not directly usable for regression testing since these are not constants.
  25. -- So, just try to test parser and hope for the best - thomas 97/04/26
  26. SELECT (timestamp without time zone 'today' = (timestamp without time zone 'yesterday' + interval '1 day')) as "True";
  27. SELECT (timestamp without time zone 'today' = (timestamp without time zone 'tomorrow' - interval '1 day')) as "True";
  28. SELECT (timestamp without time zone 'today 10:30' = (timestamp without time zone 'yesterday' + interval '1 day 10 hr 30 min')) as "True";
  29. SELECT (timestamp without time zone '10:30 today' = (timestamp without time zone 'yesterday' + interval '1 day 10 hr 30 min')) as "True";
  30. SELECT (timestamp without time zone 'tomorrow' = (timestamp without time zone 'yesterday' + interval '2 days')) as "True";
  31. SELECT (timestamp without time zone 'tomorrow 16:00:00' = (timestamp without time zone 'today' + interval '1 day 16 hours')) as "True";
  32. SELECT (timestamp without time zone '16:00:00 tomorrow' = (timestamp without time zone 'today' + interval '1 day 16 hours')) as "True";
  33. SELECT (timestamp without time zone 'yesterday 12:34:56' = (timestamp without time zone 'tomorrow' - interval '2 days - 12:34:56')) as "True";
  34. SELECT (timestamp without time zone '12:34:56 yesterday' = (timestamp without time zone 'tomorrow' - interval '2 days - 12:34:56')) as "True";
  35. SELECT (timestamp without time zone 'tomorrow' > 'now') as "True";
  36. SELECT (timestamp with time zone 'today' = (timestamp with time zone 'yesterday' + interval '1 day')) as "True";
  37. SELECT (timestamp with time zone 'today' = (timestamp with time zone 'tomorrow' - interval '1 day')) as "True";
  38. SELECT (timestamp with time zone 'tomorrow' = (timestamp with time zone 'yesterday' + interval '2 days')) as "True";
  39. SELECT (timestamp with time zone 'tomorrow' > 'now') as "True";
  40. -- timestamp with time zone, interval arithmetic around DST change
  41. -- (just for fun, let's use an intentionally nonstandard POSIX zone spec)
  42. SET TIME ZONE 'CST7CDT,M4.1.0,M10.5.0';
  43. RESET TIME ZONE;
  44. SELECT CAST(interval '02:03' AS time) AS "02:03:00";
  45. SELECT time '01:30' + interval '02:01' AS "03:31:00";
  46. SELECT time '01:30' - interval '02:01' AS "23:29:00";
  47. SELECT time '02:30' + interval '36:01' AS "14:31:00";
  48. SELECT time '03:30' + interval '1 month 04:01' AS "07:31:00";
  49. SELECT time with time zone '01:30-08' - interval '02:01' AS "23:29:00-08";
  50. SELECT time with time zone '02:30-08' + interval '36:01' AS "14:31:00-08";
  51. -- These two tests cannot be used because they default to current timezone,
  52. -- which may be either -08 or -07 depending on the time of year.
  53. -- SELECT time with time zone '01:30' + interval '02:01' AS "03:31:00-08";
  54. -- SELECT time with time zone '03:30' + interval '1 month 04:01' AS "07:31:00-08";
  55. -- Try the following two tests instead, as a poor substitute
  56. SELECT CAST(CAST(date 'today' + time with time zone '05:30'
  57. + interval '02:01' AS time with time zone) AS time) AS "07:31:00";
  58. SELECT CAST(cast(date 'today' + time with time zone '03:30'
  59. + interval '1 month 04:01' as timestamp without time zone) AS time) AS "07:31:00";
  60. -- SQL9x OVERLAPS operator
  61. -- test with time zone
  62. SELECT (timestamp with time zone '2000-11-27', timestamp with time zone '2000-11-28')
  63. OVERLAPS (timestamp with time zone '2000-11-27 12:00', timestamp with time zone '2000-11-30') AS "True";
  64. SELECT (timestamp with time zone '2000-11-26', timestamp with time zone '2000-11-27')
  65. OVERLAPS (timestamp with time zone '2000-11-27 12:00', timestamp with time zone '2000-11-30') AS "False";
  66. -- test without time zone
  67. SELECT (timestamp without time zone '2000-11-27', timestamp without time zone '2000-11-28')
  68. OVERLAPS (timestamp without time zone '2000-11-27 12:00', timestamp without time zone '2000-11-30') AS "True";
  69. SELECT (timestamp without time zone '2000-11-26', timestamp without time zone '2000-11-27')
  70. OVERLAPS (timestamp without time zone '2000-11-27 12:00', timestamp without time zone '2000-11-30') AS "False";
  71. -- test time and interval
  72. SELECT (time '00:00', time '01:00')
  73. OVERLAPS (time '00:30', time '01:30') AS "True";
  74. CREATE TABLE TEMP_TIMESTAMP (f1 timestamp with time zone);
  75. -- get some candidate input values
  76. INSERT INTO TEMP_TIMESTAMP (f1)
  77. SELECT d1 FROM TIMESTAMP_TBL
  78. WHERE d1 BETWEEN '13-jun-1957' AND '1-jan-1997'
  79. OR d1 BETWEEN '1-jan-1999' AND '1-jan-2010';
  80. DROP TABLE TEMP_TIMESTAMP;
  81. --
  82. -- Comparisons between datetime types, especially overflow cases
  83. ---
  84. SELECT '2202020-10-05'::date::timestamp; -- fail
  85. SELECT '2202020-10-05'::date > '2020-10-05'::timestamp as t;
  86. SELECT '2020-10-05'::timestamp > '2202020-10-05'::date as f;
  87. SELECT '2202020-10-05'::date::timestamptz; -- fail
  88. SELECT '2202020-10-05'::date > '2020-10-05'::timestamptz as t;
  89. SELECT '2020-10-05'::timestamptz > '2202020-10-05'::date as f;
  90. SET TimeZone = 'UTC-2';
  91. SELECT '4714-11-24 BC'::date < '2020-10-05'::timestamptz as t;
  92. SELECT '2020-10-05'::timestamptz >= '4714-11-24 BC'::date as t;
  93. SELECT '4714-11-24 BC'::timestamp < '2020-10-05'::timestamptz as t;
  94. SELECT '2020-10-05'::timestamptz >= '4714-11-24 BC'::timestamp as t;
  95. RESET TimeZone;
  96. --
  97. -- Formats
  98. --
  99. SET DateStyle TO 'US,Postgres';
  100. SET DateStyle TO 'US,ISO';
  101. SELECT d1 AS us_iso FROM TIMESTAMP_TBL;
  102. SET DateStyle TO 'US,SQL';
  103. SET DateStyle TO 'European,Postgres';
  104. SET DateStyle TO 'European,ISO';
  105. SET DateStyle TO 'European,SQL';
  106. RESET DateStyle;
  107. SELECT to_timestamp('97/Feb/16', 'YYMonDD');
  108. SELECT to_timestamp('2011-12-18 11:38 PST', 'YYYY-MM-DD HH12:MI TZ'); -- NYI
  109. SELECT to_timestamp('2000 + + JUN', 'YYYY MON');
  110. SELECT to_date('2011 x12 x18', 'YYYYxMMxDD');
  111. --
  112. -- Check errors for some incorrect usages of to_timestamp() and to_date()
  113. --
  114. -- Mixture of date conventions (ISO week and Gregorian):
  115. SELECT to_timestamp('2005527', 'YYYYIWID');
  116. -- Insufficient characters in the source string:
  117. SELECT to_timestamp('19971', 'YYYYMMDD');
  118. -- Insufficient digit characters for a single node:
  119. SELECT to_timestamp('19971)24', 'YYYYMMDD');
  120. -- We don't accept full-length day or month names if short form is specified:
  121. SELECT to_timestamp('Friday 1-January-1999', 'DY DD MON YYYY');
  122. SELECT to_timestamp('Fri 1-January-1999', 'DY DD MON YYYY');
  123. -- Value clobbering:
  124. SELECT to_timestamp('1997-11-Jan-16', 'YYYY-MM-Mon-DD');
  125. -- Non-numeric input:
  126. SELECT to_timestamp('199711xy', 'YYYYMMDD');
  127. -- Input that doesn't fit in an int:
  128. SELECT to_timestamp('10000000000', 'FMYYYY');
  129. -- Out-of-range and not-quite-out-of-range fields:
  130. SELECT to_timestamp('2016-06-13 25:00:00', 'YYYY-MM-DD HH24:MI:SS');
  131. SELECT to_timestamp('2016-06-13 15:60:00', 'YYYY-MM-DD HH24:MI:SS');
  132. SELECT to_timestamp('2016-06-13 15:50:60', 'YYYY-MM-DD HH24:MI:SS');
  133. SELECT to_timestamp('2016-06-13 15:50:55', 'YYYY-MM-DD HH:MI:SS');
  134. SELECT to_timestamp('2016-13-01 15:50:55', 'YYYY-MM-DD HH24:MI:SS');
  135. SELECT to_timestamp('2016-02-30 15:50:55', 'YYYY-MM-DD HH24:MI:SS');
  136. SELECT to_timestamp('2015-02-29 15:50:55', 'YYYY-MM-DD HH24:MI:SS');
  137. SELECT to_timestamp('2015-02-11 86400', 'YYYY-MM-DD SSSS');
  138. SELECT to_timestamp('2015-02-11 86400', 'YYYY-MM-DD SSSSS');
  139. SELECT to_date('2016-13-10', 'YYYY-MM-DD');
  140. SELECT to_date('2016-02-30', 'YYYY-MM-DD');
  141. SELECT to_date('2015-02-29', 'YYYY-MM-DD');
  142. SELECT to_date('2015 366', 'YYYY DDD');
  143. SELECT to_date('2016 367', 'YYYY DDD');
  144. --
  145. -- Check behavior with SQL-style fixed-GMT-offset time zone (cf bug #8572)
  146. --
  147. SET TIME ZONE 'America/New_York';
  148. SET TIME ZONE '-1.5';
  149. SELECT to_char('2012-12-12 12:00'::timestamptz, 'YYYY-MM-DD SSSS');
  150. SELECT to_char('2012-12-12 12:00'::timestamptz, 'YYYY-MM-DD SSSSS');
  151. RESET TIME ZONE;