Commit 621691d8 authored by Tom Lane's avatar Tom Lane

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.
parent d2ba12b4
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * 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) ...@@ -3510,6 +3510,7 @@ EncodeInterval(struct tm * tm, fsec_t fsec, int style, char *str)
is_before = (tm->tm_mday < 0); is_before = (tm->tm_mday < 0);
is_nonzero = TRUE; is_nonzero = TRUE;
} }
if ((!is_nonzero) || (tm->tm_hour != 0) || (tm->tm_min != 0) if ((!is_nonzero) || (tm->tm_hour != 0) || (tm->tm_min != 0)
|| (tm->tm_sec != 0) || (fsec != 0)) || (tm->tm_sec != 0) || (fsec != 0))
{ {
...@@ -3523,7 +3524,7 @@ EncodeInterval(struct tm * tm, fsec_t fsec, int style, char *str) ...@@ -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 */ /* Mark as "non-zero" since the fields are now filled in */
is_nonzero = TRUE; is_nonzero = TRUE;
/* fractional seconds? */ /* need fractional seconds? */
if (fsec != 0) if (fsec != 0)
{ {
#ifdef HAVE_INT64_TIMESTAMP #ifdef HAVE_INT64_TIMESTAMP
...@@ -3536,14 +3537,11 @@ EncodeInterval(struct tm * tm, fsec_t fsec, int style, char *str) ...@@ -3536,14 +3537,11 @@ EncodeInterval(struct tm * tm, fsec_t fsec, int style, char *str)
#endif #endif
TrimTrailingZeros(cp); TrimTrailingZeros(cp);
cp += strlen(cp); cp += strlen(cp);
is_nonzero = TRUE;
} }
/* otherwise, integer seconds only? */ else
else if (tm->tm_sec != 0)
{ {
sprintf(cp, ":%02d", abs(tm->tm_sec)); sprintf(cp, ":%02d", abs(tm->tm_sec));
cp += strlen(cp); cp += strlen(cp);
is_nonzero = TRUE;
} }
} }
break; break;
......
...@@ -6,43 +6,43 @@ SET DATESTYLE = 'ISO'; ...@@ -6,43 +6,43 @@ SET DATESTYLE = 'ISO';
SELECT INTERVAL '01:00' AS "One hour"; SELECT INTERVAL '01:00' AS "One hour";
One hour One hour
---------- ----------
01:00 01:00:00
(1 row) (1 row)
SELECT INTERVAL '+02:00' AS "Two hours"; SELECT INTERVAL '+02:00' AS "Two hours";
Two hours Two hours
----------- -----------
02:00 02:00:00
(1 row) (1 row)
SELECT INTERVAL '-08:00' AS "Eight hours"; SELECT INTERVAL '-08:00' AS "Eight hours";
Eight hours Eight hours
------------- -------------
-08:00 -08:00:00
(1 row) (1 row)
SELECT INTERVAL '-05' AS "Five hours"; SELECT INTERVAL '-05' AS "Five hours";
Five hours Five hours
------------ ------------
-05:00 -05:00:00
(1 row) (1 row)
SELECT INTERVAL '-1 +02:03' AS "22 hours ago..."; SELECT INTERVAL '-1 +02:03' AS "22 hours ago...";
22 hours ago... 22 hours ago...
----------------- -----------------
-21:57 -21:57:00
(1 row) (1 row)
SELECT INTERVAL '-1 days +02:03' AS "22 hours ago..."; SELECT INTERVAL '-1 days +02:03' AS "22 hours ago...";
22 hours ago... 22 hours ago...
----------------- -----------------
-21:57 -21:57:00
(1 row) (1 row)
SELECT INTERVAL '10 years -11 month -12 days +13:14' AS "9 years..."; SELECT INTERVAL '10 years -11 month -12 days +13:14' AS "9 years...";
9 years... 9 years...
------------------------------- ----------------------------------
9 years 1 mon -11 days -10:46 9 years 1 mon -11 days -10:46:00
(1 row) (1 row)
CREATE TABLE INTERVAL_TBL (f1 interval); CREATE TABLE INTERVAL_TBL (f1 interval);
...@@ -63,10 +63,10 @@ INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 30 eons ago'); ...@@ -63,10 +63,10 @@ INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 30 eons ago');
ERROR: Bad interval external representation '@ 30 eons ago' ERROR: Bad interval external representation '@ 30 eons ago'
-- test interval operators -- test interval operators
SELECT '' AS ten, INTERVAL_TBL.*; SELECT '' AS ten, INTERVAL_TBL.*;
ten | f1 ten | f1
-----+---------------- -----+-----------------
| 00:01 | 00:01:00
| 05:00 | 05:00:00
| 10 days | 10 days
| 34 years | 34 years
| 3 mons | 3 mons
...@@ -74,30 +74,30 @@ SELECT '' AS ten, INTERVAL_TBL.*; ...@@ -74,30 +74,30 @@ SELECT '' AS ten, INTERVAL_TBL.*;
| 1 day 02:03:04 | 1 day 02:03:04
| 6 years | 6 years
| 5 mons | 5 mons
| 5 mons 12:00 | 5 mons 12:00:00
(10 rows) (10 rows)
SELECT '' AS nine, INTERVAL_TBL.* SELECT '' AS nine, INTERVAL_TBL.*
WHERE INTERVAL_TBL.f1 <> interval '@ 10 days'; WHERE INTERVAL_TBL.f1 <> interval '@ 10 days';
nine | f1 nine | f1
------+---------------- ------+-----------------
| 00:01 | 00:01:00
| 05:00 | 05:00:00
| 34 years | 34 years
| 3 mons | 3 mons
| -00:00:14 | -00:00:14
| 1 day 02:03:04 | 1 day 02:03:04
| 6 years | 6 years
| 5 mons | 5 mons
| 5 mons 12:00 | 5 mons 12:00:00
(9 rows) (9 rows)
SELECT '' AS three, INTERVAL_TBL.* SELECT '' AS three, INTERVAL_TBL.*
WHERE INTERVAL_TBL.f1 <= interval '@ 5 hours'; WHERE INTERVAL_TBL.f1 <= interval '@ 5 hours';
three | f1 three | f1
-------+----------- -------+-----------
| 00:01 | 00:01:00
| 05:00 | 05:00:00
| -00:00:14 | -00:00:14
(3 rows) (3 rows)
...@@ -105,8 +105,8 @@ SELECT '' AS three, INTERVAL_TBL.* ...@@ -105,8 +105,8 @@ SELECT '' AS three, INTERVAL_TBL.*
WHERE INTERVAL_TBL.f1 < interval '@ 1 day'; WHERE INTERVAL_TBL.f1 < interval '@ 1 day';
three | f1 three | f1
-------+----------- -------+-----------
| 00:01 | 00:01:00
| 05:00 | 05:00:00
| -00:00:14 | -00:00:14
(3 rows) (3 rows)
...@@ -119,81 +119,81 @@ SELECT '' AS one, INTERVAL_TBL.* ...@@ -119,81 +119,81 @@ SELECT '' AS one, INTERVAL_TBL.*
SELECT '' AS five, INTERVAL_TBL.* SELECT '' AS five, INTERVAL_TBL.*
WHERE INTERVAL_TBL.f1 >= interval '@ 1 month'; WHERE INTERVAL_TBL.f1 >= interval '@ 1 month';
five | f1 five | f1
------+-------------- ------+-----------------
| 34 years | 34 years
| 3 mons | 3 mons
| 6 years | 6 years
| 5 mons | 5 mons
| 5 mons 12:00 | 5 mons 12:00:00
(5 rows) (5 rows)
SELECT '' AS nine, INTERVAL_TBL.* SELECT '' AS nine, INTERVAL_TBL.*
WHERE INTERVAL_TBL.f1 > interval '@ 3 seconds ago'; WHERE INTERVAL_TBL.f1 > interval '@ 3 seconds ago';
nine | f1 nine | f1
------+---------------- ------+-----------------
| 00:01 | 00:01:00
| 05:00 | 05:00:00
| 10 days | 10 days
| 34 years | 34 years
| 3 mons | 3 mons
| 1 day 02:03:04 | 1 day 02:03:04
| 6 years | 6 years
| 5 mons | 5 mons
| 5 mons 12:00 | 5 mons 12:00:00
(9 rows) (9 rows)
SELECT '' AS fortyfive, r1.*, r2.* SELECT '' AS fortyfive, r1.*, r2.*
FROM INTERVAL_TBL r1, INTERVAL_TBL r2 FROM INTERVAL_TBL r1, INTERVAL_TBL r2
WHERE r1.f1 > r2.f1 WHERE r1.f1 > r2.f1
ORDER BY r1.f1, r2.f1; ORDER BY r1.f1, r2.f1;
fortyfive | f1 | f1 fortyfive | f1 | f1
-----------+----------------+---------------- -----------+-----------------+-----------------
| 00:01 | -00:00:14 | 00:01:00 | -00:00:14
| 05:00 | -00:00:14 | 05:00:00 | -00:00:14
| 05:00 | 00:01 | 05:00:00 | 00:01:00
| 1 day 02:03:04 | -00:00:14 | 1 day 02:03:04 | -00:00:14
| 1 day 02:03:04 | 00:01 | 1 day 02:03:04 | 00:01:00
| 1 day 02:03:04 | 05:00 | 1 day 02:03:04 | 05:00:00
| 10 days | -00:00:14 | 10 days | -00:00:14
| 10 days | 00:01 | 10 days | 00:01:00
| 10 days | 05:00 | 10 days | 05:00:00
| 10 days | 1 day 02:03:04 | 10 days | 1 day 02:03:04
| 3 mons | -00:00:14 | 3 mons | -00:00:14
| 3 mons | 00:01 | 3 mons | 00:01:00
| 3 mons | 05:00 | 3 mons | 05:00:00
| 3 mons | 1 day 02:03:04 | 3 mons | 1 day 02:03:04
| 3 mons | 10 days | 3 mons | 10 days
| 5 mons | -00:00:14 | 5 mons | -00:00:14
| 5 mons | 00:01 | 5 mons | 00:01:00
| 5 mons | 05:00 | 5 mons | 05:00:00
| 5 mons | 1 day 02:03:04 | 5 mons | 1 day 02:03:04
| 5 mons | 10 days | 5 mons | 10 days
| 5 mons | 3 mons | 5 mons | 3 mons
| 5 mons 12:00 | -00:00:14 | 5 mons 12:00:00 | -00:00:14
| 5 mons 12:00 | 00:01 | 5 mons 12:00:00 | 00:01:00
| 5 mons 12:00 | 05:00 | 5 mons 12:00:00 | 05:00:00
| 5 mons 12:00 | 1 day 02:03:04 | 5 mons 12:00:00 | 1 day 02:03:04
| 5 mons 12:00 | 10 days | 5 mons 12:00:00 | 10 days
| 5 mons 12:00 | 3 mons | 5 mons 12:00:00 | 3 mons
| 5 mons 12:00 | 5 mons | 5 mons 12:00:00 | 5 mons
| 6 years | -00:00:14 | 6 years | -00:00:14
| 6 years | 00:01 | 6 years | 00:01:00
| 6 years | 05:00 | 6 years | 05:00:00
| 6 years | 1 day 02:03:04 | 6 years | 1 day 02:03:04
| 6 years | 10 days | 6 years | 10 days
| 6 years | 3 mons | 6 years | 3 mons
| 6 years | 5 mons | 6 years | 5 mons
| 6 years | 5 mons 12:00 | 6 years | 5 mons 12:00:00
| 34 years | -00:00:14 | 34 years | -00:00:14
| 34 years | 00:01 | 34 years | 00:01:00
| 34 years | 05:00 | 34 years | 05:00:00
| 34 years | 1 day 02:03:04 | 34 years | 1 day 02:03:04
| 34 years | 10 days | 34 years | 10 days
| 34 years | 3 mons | 34 years | 3 mons
| 34 years | 5 mons | 34 years | 5 mons
| 34 years | 5 mons 12:00 | 34 years | 5 mons 12:00:00
| 34 years | 6 years | 34 years | 6 years
(45 rows) (45 rows)
SET DATESTYLE = 'postgres'; SET DATESTYLE = 'postgres';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment