From 621691d816c45396ab9f8f9486ee9eb2a037795c Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Wed, 25 Jun 2003 21:14:15 +0000
Subject: [PATCH] In ISO datestyle, never emit just HH:MM, always emit HH:MM:SS
 or HH:MM:SS.SSS... when there is a nonzero part-of-a-day field in an interval
 value.  The seconds part used to be suppressed if zero, but there's no
 equivalent behavior for timestamp, and since we're modeling this format on
 timestamp it's probably wrong.  Per complaint and patch from Larry Rosenman.

---
 src/backend/utils/adt/datetime.c       |  10 +-
 src/test/regress/expected/interval.out | 156 ++++++++++++-------------
 2 files changed, 82 insertions(+), 84 deletions(-)

diff --git a/src/backend/utils/adt/datetime.c b/src/backend/utils/adt/datetime.c
index 31b42d4612f..288203389bb 100644
--- a/src/backend/utils/adt/datetime.c
+++ b/src/backend/utils/adt/datetime.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/datetime.c,v 1.105 2003/05/18 01:06:26 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/datetime.c,v 1.106 2003/06/25 21:14:14 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -3510,6 +3510,7 @@ EncodeInterval(struct tm * tm, fsec_t fsec, int style, char *str)
 				is_before = (tm->tm_mday < 0);
 				is_nonzero = TRUE;
 			}
+
 			if ((!is_nonzero) || (tm->tm_hour != 0) || (tm->tm_min != 0)
 				|| (tm->tm_sec != 0) || (fsec != 0))
 			{
@@ -3523,7 +3524,7 @@ EncodeInterval(struct tm * tm, fsec_t fsec, int style, char *str)
 				/* Mark as "non-zero" since the fields are now filled in */
 				is_nonzero = TRUE;
 
-				/* fractional seconds? */
+				/* need fractional seconds? */
 				if (fsec != 0)
 				{
 #ifdef HAVE_INT64_TIMESTAMP
@@ -3536,14 +3537,11 @@ EncodeInterval(struct tm * tm, fsec_t fsec, int style, char *str)
 #endif
 					TrimTrailingZeros(cp);
 					cp += strlen(cp);
-					is_nonzero = TRUE;
 				}
-				/* otherwise, integer seconds only? */
-				else if (tm->tm_sec != 0)
+				else
 				{
 					sprintf(cp, ":%02d", abs(tm->tm_sec));
 					cp += strlen(cp);
-					is_nonzero = TRUE;
 				}
 			}
 			break;
diff --git a/src/test/regress/expected/interval.out b/src/test/regress/expected/interval.out
index 32120be5412..da48abc2f59 100644
--- a/src/test/regress/expected/interval.out
+++ b/src/test/regress/expected/interval.out
@@ -6,43 +6,43 @@ SET DATESTYLE = 'ISO';
 SELECT INTERVAL '01:00' AS "One hour";
  One hour 
 ----------
- 01:00
+ 01:00:00
 (1 row)
 
 SELECT INTERVAL '+02:00' AS "Two hours";
  Two hours 
 -----------
- 02:00
+ 02:00:00
 (1 row)
 
 SELECT INTERVAL '-08:00' AS "Eight hours";
  Eight hours 
 -------------
- -08:00
+ -08:00:00
 (1 row)
 
 SELECT INTERVAL '-05' AS "Five hours";
  Five hours 
 ------------
- -05:00
+ -05:00:00
 (1 row)
 
 SELECT INTERVAL '-1 +02:03' AS "22 hours ago...";
  22 hours ago... 
 -----------------
- -21:57
+ -21:57:00
 (1 row)
 
 SELECT INTERVAL '-1 days +02:03' AS "22 hours ago...";
  22 hours ago... 
 -----------------
- -21:57
+ -21:57:00
 (1 row)
 
 SELECT INTERVAL '10 years -11 month -12 days +13:14' AS "9 years...";
-          9 years...           
--------------------------------
- 9 years 1 mon -11 days -10:46
+            9 years...            
+----------------------------------
+ 9 years 1 mon -11 days -10:46:00
 (1 row)
 
 CREATE TABLE INTERVAL_TBL (f1 interval);
