--
-- INTERVAL
--
SET DATESTYLE = 'ISO';
-- check acceptance of "time zone style"
SELECT INTERVAL '01:00' AS "One hour";
 One hour 
----------
 01:00:00
(1 row)

SELECT INTERVAL '+02:00' AS "Two hours";
 Two hours 
-----------
 02:00:00
(1 row)

SELECT INTERVAL '-08:00' AS "Eight hours";
 Eight hours 
-------------
 -08:00:00
(1 row)

SELECT INTERVAL '-05' AS "Five hours";
 Five hours 
------------
 -05:00:00
(1 row)

SELECT INTERVAL '-1 +02:03' AS "22 hours ago...";
  22 hours ago...  
-------------------
 -1 days +02:03:00
(1 row)

SELECT INTERVAL '-1 days +02:03' AS "22 hours ago...";
  22 hours ago...  
-------------------
 -1 days +02:03:00
(1 row)

SELECT INTERVAL '1.5 weeks' AS "Ten days twelve hours";
 Ten days twelve hours 
-----------------------
 10 days 12:00:00
(1 row)

SELECT INTERVAL '1.5 months' AS "One month 15 days";
 One month 15 days 
-------------------
 1 mon 15 days
(1 row)

SELECT INTERVAL '10 years -11 month -12 days +13:14' AS "9 years...";
            9 years...            
----------------------------------
 9 years 1 mon -12 days +13:14:00
(1 row)

CREATE TABLE INTERVAL_TBL (f1 interval);
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 1 minute');
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 5 hour');
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 10 day');
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 34 year');
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 3 months');
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 14 seconds ago');
INSERT INTO INTERVAL_TBL (f1) VALUES ('1 day 2 hours 3 minutes 4 seconds');
INSERT INTO INTERVAL_TBL (f1) VALUES ('6 years');
INSERT INTO INTERVAL_TBL (f1) VALUES ('5 months');
INSERT INTO INTERVAL_TBL (f1) VALUES ('5 months 12 hours');
-- badly formatted interval
INSERT INTO INTERVAL_TBL (f1) VALUES ('badly formatted interval');
ERROR:  invalid input syntax for type interval: "badly formatted interval"
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 30 eons ago');
ERROR:  invalid input syntax for type interval: "@ 30 eons ago"
-- test interval operators
SELECT '' AS ten, * FROM INTERVAL_TBL;
 ten |       f1        
-----+-----------------
     | 00:01:00
     | 05:00:00
     | 10 days
     | 34 years
     | 3 mons
     | -00:00:14
     | 1 day 02:03:04
     | 6 years
     | 5 mons
     | 5 mons 12:00:00
(10 rows)

SELECT '' AS nine, * FROM INTERVAL_TBL
   WHERE INTERVAL_TBL.f1 <> interval '@ 10 days';
 nine |       f1        
------+-----------------
      | 00:01:00
      | 05:00:00
      | 34 years
      | 3 mons
      | -00:00:14
      | 1 day 02:03:04
      | 6 years
      | 5 mons
      | 5 mons 12:00:00
(9 rows)

SELECT '' AS three, * FROM INTERVAL_TBL
   WHERE INTERVAL_TBL.f1 <= interval '@ 5 hours';
 three |    f1     
-------+-----------
       | 00:01:00
       | 05:00:00
       | -00:00:14
(3 rows)

SELECT '' AS three, * FROM INTERVAL_TBL
   WHERE INTERVAL_TBL.f1 < interval '@ 1 day';
 three |    f1     
-------+-----------
       | 00:01:00
       | 05:00:00
       | -00:00:14
(3 rows)

SELECT '' AS one, * FROM INTERVAL_TBL
   WHERE INTERVAL_TBL.f1 = interval '@ 34 years';
 one |    f1    
-----+----------
     | 34 years
(1 row)

SELECT '' AS five, * FROM INTERVAL_TBL 
   WHERE INTERVAL_TBL.f1 >= interval '@ 1 month';
 five |       f1        
------+-----------------
      | 34 years
      | 3 mons
      | 6 years
      | 5 mons
      | 5 mons 12:00:00
