--
-- HOROLOGY
--
-- should fail in mdy mode:
SELECT timestamp with time zone '27/12/2001 04:05:06.789-08';
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:5: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(130): ERROR:  date/time field value out of range: "27/12/2001 04:05:06.789-08"
HINT:  Perhaps you need a different "datestyle" setting.



-- 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";
 True 
------
 t
(1 row)

SELECT (timestamp without time zone 'today' = (timestamp without time zone 'tomorrow' - interval '1 day')) as "True";
 True 
------
 t
(1 row)

SELECT (timestamp without time zone 'today 10:30' = (timestamp without time zone 'yesterday' + interval '1 day 10 hr 30 min')) as "True";
 True 
------
 t
(1 row)

SELECT (timestamp without time zone '10:30 today' = (timestamp without time zone 'yesterday' + interval '1 day 10 hr 30 min')) as "True";
 True 
------
 t
(1 row)

SELECT (timestamp without time zone 'tomorrow' = (timestamp without time zone 'yesterday' + interval '2 days')) as "True";
 True 
------
 t
(1 row)

SELECT (timestamp without time zone 'tomorrow 16:00:00' = (timestamp without time zone 'today' + interval '1 day 16 hours')) as "True";
 True 
------
 t
(1 row)

SELECT (timestamp without time zone '16:00:00 tomorrow' = (timestamp without time zone 'today' + interval '1 day 16 hours')) as "True";
 True 
------
 t
(1 row)

SELECT (timestamp without time zone 'yesterday 12:34:56' = (timestamp without time zone 'tomorrow' - interval '2 days - 12:34:56')) as "True";
 True 
------
 t
(1 row)

SELECT (timestamp without time zone '12:34:56 yesterday' = (timestamp without time zone 'tomorrow' - interval '2 days - 12:34:56')) as "True";
 True 
------
 t
(1 row)

SELECT (timestamp with time zone 'today' = (timestamp with time zone 'yesterday' + interval '1 day')) as "True";
 True 
------
 t
(1 row)

SELECT (timestamp with time zone 'today' = (timestamp with time zone 'tomorrow' - interval '1 day')) as "True";
 True 
------
 t
(1 row)

SELECT (timestamp with time zone 'tomorrow' = (timestamp with time zone 'yesterday' + interval '2 days')) as "True";
 True 
------
 t
(1 row)

--
-- time, interval arithmetic
--
SELECT CAST(interval '02:03' AS time) AS "02:03:00";
 02:03:00 
----------
 02:03:00
(1 row)

SELECT time '01:30' + interval '02:01' AS "03:31:00";
 03:31:00 
----------
 03:31:00
(1 row)

SELECT time '01:30' - interval '02:01' AS "23:29:00";
 23:29:00 
----------
 23:29:00
(1 row)

SELECT time '02:30' + interval '36:01' AS "14:31:00";
 14:31:00 
----------
 14:31:00
(1 row)

SELECT time '03:30' + interval '1 month 04:01' AS "07:31:00";
 07:31:00 
----------
 07:31:00
(1 row)

SELECT time with time zone '01:30-08' - interval '02:01' AS "23:29:00-08";
 23:29:00-08 
-------------
 23:29:00-08
(1 row)

SELECT time with time zone '02:30-08' + interval '36:01' AS "14:31:00-08";
 14:31:00-08 
-------------
 14:31:00-08
(1 row)

-- 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";
-- Try the following two tests instead, as a poor substitute
SELECT CAST(CAST(date 'today' + time with time zone '05:30'
            + interval '02:01' AS time with time zone) AS time) AS "07:31:00";
 07:31:00 
----------
 07:31:00
(1 row)

SELECT CAST(cast(date 'today' + time with time zone '03:30'
  + interval '1 month 04:01' as timestamp without time zone) AS time) AS "07:31:00";
 07:31:00 
----------
 07:31:00
(1 row)

-- SQL9x OVERLAPS operator
-- 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";
 True 
------
 t
(1 row)

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";
 False 
-------
 f
(1 row)

-- 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";
 True 
------
 t
(1 row)

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";
 False 
-------
 f
(1 row)

-- test time and interval
SELECT (time '00:00', time '01:00')
  OVERLAPS (time '00:30', time '01:30') AS "True";
 True 
------
 t
(1 row)

--
-- Comparisons between datetime types, especially overflow cases
---
SELECT '2202020-10-05'::date::timestamp;  -- fail
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:74: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(40): ERROR:  date out of range for timestamp



SELECT '2202020-10-05'::date > '2020-10-05'::timestamp as t;
 t 
---
 t
(1 row)

SELECT '2020-10-05'::timestamp > '2202020-10-05'::date as f;
 f 
---
 f
(1 row)

SELECT '2202020-10-05'::date::timestamptz;  -- fail
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:78: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(40): ERROR:  date out of range for timestamp



SELECT '2202020-10-05'::date > '2020-10-05'::timestamptz as t;
 t 
---
 t
(1 row)

SELECT '2020-10-05'::timestamptz > '2202020-10-05'::date as f;
 f 
---
 f
(1 row)

SELECT '4714-11-24 BC'::date < '2020-10-05'::timestamptz as t;
 t 
---
 t
(1 row)

SELECT '2020-10-05'::timestamptz >= '4714-11-24 BC'::date as t;
 t 
---
 t
(1 row)

SELECT '4714-11-24 BC'::timestamp < '2020-10-05'::timestamptz as t;
 t 
---
 t
(1 row)

SELECT '2020-10-05'::timestamptz >= '4714-11-24 BC'::timestamp as t;
 t 
---
 t
(1 row)

