Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pFzDk-0006bs-3Q for pgsql-general@arkaria.postgresql.org; Thu, 12 Jan 2023 15:10:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pFzDj-0005bP-3j for pgsql-general@arkaria.postgresql.org; Thu, 12 Jan 2023 15:10:23 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pFzBo-0006Ws-Ec for pgsql-general@lists.postgresql.org; Thu, 12 Jan 2023 15:08:24 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pFzBi-0005B5-4x for pgsql-general@lists.postgresql.org; Thu, 12 Jan 2023 15:08:23 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 30CF8EWr953417; Thu, 12 Jan 2023 10:08:14 -0500 From: Tom Lane To: Sebastien Flaesch cc: pgsql-general Subject: Re: Intervals and ISO 8601 duration In-reply-to: References: Comments: In-reply-to Sebastien Flaesch message dated "Thu, 12 Jan 2023 13:40:43 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <953415.1673536094.1@sss.pgh.pa.us> Date: Thu, 12 Jan 2023 10:08:14 -0500 Message-ID: <953416.1673536094@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Sebastien Flaesch writes: > PostgreSQL has the INTERVAL type, which can be defined with fields such as: > INTERVAL YEAR TO MONTH (year-month class) > INTERVAL DAY TO SECOND(p) (day-second class) You can also say just INTERVAL, without any of the restrictions. > It's not possible to define an INTERVAL YEAR TO SECOND(p), which makes > sense, It's not so much that it doesn't make sense as that the SQL standard doesn't have such a spelling. They enumerate a few allowed combinations (I think that no-modifiers is one of them), and we accept those for pro forma syntax compliance. > Should the following convert to a day-second interval? > test1=> select cast('P2Y10M15DT10H30M20S' as interval day to second); > interval > ---------------------------------- > 2 years 10 mons 15 days 10:30:20 > (1 row) > Should PostgreSQL not raise an SQL error in above cases? We regard these modifiers as similar to precision restrictions in numerics and timestamps: we will round off low-order fields to match the typmod, but we will not throw away high-order fields. This probably doesn't match the SQL spec in detail, but the details of their datetime types are sufficiently brain-dead that we've never worried about that too much (eg, they still don't have a model for daylight-savings time, last I checked). What Postgres actually stores for an interval is three fields: months, days, and microseconds. If we're forced to interconvert between those units, we use 30 days = 1 month and 24 hours = 1 day, but it's usually best to avoid doing that. regards, tom lane