(5 rows)

SELECT '' AS nine, * FROM INTERVAL_TBL
   WHERE INTERVAL_TBL.f1 > interval '@ 3 seconds ago';
 nine |       f1        
------+-----------------
      | 00:01:00
      | 05:00:00
      | 10 days
      | 34 years
      | 3 mons
      | 1 day 02:03:04
      | 6 years
      | 5 mons
      | 5 mons 12:00:00
(9 rows)

SELECT '' AS fortyfive, r1.*, r2.*
   FROM INTERVAL_TBL r1, INTERVAL_TBL r2
   WHERE r1.f1 > r2.f1
   ORDER BY r1.f1, r2.f1;
 fortyfive |       f1        |       f1        
-----------+-----------------+-----------------
           | 00:01:00        | -00:00:14
           | 05:00:00        | -00:00:14
           | 05:00:00        | 00:01:00
           | 1 day 02:03:04  | -00:00:14
           | 1 day 02:03:04  | 00:01:00
           | 1 day 02:03:04  | 05:00:00
           | 10 days         | -00:00:14
           | 10 days         | 00:01:00
           | 10 days         | 05:00:00
           | 10 days         | 1 day 02:03:04
           | 3 mons          | -00:00:14
           | 3 mons          | 00:01:00
           | 3 mons          | 05:00:00
           | 3 mons          | 1 day 02:03:04
           | 3 mons          | 10 days
           | 5 mons          | -00:00:14
           | 5 mons          | 00:01:00
           | 5 mons          | 05:00:00
           | 5 mons          | 1 day 02:03:04
           | 5 mons          | 10 days
           | 5 mons          | 3 mons
           | 5 mons 12:00:00 | -00:00:14
           | 5 mons 12:00:00 | 00:01:00
           | 5 mons 12:00:00 | 05:00:00
           | 5 mons 12:00:00 | 1 day 02:03:04
           | 5 mons 12:00:00 | 10 days
           | 5 mons 12:00:00 | 3 mons
           | 5 mons 12:00:00 | 5 mons
           | 6 years         | -00:00:14
           | 6 years         | 00:01:00
           | 6 years         | 05:00:00
           | 6 years         | 1 day 02:03:04
           | 6 years         | 10 days
           | 6 years         | 3 mons
           | 6 years         | 5 mons
           | 6 years         | 5 mons 12:00:00
           | 34 years        | -00:00:14
           | 34 years        | 00:01:00
           | 34 years        | 05:00:00
           | 34 years        | 1 day 02:03:04
           | 34 years        | 10 days
           | 34 years        | 3 mons
           | 34 years        | 5 mons
           | 34 years        | 5 mons 12:00:00
           | 34 years        | 6 years
(45 rows)

-- Test multiplication and division with intervals.
-- Floating point arithmetic rounding errors can lead to unexpected results, 
-- though the code attempts to do the right thing and round up to days and 
-- minutes to avoid results such as '3 days 24:00 hours' or '14:20:60'. 
-- Note that it is expected for some day components to be greater than 29 and 
-- some time components be greater than 23:59:59 due to how intervals are 
-- stored internally.
CREATE TABLE INTERVAL_MULDIV_TBL (span interval);
COPY INTERVAL_MULDIV_TBL FROM STDIN;
SELECT span * 0.3 AS product
FROM INTERVAL_MULDIV_TBL;
              product               
------------------------------------
 1 year 12 days 122:24:00
 -1 years -12 days +93:36:00
 -3 days -14:24:00
 2 mons 13 days 01:22:28.80
 -10 mons +120 days 37:28:21.6567
 1 mon 6 days
 4 mons 6 days
 24 years 11 mons 320 days 16:48:00
(8 rows)

SELECT span * 8.2 AS product
FROM INTERVAL_MULDIV_TBL;
                   product                   
---------------------------------------------
 28 years 104 days 2961:36:00
 -28 years -104 days +2942:24:00
 -98 days -09:36:00
 6 years 1 mon -197 days +93:34:27.20
 -24 years -7 mons +3946 days 640:15:11.9498
 2 years 8 mons 24 days
 9 years 6 mons 24 days
 682 years 7 mons 8215 days 19:12:00