--
-- Formats
--
--
-- to_timestamp()
--
SELECT to_timestamp('97/Feb/16', 'YYMonDD');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:98: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(123): ERROR:  invalid value "/Feb/16" for "Mon"
DETAIL:  The given value did not match any of the allowed values for this field.



SELECT to_timestamp('2011-12-18 11:38 PST', 'YYYY-MM-DD HH12:MI TZ');  -- NYI
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:101: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(59): ERROR:  formatting field "TZ" is only supported in to_char



--
-- Check handling of BC dates
--
--
-- Check handling of multiple spaces in format and/or input
--
SELECT to_date('2011 x12 x18', 'YYYYxMMxDD');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:112: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(71): ERROR:  invalid value "x1" for "MM"
DETAIL:  Value must be an integer.



--
-- Check errors for some incorrect usages of to_timestamp() and to_date()
--
-- Mixture of date conventions (ISO week and Gregorian):
SELECT to_timestamp('2005527', 'YYYYIWID');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:119: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(132): ERROR:  invalid combination of date conventions
HINT:  Do not mix Gregorian and ISO week date conventions in a formatting template.



-- Insufficient characters in the source string:
SELECT to_timestamp('19971', 'YYYYMMDD');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:122: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(193): ERROR:  source string too short for "MM" formatting field
DETAIL:  Field requires 2 characters, but only 1 remain.
HINT:  If your source string is not fixed-width, try using the "FM" modifier.



-- Insufficient digit characters for a single node:
SELECT to_timestamp('19971)24', 'YYYYMMDD');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:125: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(180): ERROR:  invalid value "1)" for "MM"
DETAIL:  Field requires 2 characters, but only 1 could be parsed.
HINT:  If your source string is not fixed-width, try using the "FM" modifier.



-- We don't accept full-length day or month names if short form is specified:
SELECT to_timestamp('Friday 1-January-1999', 'DY DD MON YYYY');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:128: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(71): ERROR:  invalid value "da" for "DD"
DETAIL:  Value must be an integer.



SELECT to_timestamp('Fri 1-January-1999', 'DY DD MON YYYY');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:129: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(75): ERROR:  invalid value "uary" for "YYYY"
DETAIL:  Value must be an integer.



-- Value clobbering:
SELECT to_timestamp('1997-11-Jan-16', 'YYYY-MM-Mon-DD');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:132: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(140): ERROR:  conflicting values for "Mon" field in formatting string
DETAIL:  This value contradicts a previous setting for the same field type.



-- Non-numeric input:
SELECT to_timestamp('199711xy', 'YYYYMMDD');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:135: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(71): ERROR:  invalid value "xy" for "DD"
DETAIL:  Value must be an integer.



-- Input that doesn't fit in an int:
SELECT to_timestamp('10000000000', 'FMYYYY');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:138: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(121): ERROR:  value for "YYYY" in source string is out of range
DETAIL:  Value must be in the range -2147483648 to 2147483647.



-- Out-of-range and not-quite-out-of-range fields:
SELECT to_timestamp('2016-06-13 25:00:00', 'YYYY-MM-DD HH24:MI:SS');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:141: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(66): ERROR:  date/time field value out of range: "2016-06-13 25:00:00"



SELECT to_timestamp('2016-06-13 15:60:00', 'YYYY-MM-DD HH24:MI:SS');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:142: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(66): ERROR:  date/time field value out of range: "2016-06-13 15:60:00"



SELECT to_timestamp('2016-06-13 15:50:60', 'YYYY-MM-DD HH24:MI:SS');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:143: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(66): ERROR:  date/time field value out of range: "2016-06-13 15:50:60"



SELECT to_timestamp('2016-06-13 15:50:55', 'YYYY-MM-DD HH:MI:SS');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:144: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(115): ERROR:  hour "15" is invalid for the 12-hour clock
HINT:  Use the 24-hour clock, or give an hour between 1 and 12.



SELECT to_timestamp('2016-13-01 15:50:55', 'YYYY-MM-DD HH24:MI:SS');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:145: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(66): ERROR:  date/time field value out of range: "2016-13-01 15:50:55"



SELECT to_timestamp('2016-02-30 15:50:55', 'YYYY-MM-DD HH24:MI:SS');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:146: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(66): ERROR:  date/time field value out of range: "2016-02-30 15:50:55"



SELECT to_timestamp('2015-02-29 15:50:55', 'YYYY-MM-DD HH24:MI:SS');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:147: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(66): ERROR:  date/time field value out of range: "2015-02-29 15:50:55"



SELECT to_timestamp('2015-02-11 86400', 'YYYY-MM-DD SSSS');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:148: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(63): ERROR:  date/time field value out of range: "2015-02-11 86400"



SELECT to_timestamp('2015-02-11 86400', 'YYYY-MM-DD SSSSS');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:149: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(63): ERROR:  date/time field value out of range: "2015-02-11 86400"



SELECT to_date('2016-13-10', 'YYYY-MM-DD');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:150: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(57): ERROR:  date/time field value out of range: "2016-13-10"



SELECT to_date('2016-02-30', 'YYYY-MM-DD');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:151: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(57): ERROR:  date/time field value out of range: "2016-02-30"



SELECT to_date('2015-02-29', 'YYYY-MM-DD');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:152: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(57): ERROR:  date/time field value out of range: "2015-02-29"



SELECT to_date('2015 366', 'YYYY DDD');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:153: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(55): ERROR:  date/time field value out of range: "2015 366"



SELECT to_date('2016 367', 'YYYY DDD');
psql:/home/runner/.ya/build/build_root/1xh3/00369b/environment/arcadia/ydb/tests/functional/postgresql/cases/horology.sql:154: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(55): ERROR:  date/time field value out of range: "2016 367"



--
-- Check behavior with SQL-style fixed-GMT-offset time zone (cf bug #8572)
--