@@ -63,10 +63,10 @@ INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 30 eons ago');
 ERROR:  Bad interval external representation '@ 30 eons ago'
 -- test interval operators
 SELECT '' AS ten, INTERVAL_TBL.*;
- ten |       f1       
------+----------------
-     | 00:01
-     | 05:00
+ ten |       f1        
+-----+-----------------
+     | 00:01:00
+     | 05:00:00
      | 10 days
      | 34 years
      | 3 mons
@@ -74,30 +74,30 @@ SELECT '' AS ten, INTERVAL_TBL.*;
      | 1 day 02:03:04
      | 6 years
      | 5 mons
-     | 5 mons 12:00
+     | 5 mons 12:00:00
 (10 rows)
 
 SELECT '' AS nine, INTERVAL_TBL.*
    WHERE INTERVAL_TBL.f1 <> interval '@ 10 days';
- nine |       f1       
-------+----------------
-      | 00:01
-      | 05:00
+ 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
+      | 5 mons 12:00:00
 (9 rows)
 
 SELECT '' AS three, INTERVAL_TBL.*
    WHERE INTERVAL_TBL.f1 <= interval '@ 5 hours';
  three |    f1     
 -------+-----------
-       | 00:01
-       | 05:00
+       | 00:01:00
+       | 05:00:00
        | -00:00:14
 (3 rows)
 
@@ -105,8 +105,8 @@ SELECT '' AS three, INTERVAL_TBL.*
    WHERE INTERVAL_TBL.f1 < interval '@ 1 day';
  three |    f1     
 -------+-----------
-       | 00:01
-       | 05:00
+       | 00:01:00
+       | 05:00:00
        | -00:00:14
 (3 rows)
 
@@ -119,81 +119,81 @@ SELECT '' AS one, INTERVAL_TBL.*
 
 SELECT '' AS five, INTERVAL_TBL.* 
    WHERE INTERVAL_TBL.f1 >= interval '@ 1 month';
- five |      f1      
-------+--------------
+ five |       f1        
+------+-----------------
       | 34 years
       | 3 mons
       | 6 years
       | 5 mons
-      | 5 mons 12:00
+      | 5 mons 12:00:00
 (5 rows)
 
 SELECT '' AS nine, INTERVAL_TBL.*
    WHERE INTERVAL_TBL.f1 > interval '@ 3 seconds ago';
- nine |       f1       
-------+----------------
-      | 00:01
-      | 05:00
+ 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
+      | 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:14
-           | 05:00          | -00:00:14
-           | 05:00          | 00:01
-           | 1 day 02:03:04 | -00:00:14
-           | 1 day 02:03:04 | 00:01
-           | 1 day 02:03:04 | 05:00
-           | 10 days        | -00:00:14
-           | 10 days        | 00:01
-           | 10 days        | 05:00
-           | 10 days        | 1 day 02:03:04
-           | 3 mons         | -00:00:14
-           | 3 mons         | 00:01
-           | 3 mons         | 05:00
-           | 3 mons         | 1 day 02:03:04
-           | 3 mons         | 10 days
-           | 5 mons         | -00:00:14
-           | 5 mons         | 00:01
-           | 5 mons         | 05:00
-           | 5 mons         | 1 day 02:03:04
-           | 5 mons         | 10 days
-           | 5 mons         | 3 mons
-           | 5 mons 12:00   | -00:00:14
-           | 5 mons 12:00   | 00:01
-           | 5 mons 12:00   | 05:00
-           | 5 mons 12:00   | 1 day 02:03:04
-           | 5 mons 12:00   | 10 days
-           | 5 mons 12:00   | 3 mons
-           | 5 mons 12:00   | 5 mons
-           | 6 years        | -00:00:14
-           | 6 years        | 00:01
-           | 6 years        | 05: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
-           | 34 years       | -00:00:14
-           | 34 years       | 00:01
-           | 34 years       | 05: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
-           | 34 years       | 6 years
+ 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)
 
 SET DATESTYLE = 'postgres';
-- 
GitLab