(8 rows)

SELECT span / 10 AS quotient
FROM INTERVAL_MULDIV_TBL;
             quotient             
----------------------------------
 4 mons 4 days 40:48:00
 -4 mons -4 days +31:12:00
 -1 days -04:48:00
 25 days -15:32:30.40
 -3 mons +30 days 12:29:27.2189
 12 days
 1 mon 12 days
 8 years 3 mons 126 days 21:36:00
(8 rows)

SELECT span / 100 AS quotient
FROM INTERVAL_MULDIV_TBL;
        quotient         
-------------------------
 12 days 13:40:48
 -12 days -06:28:48
 -02:52:48
 2 days 10:26:44.96
 -6 days +01:14:56.72189
 1 day 04:48:00
 4 days 04:48:00
 9 mons 39 days 16:33:36
(8 rows)

DROP TABLE INTERVAL_MULDIV_TBL;
SET DATESTYLE = 'postgres';
SELECT '' AS ten, * FROM INTERVAL_TBL;
 ten |              f1               
-----+-------------------------------
     | @ 1 min
     | @ 5 hours
     | @ 10 days
     | @ 34 years
     | @ 3 mons
     | @ 14 secs ago
     | @ 1 day 2 hours 3 mins 4 secs
     | @ 6 years
     | @ 5 mons
     | @ 5 mons 12 hours
(10 rows)

-- test avg(interval), which is somewhat fragile since people have been
-- known to change the allowed input syntax for type interval without
-- updating pg_aggregate.agginitval
select avg(f1) from interval_tbl;
                       avg                       
-------------------------------------------------
 @ 4 years 1 mon 10 days 4 hours 18 mins 23 secs
(1 row)

-- test long interval input
select '4 millenniums 5 centuries 4 decades 1 year 4 months 4 days 17 minutes 31 seconds'::interval;
                  interval                  
--------------------------------------------
 @ 4541 years 4 mons 4 days 17 mins 31 secs
(1 row)

-- test justify_hours() and justify_days()
SELECT justify_hours(interval '6 months 3 days 52 hours 3 minutes 2 seconds') as "6 mons 5 days 4 hours 3 mins 2 seconds";
 6 mons 5 days 4 hours 3 mins 2 seconds 
----------------------------------------
 @ 6 mons 5 days 4 hours 3 mins 2 secs
(1 row)

SELECT justify_days(interval '6 months 36 days 5 hours 4 minutes 3 seconds') as "7 mons 6 days 5 hours 4 mins 3 seconds";
 7 mons 6 days 5 hours 4 mins 3 seconds 
----------------------------------------
 @ 7 mons 6 days 5 hours 4 mins 3 secs
(1 row)

-- test justify_interval()
SELECT justify_interval(interval '1 month -1 hour') as "1 month -1 hour";
  1 month -1 hour   
--------------------
 @ 29 days 23 hours
(1 row)

-- test fractional second input, and detection of duplicate units
SET DATESTYLE = 'ISO';
SELECT '1 millisecond'::interval, '1 microsecond'::interval,
       '500 seconds 99 milliseconds 51 microseconds'::interval;
   interval   |    interval     |    interval     
--------------+-----------------+-----------------
 00:00:00.001 | 00:00:00.000001 | 00:08:20.099051
(1 row)

SELECT '3 days 5 milliseconds'::interval;
      interval       
---------------------
 3 days 00:00:00.005
(1 row)

SELECT '1 second 2 seconds'::interval;              -- error
ERROR:  invalid input syntax for type interval: "1 second 2 seconds"
SELECT '10 milliseconds 20 milliseconds'::interval; -- error
ERROR:  invalid input syntax for type interval: "10 milliseconds 20 milliseconds"
SELECT '5.5 seconds 3 milliseconds'::interval;      -- error
ERROR:  invalid input syntax for type interval: "5.5 seconds 3 milliseconds"
SELECT '1:20:05 5 microseconds'::interval;          -- error
ERROR:  invalid input syntax for type interval: "1:20:05 5 microseconds"
